J3.x

Triggering content plugins in your extension: Difference between revisions

From Joomla! Documentation

m added the exact code to use content plugins
Marked this version for translation
 
(26 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Development]]
<noinclude><languages /></noinclude>
[[Category:Plugins]]
<noinclude>{{Joomla version|version=3.x}}</noinclude>
<translate><!--T:1-->
A common example of [[S:MyLanguage/Supporting_plugins_in_your_component|using plugins]] is to run the '''content plugins''' on some text. This is useful if you want to support plugins that usually work on Content from a custom extension. For the content prepare trigger you can simply call:</translate>


Using the plugin system in your addon is fairly simple. The most important part is good planning because, to some degree, you're defining an interface for other people to use.
To activate the plugins for your addon, you just have to use the following  code:
<source lang="php">
<source lang="php">
$dispatcher =& JDispatcher::getInstance();
$text = JHtml::_('content.prepare', $text);
$results = $dispatcher->trigger('onSomething', $params);
</source>
</source>
This function calls everyplugin that has registered itself for the event 'onSomething' and hands over the array $param as parameter.


As a return value you get an array of results from these plugins. All this is of course optional, you could also use the system without parameters and/or a result.
<translate><!--T:2-->
For any other content triggers you must call:</translate>


You can also pass arguments by reference to allow plugins to edit the passed data. For example:
<source lang="php">
<source lang="php">
$dispatcher =& JDispatcher::getInstance();
$article = new stdClass;
$dispatcher->trigger('onSomething', &$params);
$article->text = $text;
</source>


A common usage might be to use the content plugins if you have an editable field. This is the code that will do that:
// <translate><!--T:7--> add more to parameters if needed</translate>
$params = new JObject;


<source lang="php">
// <translate><!--T:8--> Note JEventDispatcher succeeded the older JDispatcher from Joomla 1.5/2.5 however it does still work if you need to keep compatibility.</translate>
        $dispatcher =& JDispatcher::getInstance();
JPluginHelper::importPlugin('content');
        $item->text = & your_text_area_item; 
$dispatcher = JEventDispatcher::getInstance();
        $item->params = clone($params);
$dispatcher->trigger('onContentPrepare', array('some.context', &$article, &$params, 0));
        JPluginHelper::importPlugin('content');  
        $dispatcher->trigger('onPrepareContent', array (& $item, & $item->params, 0));  
</source>
</source>


You might want to look at core components (for example com_content) for an example.
<translate><!--T:3-->
You might want to look at core components (for example com_content) for an example. See the [[S:MyLanguage/Plugin/Events|triggers page]] for the possible content plugin triggers.</translate>
 
<noinclude>
[[Category:Extension development{{#translation:}}]]
[[Category:Plugins{{#translation:}}]]
[[Category:Plugin Development{{#translation:}}]]
[[Category:Development Recommended Reading{{#translation:}}]]
</noinclude>

Latest revision as of 17:24, 7 April 2020

Joomla! 
3.x

A common example of using plugins is to run the content plugins on some text. This is useful if you want to support plugins that usually work on Content from a custom extension. For the content prepare trigger you can simply call:

$text = JHtml::_('content.prepare', $text);

For any other content triggers you must call:

$article = new stdClass;
$article->text = $text;

// add more to parameters if needed
$params = new JObject;

// Note JEventDispatcher succeeded the older JDispatcher from Joomla 1.5/2.5 however it does still work if you need to keep compatibility.
JPluginHelper::importPlugin('content');
$dispatcher = JEventDispatcher::getInstance();
$dispatcher->trigger('onContentPrepare', array('some.context', &$article, &$params, 0));

You might want to look at core components (for example com_content) for an example. See the triggers page for the possible content plugin triggers.