Page 1 of 1

Adding additional plugin triggers for code to hook onto

Posted: Wed Apr 22, 2009 12:36 pm
by harveyn
Introduction
A core code hack that we use extensively on our Joomla 1.0.x sites is to add an additional plugin group called 'tabs'. These are then called on a new trigger of 'onContentTab'. These plugins then can be used to add further information to a content item - currently we use it to link to components we've written to topic code items from a defined list; set comprehensive meta data from a predefined hierarchy; and locate content items on a google map. We also had to add a new trigger to the save function but this has now been add as we have 'onAfterContentSave'.

Present Issue
If you create a component that relates additional information to an existing content item the only way to manage it is through the component - this proposal means you can manage it when editing the content item.

Proposal
Adding the 'tabs' group and a new trigger of 'onContentTab' can allow third party developers to extend the control over additional information related to the content item. Some of the major issues with Joomla could then be solved by third party components:
Comments can be a stand alone component with the comments for the item pulled from the component and displayed in a tab when editing the component, allowing the editor to moderate the comments for a item then and there;
A Version Control tab can get a list of alternative versions of the content from a version control component - and, for example, show then in a pop up window and be used to select a different version (even a newer version);

Implementation
I'm sure this is not the right way to do this but I was trying to go from the old 1.0.x way to the new system.

After the last core tab is added

Code: Select all

line 538: echo $pane->endPanel();
Add

Code: Select all

// 20090421 NH - Adding tabs plugins to content editing so that topic coding etc can run
$dispatcher	=& JDispatcher::getInstance();
$tabs =& JPluginHelper::importPlugin('tabs');

// Get tab plugins
$dispatcher->trigger('onContentTab', array (& $row, $pane));
This imports the tabs plugins and then triggers the 'onContentTab' events.

A basic plugin to add an additional topic to the content item attribs field

Code: Select all

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

$dispatcher	=& JDispatcher::getInstance();
$dispatcher->register('onContentTab', 'plgTabsTopic');

/**
 * Example system plugin
 */
class plgTabsTopic extends JPlugin
{
    /**
     * Constructor
     *
     * For php4 compatibility we must not use the __constructor as a constructor for plugins
     * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
     * This causes problems with cross-referencing necessary for the observer design pattern.
     *
     * @access  protected
     * @param   object  $subject The object to observe
     * @param   array   $config  An array that holds the plugin configuration
     * @since   1.0
     */
    function plgTabsTopic( &$subject )
    {
        parent::__construct( $subject );

        // Do some extra initialisation in this constructor if required
    } // plgTabsTopic

    /**
     * Do something onContentTab
     */
    function onContentTab($article, $pane)
    {
        $params = new JParameter( $article->attribs );

        // Perform some action
		echo $pane->startPanel( 'Topics', "topics-page" );
        echo '<table class="paramlist admintable" cellspacing="1" width="100%">
<tbody>
<tr>
<td class="paramlist_key" width="40%">
Topic Name
</td>
<td class="paramlist_value">
<input id="paramstopic" class="inputbox" name="params[topic]" value="'. (isset($params->_registry['_default']['data']->topic) ? $params->_registry['_default']['data']->topic : '') .'">
</td>
</tr>
</tbody>
</table>'
             ;

		echo $pane->endPanel();
    } // onContentTab
}
I based this on the tutorial on the developer area, I'm sure I am probably doing the $dispatcher calls wrong but it's the only way I've got it working so far. Anyway, this successfully allows the editor to add further entries to the attribs field.

Integrating the plugin with a component would allow the system to create a list of options instead of a free text box. Then, using another plugin that is triggered on the 'onAfterContentSave', the select option can be stripped from the attribs field and saved to the component.

Impacts
Users
The editor of a content item can now manage all component entries that a linked to a content item and allows third party components to greatly improve integration with content - link an Image Gallery category with a content item as another example.

Third Party Extensions
Components can now be used to extend content items.

Performance
This is taken from the original white paper that proposed the addition of the 'onBeforeContentSave' and 'onAfterContentSave' triggers that was accepted and rolled out with 1.5.4 (I think), see http://forum.joomla.org/viewtopic.php?p ... 0#p1206320.
"There will be an impact on performance as the number of plugins increases, however this is mitigated by the fact that it will only occur when a user attempts to save an item. The true performance impact will be determined by the plugins that are triggered and what they cause the system to do (e.g. database queries)."
The 'tabs' plugins should only be loaded in the administration pages so should not affect front end performance and if they do does this indicate there is a problem with how the plugins are currently working?

Upgrade impacts (from 1.5.x)
There would be no impacts on existing sites however any plugins developed to exploit these new triggers (and plugin group 'tabs') would obviously only compatible with the newer versions.