Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 00:36:46

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,059
Website GitHub

Image panel customisation

A few of the admin-side panels have already been given additional functionality to allow plugins to do more customisation. Now it’s the turn of the Images panel. Callbacks have been added so you can:

  • Register your own custom steps.
  • Add fields to the displayed back-end table.
  • Add row data in these extra fields.

To show you how easy it is to add a single field to the images panel and to perform custom steps now, here’s some simple plugin code that demonstrates the features. It adds a single field called “Location” to the database and panel.

It’s not a downloadable/installable plugin, because I want to show the code here, and it uses hard-coded language strings so could do with polishing, but it gives you a flavour of what is possible. If you want to play along…

  1. From Admin->Plugins, hit Create.
  2. Give the plugin a name, owner, version, etc.
  3. Ensure its type is “Back-end only (with async)”.
  4. Ensure the ‘Lifecycle notify’ checkbox is set (although it won’t do anything in this case, it will save head scratching if you export the plugin as an installable template/txt file).
  5. Paste the code below into the Code box and Save everything.
  6. Enable the plugin and visit the Images panel.

The code functions are briefly commented, so you can see what each registered callback represents.

if (txpinterface === 'admin') {
	new smd_img_extra();
}

class smd_img_extra
{
	/**
	 * Kick everything off by registering additional functionality on the Images panel.
	 */
	public function __construct()
	{
		global $event;

        register_callback(array($this, 'welcome'), 'plugin_lifecycle.smd_img_extra');
		register_callback(array($this, 'add_step'), 'image', 'steps');
		register_callback(array($this, 'step_handler'), 'image', 'smd_mystep');
		register_callback(array($this, 'add_fields'), 'image', 'fields', 'list');
		register_callback(array($this, 'row_data'), 'image_ui', 'list.row');
		register_callback(array($this, 'edit'), 'image_ui', 'extend_detail_form');
		register_callback(array($this, 'save'), 'image', 'image_save', 1);
		register_callback(array($this, 'search'), 'search_criteria', 'image');

        // Ensure installation/upgrade has taken place in case lifecycle isn't fired.
        if ($event !== 'plugin') {
            $this->install();
        }
	}

	/**
	 * Handle lifecycle dispatching.
	 */
	public function welcome($evt, $stp)
	{
        switch ($stp) {
            case 'installed':
            	$this->install();
                break;
            case 'deleted':
            	$this->uninstall();
                break;
        }
	}

	/**
	 * Add the new column to the database table on plugin installation.
	 */
	public function install()
	{
    	safe_alter('txp_image', 'ADD COLUMN location VARCHAR(255)');
	}

	/**
	 * Remove the new column from the database table on plugin de-installation.
	 */
	public function uninstall()
	{
   		safe_alter('txp_image', 'DROP COLUMN location');
	}

	/**
	 * Add a new step called 'smd_mystep' to the Images panel.
	 */
	public function add_step($evt, $stp, &$steps)
	{
		return $steps['mystep'] = false;
	}

	/**
	 * Do stuff when 'smd_mystep' is called.
	 */
	public function step_handler($evt, $stp)
	{
		dmp('Pouring the tea...');
		image_list();
	}

	/**
	 * Register the new 'location' column.
	 */
	public function add_fields($evt, $stp, &$payload)
	{
		return array_merge($payload, $payload['location'] = array(
			'column' => 'txp_image.location',
			'label' => 'Location',
		));
	}

	/**
	 * Add the new 'location' data to each row.
	 */
	public function row_data($evt, $stp, $data, $rs)
	{
		return td(txpspecialchars($rs['location']), '', 'txp-list-col-location');
	}

	/**
	 * Allow the 'location' value to be edited.
	 */
	public function edit($evt, $stp, $data, $rs)
	{
		return inputLabel('location', fInput('text', 'location', $rs['location']), 'Location');
	}

	/**
	 * Save the 'location' value.
	 */
	public function save($evt, $stp)
	{
		$loc = ps('location');
		$id = assert_int(ps('id'));
		safe_update('txp_image', "location = '" . doSlash($loc) . "'", 'id = ' . $id);
	}

	/**
	 * Allow the 'location' field to be searched.
	 */
	public function search($evt, $stp, &$methods)
	{
		$methods['location'] = array(
			'column' => 'txp_image.location',
			'label' => 'Location',
		);
	}
}

Have fun!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 Yesterday 09:52:16

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,055
Website GitHub

Re: Image panel customisation

Nice! This would be a way of making dedicated expansions to image handling and goes beyond what jcr_image_custom can currently do by allowing you to add the field to the list view and include it in search.

Is there any way of influencing where the fields appear in the edit panel, and where the column also appears in the table listing?


TXP Builders – finely-crafted code, design and txp

Offline

#3 Yesterday 11:24:00

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,059
Website GitHub

Re: Image panel customisation

jakob wrote #341196:

Is there any way of influencing where the fields appear in the edit panel, and where the column also appears in the table listing?

At the moment no, short of using the new steps to rewire the entire panel. The callbacks are in specific places so the panels are limited to “extend”.

In theory, the column order could be rejigged because they’re by reference, so if you edited the array, the column order would change. BUT, the callback that adds the values is in a fixed location and this will always add stuff to the right of everything else.

Come the revolution we’ll look into making panel building more flexible. For now this is just a stepping stone to help plugins a bit.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 Yesterday 21:28:27

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,055
Website GitHub

Re: Image panel customisation

Thanks for the clarification. Entirely understandable, even if I had been hoping to be able to remove some edits to the core on two sites I inherited, which add a menu tree column to the list pane.

I can’t find the parallel thread that applies to the article list pane, so apologies for asking here. Would these new callbacks – specifically the add column and search callbacks – make it possible to make things like smd_tags searchable and filterable from the list pane?

smd_tags offers greater organisational flexibility for structuring the contents of a site and overcomes the two category limit, but lack the admin-side handling options that categories have. For example, to show the articles with a particular tag, one has to go to the smd_tags pane and click the number alongside the tag. That transfers the article search into a list of relevant id numbers.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB