We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28436
    • 242 Posts
    Hello Guys,

    i want to replace something before the page is rendered with a placehoder. OK.
    And after rendering i want replace the striped content back in place where i strip it before.

    $e = &$modx->Event;
    
    //var_dump( $e );
    
    switch ($e->name) 
    {
        case "OnWebPagePrerender":
    
            // catch all marked blocks and replace it with a uncritical placeholder // ok, this work   
     
        break;
        case "OnWebPageComplete": // btw. iam looking for the event before this. "OnBeforeWebPageComplete"
    
            // take the catched(and unparsed) blocks and replace them with the give placeholders
            // Dont work.
           
            echo 'here i am OnWebPageComplete'; // Dont work
        break;
    
        default :
        return;
      break;
    }


    Both events are checked in the event tab.

    mmmhhh it sounds logical that i can do nothing with the content after OnWebPageComplete.

    So i think my question is: what is the event between "finishing rendering" and "deliver it to browser"?

    In the list of the plugin execution order, the OnWebPageComplete event is before OnWebPagePreRender? Is this correct?

    Damnit guys, im really confused...

    I’ll be thankfully for every hint.
    Thanks.

    ciao, Stefan

    PS: Sorry for me horrible english.
      • 27376
      • 576 Posts
      These Resources on the wiki will help you determine which events need to be checked:
        • 28436
        • 242 Posts
        Hello Matt,

        thanks you for your reply. That was absolutly that for what iam looking. sometimes i forgot the wiki existent.

        But.....

        what i want to do is catching all <pre> Tags, replace it with an placeholder and put them back after parsing/merging the content.
        The meaning is to protecting them before the parser will destroy all the codesnippets in it.

        $e = &$modx->Event;
        //var_dump( $e );
        
        $_SESSION['codeblocks'] = array();
        
        $i = 1;
        switch ($e->name)
        {
            case "OnParseDocument":
                $content_raw = $modx->documentObject['content'];
                // echo $content_raw;
                function catchCodeblocks( $codeblocks )
                {
                    global $i;
                    $replacement = str_replace($codeblocks[0],'#codeblock#-'.$i.'-#',$codeblocks[0]);
                    $_SESSION['codeblocks'][$replacement] = $codeblocks[0];
                    $i++;
                    return $replacement;
                }
                $content_tmp = preg_replace_callback('|<pre>(.*?)</pre>|si', 'catchCodeblocks', $content_raw);
        
                //echo $content_tmp;
                $modx->documentObject['content'] =  $content_tmp;
            break;
        
            case 'OnWebPagePrerender':
                //$o = &$modx->documentOutput; 
                $content_tmp = $modx->documentObject['content'];
        
                //echo $content_tmp;
        
                function setCodeblocks( $replacements )
                {
                    return str_replace($replacements[0],$_SESSION['codeblocks'][$replacements[0]],$replacements[0]);
                }
                $content = preg_replace_callback('|#codeblock#-(.*?)-#|si', 'setCodeblocks', $content_tmp);
                //echo $content;
                $modx->documentObject['content'] = $content;
            break;
        
            default :
            return;
            break;
        }

        Both events are checked. if i put the code directly in the plugintextarea, absolutly nothing will happen.
        if i include it by require_once from the filesystem, i will work... that means, the first part will work, the second part is ignored.
        if i splitt it in two plugins and check onParseDocument for the first and onPagePrerender for the second, same result.. onParse work, onPrerender not.

        damn.... I dont await your reply.. just writing down my thoughts...

        thank you
        ciao, Stefan
          • 22303 MODX Staff
          • 10,725 Posts
          If you are trying to alter the content for output, you need to use $modx->documentOutput within the context of the events; do not attempt to alter the values of the $modx->documentObject.
            • 28436
            • 242 Posts
            Hello Jason, thank you for your hint.

            this one work... so far... i hope its not an accident that it do the job.
            <?php
            $e = &$modx->Event;
            $i=1;
            switch ($e->name)
            {
                case 'OnLoadWebDocument':
                    //$o = &$modx->documentOutput; // not present?
                    $o = &$modx->documentObject['content'];
                    function catchCodeblocks( $codeblocks )
                    {
                        global $i;
                        $replacement = str_replace($codeblocks[0],'#codeblock#-'.$i.'-#',$codeblocks[0]);
                        $_SESSION['codeblocks'][$replacement] = $codeblocks[0]; // codeblock[1] is just the value without tags
                        $i++;
                        return $replacement;
                    }
                    $o = preg_replace_callback('|<pre>(.*?)</pre>|si', 'catchCodeblocks', $o );
                break;
                case 'OnWebPagePrerender':
                    $o = &$modx->documentOutput; // get a reference of the output
                    function setCodeblocks( $replacements )
                    {
                        return str_replace($replacements[0],$_SESSION['codeblocks'][$replacements[0]],$replacements[0]);
                    }
                    $o = preg_replace_callback('|#codeblock#-(.*?)-#|si', 'setCodeblocks', $o);
                break;
                default :
                return; // stop here - this is very important.
                break;
            }
            ?>


            My final goal is using the highlight_string function... but on the horizon i can see some trouble with the tinymce.

            Thank you
            Stefan