Calling plugin method inside custom component

2,930 views
Skip to first unread message

Antanas Vipartas

unread,
Aug 23, 2011, 11:57:25 AM8/23/11
to joomla-de...@googlegroups.com
Hi, I'm developing with Joomla 1.7

I created my custom component and a content plugin. How should I call my plugin's method onContentBeforeDisplay() inside components view?

this doesn't work...
$plugin =& JPluginHelper::getPlugin('content', 'kokteiliai');
$plugin::onContentBeforeDisplay();

Mark Dexter

unread,
Aug 23, 2011, 12:14:23 PM8/23/11
to joomla-de...@googlegroups.com
I think you need to trigger the event. Something like this:

$dispatcher = JDispatcher::getInstance();
$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_content.article', &$item, &$this->params, $offset));

I copied that from com_content. The array are the arguments for the plugin method. You might also want to set a context in your custom component so you can easily check this in your plugin, to make sure you only execute the plugin when you are in your custom component. Hope that helps. Mark


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/Q1HpjD9--SIJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Antanas Vipartas

unread,
Aug 23, 2011, 2:26:44 PM8/23/11
to joomla-de...@googlegroups.com
Thanks for the solution. I have already tried that and it works.

But this method calls all content plugins which is not very good for performance optimization. I would like to execute only my custom plugin.

It should be other way to call exactly that plugin...

Mark Dexter

unread,
Aug 23, 2011, 4:33:16 PM8/23/11
to joomla-de...@googlegroups.com
As I understand it, the whole idea behind plugins is that the calling event doesn't have to know what if any plugins might get called. If you just want to call a fixed method at a fixed point in the program, why use a plugin? Why not just call a method? Good luck. Mark

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/wnA3egq2-RsJ.

Antanas Vipartas

unread,
Aug 24, 2011, 12:51:46 PM8/24/11
to joomla-de...@googlegroups.com
You're right. It's better to include that piece of code into separate function/library.

Do you know any way to include library/externa file (containing my own functions) without changing the core code?

exps1

unread,
Aug 24, 2011, 2:13:15 PM8/24/11
to joomla-de...@googlegroups.com
Here is how I use custom plugins:

Typically in the view....

// Plugin params
$data = array('param1' => $items, 'param1' => $location, ....);

// Load plugin       
JPluginHelper::importPlugin('exps', 'plugin_name');
       
$dispatcher = JDispatcher::getInstance();
       
$result = $dispatcher->trigger('getMyFunction', $data);
       
$this->items = $result[0];

My custom plugins are installed in plugin folder "exps"

Typically, I use plugins to build abstract connectors (e.g. payment and shipping API's, or provide utility functions) that allow any component or module to use the API / utility without having to implement it. All the component needs to do is invoke the plugin, pass data and handle response.

Micah

Mark Dexter

unread,
Aug 24, 2011, 4:49:57 PM8/24/11
to joomla-de...@googlegroups.com
I think you just put the require statements or JLoader::register statements in your code. You can also override the standard includes files if you like. If it were me, I think I would just create a single file that loads my custom stuff (using JLoader::register) and then include that file where I need it. There could be an easier way as well. Good luck. Mark

On Wed, Aug 24, 2011 at 9:51 AM, Antanas Vipartas <vipan...@gmail.com> wrote:
You're right. It's better to include that piece of code into separate function/library.

Do you know any way to include library/externa file (containing my own functions) without changing the core code?

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/bDl6GmUazLwJ.
Reply all
Reply to author
Forward
0 new messages