Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Today 00:36:46

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,052
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

Board footer

Powered by FluxBB