Search code examples
phpjoomlaoutput

Joomla plugin get contents of whole page before output


With my Joomla plugin I would like to get and modify all the contents before the output is created.

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

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

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

I tried with onContentAfterDisplay but it continues to change only a small piece and not all the output.


Solution

  • Why won't you just do:

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

    ?

    EDIT

    Basing on your response, here is a way to modify whole page content/body in plugin. Add this method to your plugin:

    public function onAfterRender()
    {
        $app = JFactory::getApplication();
        $currentBodyToChange = $app->getBody();
    
        //do something with $currentBodyToChange
        //$bodyChanged is modified $currentBodyToChange
    
        $app->setBody($bodyChanged);
    }