Plugin development

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Plugin development

Post by uweD » Sun Aug 26, 2007 9:31 pm

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
Last edited by uweD on Wed Aug 29, 2007 2:07 am, edited 1 time in total.
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
Chris Davenport
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Thu Aug 18, 2005 8:57 am
Location: Shrewsbury, Shropshire, United Kingdom

Re: Plugin development

Post by Chris Davenport » Sun Aug 26, 2007 9:47 pm

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.
Joomla! Core Team Member | Documentation Working Group Coordinator

"Reality is merely an illusion, although a very persistent one" - Albert Einstein
"We are suspended in language such that we don't know what is up and what is down" - Niels Bohr

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Mon Aug 27, 2007 3:09 am

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
Last edited by uweD on Mon Aug 27, 2007 3:13 am, edited 1 time in total.
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Mon Aug 27, 2007 3:13 am

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
AmyStephen
Joomla! Guru
Joomla! Guru
Posts: 579
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Plugin development

Post by AmyStephen » Mon Aug 27, 2007 5:09 pm

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 :)
~*~ Joomla!'s Queen of the Blues - Jennifer Marriott ~*~
http://OpenSourceCommunity.org/node/1719/

User avatar
Tonie
Joomla! Ace
Joomla! Ace
Posts: 1585
Joined: Thu Aug 18, 2005 7:13 am
Contact:

Re: Plugin development

Post by Tonie » Mon Aug 27, 2007 5:12 pm

Moving to Joombie Coding Q&A (who made up that name  ??? ;) ).
Last edited by Tonie on Mon Aug 27, 2007 5:13 pm, edited 1 time in total.
Antonie de Wilde - Forum admin

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Mon Aug 27, 2007 9:22 pm

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
AmyStephen
Joomla! Guru
Joomla! Guru
Posts: 579
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Plugin development

Post by AmyStephen » Mon Aug 27, 2007 9:26 pm

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 :)
~*~ Joomla!'s Queen of the Blues - Jennifer Marriott ~*~
http://OpenSourceCommunity.org/node/1719/

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Mon Aug 27, 2007 10:57 pm

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
seadap
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Mon Dec 04, 2006 12:22 am
Contact:

Re: Plugin development

Post by seadap » Tue Aug 28, 2007 7:21 pm

Hey Uwe, welcome to J!101 Q&A.  I can't wait to see how you get this working.  I'm learning myself.
Knowledge is realizing that the street is one-way, wisdom is looking both directions anyway.

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Tue Aug 28, 2007 10:12 pm

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
Last edited by uweD on Tue Aug 28, 2007 10:17 pm, edited 1 time in total.
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Plugin development

Post by ianmac » Tue Aug 28, 2007 11:30 pm

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
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Tue Aug 28, 2007 11:45 pm

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Plugin development

Post by ianmac » Wed Aug 29, 2007 1:33 am

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
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Wed Aug 29, 2007 1:47 am

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Wed Aug 29, 2007 2:06 am

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
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga

User avatar
uweD
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon May 14, 2007 9:38 pm
Location: Auckland -- New Zealand
Contact:

Re: Plugin development

Post by uweD » Wed Aug 29, 2007 4:53 am

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
Last edited by uweD on Wed Aug 29, 2007 5:01 am, edited 1 time in total.
________________________
Uwe Duesing
http://www.uweduesing.com
Skype:uweOnehunga


Post Reply