We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    Hi all,

    I'm working on an add-on that needs to fire right before a web page is rendered and loaded on the front end. However, I also need access to $resource, as I'm going to need to get a TV value. What system event should I use? I've tried OnWebPageInit and OnWebPagePrerender, but neither seem to have access to $resource. Any suggestions?
      • 813 ☆ A M B ☆
      • 326 Posts
      I've been trying this, but not only does it not write to the error logs, it's causing a 500 error on page load:

      <?php
      if ($modx->event->name == 'OnWebPagePrerender') {
        $pw = $resource->getTVValue('password');
        return $pw;
      }
        • 813 ☆ A M B ☆
        • 326 Posts
        Okay, that definitely doesn't work. Some advice on Twitter said to do this:

        <?php
        if ($modx->event->name == 'OnParseDocument') {
          $pt = $modx->resource->get('pagetitle');
          return $pt;
        }


        That's obviously not a TV, but I thought I'd try it just to get it working. That plugin caused a 500 error on both the front end and the manager, and I ended up having to manually delete my cache files. Am I missing a syntax error in there or something?
          • 3749
          • 24,544 Posts
          You don't need to check the event name unless you're handling more than one event.

          Plugins don't write to the screen when you return a value (in fact, you generally shouldn't have a return statement).

          Try this:

          $pt = $modx->resource->get('pagetitle');
          $modx->log(xPDO::LOG_LEVEL_ERROR, $pt);
          


          The pagetitle should appear in the MODX error log (under the Reports menu).


          Here's another method I like to use for debugging plugins. Create a chunk called 'debug':


          function my_log($msg) {
              $c = $modx->getChunk('debug');
              $c->setContent($msg);
              $c->save();
          }
          
          $pt = $modx->resource->get('pagetitle');
          my_log('PAGETITLE: ' . $pt);




          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 813 ☆ A M B ☆
            • 326 Posts
            Man, either I'm overlooking something really obvious, or my MODx install is all kinds of screwed up. If I paste that in exactly and set it to OnParseDocument, I immediately get 500 errors in the manager and front end. So weird. I'll keep trying, though, and let you know what I figure out. If you have any other ideas of what could be causing this, please let me know.

            Brilliant idea on the chunk debugging! I'm going to start using that.
              • 813 ☆ A M B ☆
              • 326 Posts
              Correction: not a 500 error. When I try to access the manager, I'm getting"Fatal error: Call to a member function get() on a non-object in /path/to/site/public_html/core/cache/includes/elements/modplugin/7.include.cache.php on line 7". The front end continues to work fine, but I can't log into the back end at all. The only way I've gotten in is to delete the plugin code directly from the modx_site_plugins table in the DB and manually delete everything in my core/cache directory.

              Does that help at all?
                • 813 ☆ A M B ☆
                • 326 Posts
                Update! Fingers crossed -- and I don't know why this wasn't working earlier (caching issue maybe?) -- but I've got it working using Bob's last example code and OnWebPagePrerender. Thanks for the help!
                  • 3749
                  • 24,544 Posts
                  The non-object error indicates that the object is not available there. Looking at the code, I think I can see why. In OnParseDocument, the only available variables are $content and $documentOutput (deprecated and probably gone soon).

                  So for OnParseDocument, there may be no way to get the $resource itself (so no TV values) unless $modx->resource is set. It should be, but I'm not sure -- your experience suggests otherwise, but any typo in a plugin will cause all kinds of hard-to-debug chaos.

                  In OnWebPagePrerender, no variables are set in the invokeEvent() call, but $modx->resource is definitely available (as you're seeing).



                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting