Page 1 of 1

Plugin development

Posted: Sun Aug 26, 2007 9:31 pm
by uweD
Please follow up the new post

Hi,

Can anyone point me in a good direction where I can find some information


-> how to trigger an event
-> how to register an event
-> how to code a plugin which reacts to an event
-> how to make that plugin load a module and fill it with values?


I just don't seems to be able to find the answers myself. Thanks for your help,

Uwe

Re: Plugin development

Posted: Sun Aug 26, 2007 9:47 pm
by Chris Davenport
There is some documentation here: http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,tutorials:plugins/

It may not answer all your questions, but it's a start. :)

Regards,
Chris.

Re: Plugin development

Posted: Mon Aug 27, 2007 3:09 am
by uweD
Hi Chris,

Thanks a lot for the answer which was really helpful. Couple of more questions though as I ran into a couple of problems.

  • Triggeran event
    Have a look in the source code below: I tried to follow the example you mentioned. But I seem to have a problem not being able to register/trigger  the event properly.
    Is the following statement the only thing I need to do the trigger an event, and receive a value? The following line of code lives within my controller.

Code: Select all

$plgResult = $mainframe->triggerEvent('onSearchSuccesFull');
    I simply reveive an empty array as a result.


  • Register an event
    Is it enough to register my plugin the following way.
    I again followed the example you pointed out before.

Code: Select all

<?php

defined('_JEXEC') or die();

jimport('joomla.event.plugin');

class plgSearch extends JPlugin {


   function plgSearch(& $subject) {
      parent::__construct($subject);
   }


   function onSearchSuccesFull()
   {
      global $mainframe;
      
      return ("test");

   }
}

    This plugin does not seem to do anything at all. Ichecked the database in 'jos_plugins'  where the plugin is listed as any other plugin....


Thanks for your help, I am going in circles here  ;)

Uwe

Re: Plugin development

Posted: Mon Aug 27, 2007 3:13 am
by uweD
uweD wrote:Hi Chris,

Thanks a lot for the answer which was really helpful. Couple of more questions though as I ran into a couple of problems.

  • Trigger an event
    Have a look in the source code below: I tried to follow the example you mentioned. But I seem to have a problem not being able to register/trigger  the event properly.
    Is the following statement the only thing I need to do the trigger an event, and receive a value? The following line of code lives within my controller.

Code: Select all

$plgResult = $mainframe->triggerEvent('onSearchSuccesFull');
    I simply reveive an empty array as a result.


  • Register an event
    Is it enough to register my plugin the following way.
    I again followed the example you pointed out before.

Code: Select all

<?php

defined('_JEXEC') or die();

jimport('joomla.event.plugin');

class plgSearch extends JPlugin {


   function plgSearch(& $subject) {
      parent::__construct($subject);
   }


   function onSearchSuccesFull()
   {
      global $mainframe;
      
      return ("test");

   }
}

    This plugin does not seem to do anything at all. Ichecked the database in 'jos_plugins'  where the plugin is listed as any other plugin....






Thanks for your help, I am going in circles here  ;)

Uwe

Re: Plugin development

Posted: Mon Aug 27, 2007 5:09 pm
by AmyStephen
Uwe -

Thanks for your PM. I am not at all experienced in plugin development. But, I am trying to learn! :-)

The Joomla! forums have a new area called Joomla! 101 Coding. I will ask the moderators to move this thread into the Joombie Coding Q/A board. It's an incubator, of sorts, for new J! devs and it might be a good place to get support.

Will look for you "over there!"
Amy :)

Re: Plugin development

Posted: Mon Aug 27, 2007 5:12 pm
by Tonie
Moving to Joombie Coding Q&A (who made up that name  ??? ;) ).

Re: Plugin development

Posted: Mon Aug 27, 2007 9:22 pm
by uweD
Thanks Amy,

And sorry for bothering you personally.

As soon as I work out what the problem with my plugin is I will reuse it for a decent 'plugin for beginners' example and post it here,

Cheers,
Uwe

Re: Plugin development

Posted: Mon Aug 27, 2007 9:26 pm
by AmyStephen
I do not mind, at all, Uwe! And, I very much look forward to seeing your example when you are complete.

This should be a good place to ask questions about plugins and events. I'll keep an eye out. I'm certain someone with that knowledge will respond soon.

Thanks!
Amy :)

Re: Plugin development

Posted: Mon Aug 27, 2007 10:57 pm
by uweD
OK,
here is some progress on the matter:
I worked out that I need to use

Code: Select all

JPluginHelper::importPlugin( 'search' );


to actually import the plugins from the 'search' folder before I then use

Code: Select all

$plgResult = $mainframe->triggerEvent('onSearchSuccesFull',array($this->simpleSearchResult));


to actually get the results from the plugins out there listening to the event 'onSearchSuccesFull'.

This approach does the trick as long as I use the legacy-way to code a plugn:

Code: Select all

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

$mainframe->registerEvent( 'onSearchSuccesFull', 'plgPopulateModule' );


function plgPopulateModule ($param) {


return "test";

}


It doesn't work if I follow the example given here
http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,tutorials:plugins/

Any input why that's the case is more then appreciated,

Thanks, Uwe

Re: Plugin development

Posted: Tue Aug 28, 2007 7:21 pm
by seadap
Hey Uwe, welcome to J!101 Q&A.  I can't wait to see how you get this working.  I'm learning myself.

Re: Plugin development

Posted: Tue Aug 28, 2007 10:12 pm
by uweD
well, well I certainly have found some answers to my own very questions from the beginning of $this->post. If they are right answers... who knows  ;) (anyone?).

-> how to trigger an event
-> how to register an event / how to code a plugin which reacts to an event
-> how to code a plugin which reacts to an event
-> how to make that plugin load a module and fill it with values?


1. How to register an event:
I added a plugin in the plugin folder of Joomla! and added these lines of code:

Code: Select all

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

$mainframe->registerEvent( 'onSearchSuccesFull', 'plgPopulateModule' );


function plgPopulateModule () {
   $session =& JFactory::getSession();   
   $searchResult = $session->get('searchResult');      
   $html = "<table class="searchResult">";
   $html .="<tr>";
   $html .="<td>";
   $html .=$searchResult;
   $html .="</td>";
   $html .="</tr>";
   $html .="</table>";

   $module = JModuleHelper::getModule('SearchResultsNavBar');
   $module->content=$html;


      
   return true;
   


}


This plugin registers itself against the event 'onSearchSuccessfull'. When that event occurs, it reads out the session value 'searchResult'. It then puts a bit of (test) HTML stuff together using a table and the 'searchResults'. It then grabs the module 'SearchResultsNavBar' and assigns '$html' to the content field of the $module (object). That makes the module render my new content.

2. How the event is triggered
I use

Code: Select all

$mainframe->triggerEvent('onSearchSuccesFull');

in my data model once the search has finished and the search result was written in the session.



That outcome leaves me with more questions (surprise):

1. Can I execute a method within the model from my plugin?
2. Being able to assign a new value to 'content' makes me wonder if the module in this case is nothing else but a container which displays my (static!) content which is the output from my plugin. That would mean that my display logic would live in the plugin.

That was painful and possibly is not the most effective (or even the right )way to set up a case of 'loose coupling'. Hiopefully an experienced developer sees my post here and explains to me how it is done  ;).

Hope this is post was interesting,
Uwe

Re: Plugin development

Posted: Tue Aug 28, 2007 11:30 pm
by ianmac
Hey!

I would recommend using the new class system...  Then you don't have to mess with registering.

Simple name your class:

plgSearchPluginName...  so:
plgSearchExample

Then, create a method for each event you want to handle.  The name has to coordinate with your xml file as well and your file name.  So, you would have example.php, example.xml and class plgSearchExample.

Here is another tutorial for an authentication plugin, but still can be translated to a search plugin:
http://dev.joomla.org/component/option, ... ins_part2/

triggerEvent will return an array of results from the plugins that were invoked.


Ian

Re: Plugin development

Posted: Tue Aug 28, 2007 11:45 pm
by uweD
Hi ianmac,
That brings me back to my start of this long journey  ;): I just can not make it work that way. Have a look at that code, maybe you know whhy. This plugin listens for the same event;

Code: Select all

<?php

defined('_JEXEC') or die();
jimport('joomla.event.plugin');

class plgSearchBar extends JPlugin{

   var $param      =    null;


    function plgSearchBar(& $subject) {
        parent::__construct($subject);
      
        $this->_plugin = JPluginHelper::getPlugin( 'search', 'plgSearchBar' );
        $this->_params = new JParameter( $this->_plugin->params );
   
    }

    function onSearchSuccesFull ($param) {
      $param = "testPluginEhive";
     
     
     
     
      return $param;
    }
}


What kind of naming concention do need to be aware of for my plugin class?

Thanks a lot,
Uwe

Re: Plugin development

Posted: Wed Aug 29, 2007 1:33 am
by ianmac
Then, create a method for each event you want to handle.  The name has to coordinate with your xml file as well and your file name.  So, you would have example.php, example.xml and class plgSearchExample.


Based on your code above, I would assume that your plugin is named 'Bar', and is in a file called bar.php, with an xml file called bar.xml.

The xml file would look like:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="search">
    <name>Search - Bar</name>
    <author></author>
    <creationDate>May 30, 2007</creationDate>
    <copyright>(C) 2005 - 2007 Open Source Matters. All rights reserved.</copyright>
    <license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
    <authorEmail>[email protected]</authorEmail>
    <authorUrl>www.joomla.org</authorUrl>
    <version>1.5</version>
    <description>An sample search plugin</description>
    <files>
        <filename plugin="bar">bar.php</filename>
    </files>
    <params/>
</install>


Hope that helps.

Ian

Re: Plugin development

Posted: Wed Aug 29, 2007 1:47 am
by uweD
Hi Ian,

Thanks for your help mate, but still no go.
My XML file looks like that

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<install version="1.5" type="plugin" group="search">
    <name>object Search</name>
    <author>Uwe   </author>
    <creationDate>August 2007</creationDate>
    <copyright>2007 xyz</copyright>
    <license>GNU/GPL</license>
    <authorEmail>[email protected]</authorEmail>
    <authorUrl>www.joomla.org</authorUrl>
    <version>1.1</version>
    <description>Allows searching of  Objects</description>
    <files>
        <filename plugin="object">object.php</filename>
    </files>
</install>


My plugin looks like that

Code: Select all

<?php

defined('_JEXEC') or die();
jimport('joomla.event.plugin');

class plgSearchObject extends JPlugin{

    function plgSearchObject(& $subject) {
        parent::__construct($subject);

    }

    function onSearchSuccesFull ($param) {
      $param = "testPlugin";
          
      return $param;
    }
}


The xml file is name 'object.xml'
the plugin is named 'object.php'

Thanks a lot for your help,

uwe

Re: Plugin development

Posted: Wed Aug 29, 2007 2:06 am
by uweD
Ian,

I am very pleased to say: I am there - finally. The last bit of my trip around and around the biock was especially stupid: I just forgot to enable the plugin in the end.  :-[ Was a long day after all,

Thanks a lot for your help, what a great situation to have you reading and writing(!) within this forum.

Cheers mate,

Uwe

Re: Plugin development

Posted: Wed Aug 29, 2007 4:53 am
by uweD
One last word on this topic: I put my experience with plugins in a  little blog http://www.uwe-duesing.com/index.php/joomla-blog. Might be helpful to others, cheers, Uwe