Joomla 插件在输出前获取整个页面的内容

Joomla plugin get contents of whole page before output

使用我的 Joomla 插件,我想在创建输出之前获取并修改所有内容。

function newContent($html)
    {
    $html = "new content";
    return $html;
    }

function onContentPrepare()
    {
    ob_start(array($this, "newContent"));
    return true;
    }

function onContentBeforeDisplay()
    {
    ob_end_flush();
    return true;
    }

我试过 onContentAfterDisplay 但它继续只改变一小部分而不是所有输出。

你为什么不这样做:

public function onContentPrepare($context, &$article, &$params, $page = 0)
{
     $article->text = "new content";
}

?

编辑

根据您的回复,这是一种在插件中修改整个页面的方法 content/body。将此方法添加到您的插件:

public function onAfterRender()
{
    $app = JFactory::getApplication();
    $currentBodyToChange = $app->getBody();

    //do something with $currentBodyToChange
    //$bodyChanged is modified $currentBodyToChange

    $app->setBody($bodyChanged);
}

要停止在管理页面上触发插件...

$app = JFactory::getApplication();
if ($app->isSite())  echo 'Front end - do it!';
if ($app->isAdmin()) echo 'Admin pages - ignore!';