We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15861
    • 24 Posts
    I’m writing a simple plugin that checks for the existence of a cookie named ’flashme’ and creates a placeholder with that cookie’s contents, if it exists. Basically, I’m wanting to make those flash/session messages we’ve all seen (success/failure messages that fade away shortly after page load), and I’m trying to automate things a little bit. It seems to be working except that it never replaces the placeholder with the cookie. At least, it never shows up on the screen. I’ve verified it’s getting the document data and the flash message. When I view page source, the placeholder no longer exists in the source. Am I doing this at the proper event? Is there a built-in way that I’ve missed?

    Here’s the plugin source:

    // Check the event type
    $e = &$modx->Event;
    
    switch ($e->name) {
        case "OnWebPagePrerender":    
            // Has a cookie been set?
            if (isset($_COOKIE['flashme'])) {
                // A cookie exists, so replace the [+flashme+] placeholder
    
                // Get a reference to the document object.
                $o = &$modx->documentOutput;
                $msg = $_COOKIE["flashme"];
                $o = str_replace("[+flashme+]", $msg, $o);
                
                // Erase the cookie
                setcookie("flashme", "", time()-3600);
            }
        
            break;
        default: 
            return;
        break;
    }
    


    Any help or ideas would be greatly appreciated!
    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The parser removes all placeholders that have not been used at the end of a pass. This is to prevent any placeholders that were used in the document’s content but not actually processed by anything from showing up on the final page. Use an event that occurs earlier in the parsing process. That one is called just before the document is sent on to the server.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 15861
        • 24 Posts
        Susan,

        Thanks for the response. I’m actually in the process of extending your RandomQuotes snippet as we speak.

        I just tried both the OnLoadWebDocument and OnParseDocument events. I’m still getting nothing. I’ve verified that the cookie exists in the browser, but no output to the page, even after clearing the cache.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          How about if you actually loaded the placeholder into the main placeholder array with
          if (isset($_COOKIE['flashme'])) {
              $modx->setPlaceholder('flashme', $_COOKIE['flashme']) 
          }
          return '';
          

          in a snippet placed near the top of your template, instead of using a plugin?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org