J4.x

Triggering content plugins in your extension

From Joomla! Documentation

Revision as of 17:22, 7 April 2020 by M-b-o (talk | contribs) (Marked this version for translation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Other languages:
Deutsch • ‎English • ‎français • ‎中文(台灣)‎
Joomla! 
4.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 = \Joomla\CMS\HTML\HTMLHelper::_('content.prepare', $text);

For any other content triggers you must call:

// These 3 statements go at the top of your file directly below the JEXEC statement
use Joomla\CMS\CMSObject;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;

// This code goes in your component method that's triggering the plugins
$article = new \stdClass;
$article->text = $text;

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

PluginHelper::importPlugin('content');
$app = Factory::getApplication();
$app->triggerEvent('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.