MBaas
MBaas

Reputation: 7530

Joomla: a plugin for a plugin - how to trigger events

I'm working on a plugin for a plugin, but somehow I do not get the new events which I introduced to be executed in the nested plugin. Where's the bug?

In my simplified (and useless) example, foo is the "master-plugin" and bar is a plugin for foo. To proove the plugin is executed, I am simply appending its name to introtext - but this shows that only foos event-handler is called, whereas bars handler is not called (but the class gets constructed)...

foo.php:

<?php

class plgContentFoo extends JPlugin
{

     function onContentPrepare($context,$article,$params,$limitstart)
    {
        jimport('joomla.plugin.plugin');    
        JPluginHelper::importPlugin('content');
        $dispatcher = JDispatcher::getInstance();
        $dispatcher->trigger('executingOCP', array($article));
        $article->introtext .= "<br>Executed foo.OCP";
        return true;
    }


     function executingOCP($arg)
    {
        $arg->introtext .= "<br>*a*Added foo.xOCP";
        $arg->text .= "<br>*b*Added foo.OCP";
        $arg->fulltext .= "<br>*c*Added foo.OCP";

    }
}


?>

bar.php:

<?php

class plgContentBar extends JPlugin
{

    function __construct( &$subject , $config ) {
        echo "executing bar-constructor!";
    }

    function executingOCP($arg)
    {
        $arg->introtext .= "<br>*1*Added bar.OCP";
        $arg->text .= "<br>*2*Added bar.OCP";
        $arg->fulltext .= "<br>*3*Added bar.OCP";
        return true;
    }
}
?>

bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="1.6" method="upgrade" group="content">
<name>Content - bar - a foo-plugin!</name>
<creationDate>2013-05-30</creationDate>
<version>2.0.0.16</version>
<releaseDate>2013-05-30 12:00:58</releaseDate>
<releaseType>First public release!</releaseType>
<author>Michael Baas</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>mbaas.de</authorUrl>
<copyright>(c) 2013 Michael Baas</copyright>
<description>A plugin to handle foos events!</description>
<files>
<filename plugin="bar">bar.php</filename>
</files>
</extension>

and finally foo.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="1.6" method="upgrade" group="content">
<name>Content - foo</name>
<creationDate>2013-05-30</creationDate>
<version>2.0.0.16</version>
<releaseDate>2013-05-30 12:00:58</releaseDate>
<releaseType>First public release!</releaseType>
<author>Michael Baas</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>mbaas.de</authorUrl>
<copyright>(c) 2013 Michael Baas</copyright>
<description>foo is the "main " plugin...</description>
<files>
<filename plugin="foo">foo.php</filename>
</files>
</extension>

For your convenience, there's also a possibility to download these files: http://mbaas.de/foo.zip and http://mbaas.de/bar.zip

Update (2013, June 6th): to simplify debugging, I have made bar a simple content-plugin, but that had no effect: foo.onContentPrepare triggers foo.executingOCP, but bar.executingOCP is never called. Have also updated the zips! I really hope someone can help to get this sorted out and would be happy to give a free licence for the beast I am developing in return for fixing this :)

Upvotes: 3

Views: 3154

Answers (2)

MBaas
MBaas

Reputation: 7530

Well, it turned out the problem was related to the constructor!

see: https://groups.google.com/forum/?hl=de&fromgroups=#!topic/joomla-dev-general/chUTpXCZ25g

When extending JPlugin AND using a custom constructor for the class, do not forget to call the constructor of the overridden class! So I adjusted plgContentBar's constructor and now it all works:

function __construct( &$subject , $config ) {
    parent::__construct($subject, $config);
    echo "executing bar-constructor!";
}

Upvotes: 1

Valentin Despa
Valentin Despa

Reputation: 42562

I think you code is generally good. What I think you do wrong is that you break some naming conventions in Joomla and your plugin doesn't get called.

In the bar manifest, you have group="fooplugins" which actually creates another group of plugins (like content, authentication, etc...). So you can't just name your plugin plgContentBar.

So if bar is also a content plugin, just change the manifest to group="content". If it's not a content plugin, change the class name, probably into plgFoopluginsBar. So both plugins should install under /plugins/content/

Hope this helps.

Upvotes: 1

Related Questions