J4.x

在您的擴充套件中觸發內容

From Joomla! Documentation

This page is a translated version of the page J4.x:Triggering content plugins in your extension and the translation is 80% complete.
Joomla! 
4.x

在文字內容中使用外掛 的常見例子,是在某些文字中執行「內容外掛」,當您的外掛經常搭配內容使用的時候,是很有用的。要準備觸發的內容,您可以呼叫:

$text = \Joomla\CMS\HTML\HTMLHelper::_('content.prepare', $text);

對於其他的內容觸發,您可以呼叫:

// 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;

// 如果需要的話,增加更多的參數
$params = new CMSObject;

PluginHelper::importPlugin('content');
$app = Factory::getApplication();
$app->triggerEvent('onContentPrepare', array('some.context', &$article, &$params, 0));

您可能會想要看看核心元件 (例如 com_content) 以作為範例。參見 triggers page 取得可能的內容外掛觸發器。