We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22197
    • 40 Posts
    Hi

    MODX 2.6.5
    Image+ 2.8.0

    If image+ is used in a snippet (runsnippet), it works as expected

    $image = $modx->runsnippet('imageplus', array(
        'tvname' => 'tv',
        'docid' => $resourceID
    )); 
    


    The same code used in a plugin (for an OnBeforeDocFormSave system event) turns saving a resource into a loop.

    Can anyone explain this?

    Do I have to specify the output in the plugin? How?

    Ludo
      • 22197
      • 40 Posts
      I've done some further debugging, but haven't found a solution so far.

      I included the working snippet into the plugin with the code:

      $resourceID = $resource->id ;
      $modx->runsnippet('workingsnippet', array('x' => $resourceID)); 
      


      It still won't work of course, but the nginx-log reveals what's going on:

      2018/12/12 14:52:57 [error] 18924#18924: *16402 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function get() on null in /var/www/modx/cache/configiedereenbenieuwd/includes/elements/modsnippet/25.include.cache.php:29
      


      Snippet 25 is none other than 'ImagePlus' and line 29 in that snippet is

      $docid = $modx->getOption('docid', $scriptProperties, $modx->resource->get('id'), true);
      


      Apparently, modx fails while executing '$modx->resource->get('id')', the default value for '$docid'.

      But why is it taking the default value if '$docid' is not empty???

      Why is it failing when executed in the plugin while it is working ok when the snippet is fired from within a resource?

      Quote from: omegerard at Dec 12, 2018, 06:50 AM
      Hi

      MODX 2.6.5
      Image+ 2.8.0

      If image+ is used in a snippet (runsnippet), it works as expected

      $image = $modx->runsnippet('imageplus', array(
          'tvname' => 'tv',
          'docid' => $resourceID
      )); 
      


      The same code used in a plugin (for an OnBeforeDocFormSave system event) turns saving a resource into a loop.

      Can anyone explain this?

      Do I have to specify the output in the plugin? How?

      Ludo
        • 3749
        • 24,544 Posts
        You don't want $modx->resource in a plugin attached to that event. $modx->resource only makes sense in the front end and usually events that involve a resource set $resource and $id for you.

        You may be able to use either of these:

        $resource->get('id')
        $id
        


        See: https://bobsguides.com/modx-revolution-events.html#OnBeforeDocFormSave

        Keep in mind that the plugin will also fire when you save new resources, which have no ID yet at this point, but if you change the event to OnDocFormSave, I think they do -- it's often a better choice, even if it's not quite as fast.

        If you use OnBeforeDocFormSave, you'll need to test if the resource is new (you may want to check that anyway.

        if ($modx == modSystemEvent::MODE_NEW) {
           // It's new
        }


        We could probably give you better advice if we knew why you want to get the image in the plugin.

        Finally, it's really important in snippets and plugins (especially plugins) to have sanity checks before you call any object methods like get().

        if ($resource) {
           $docId = $resource->get('id');
        } else {
           $modx->log(modX::LOG_LEVEL_ERROR, '[MyPlugin] no resource');
        }


          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
          • 22197
          • 40 Posts
          Hi Bob

          Thank you very much for your sound advice.

          Good question: why I need the image in the plugin.

          I want Modx to create an image (jpg) for the Open Graph meta tag og:image. The image is basically text on a coloured background, but for one template the image shows a picture of a person next to the title. (the person that the article is about)

          I tried to solve the problem with a resource that takes the id of the article as a parameter, but Facebook's caching system appears to ignore the parameter.

          Hence the OnBeforeDocFormSave solution: ModX creates the image each time the resource is modified (not each time the resource is read in the browser). If you see a better solution, please share.

          To make things clear, '$modx->resource->get(id')' is part of the snippet 'ImagePlus'. It generates an error message because it is defined as the default value for the variable '$docid'. That default value should not play a role, as it gets it value via the scriptproperties (at least in my logic).

          Regards

          Ludo



          Quote from: BobRay at Dec 12, 2018, 08:32 PM
          You don't want $modx->resource in a plugin attached to that event. $modx->resource only makes sense in the front end and usually events that involve a resource set $resource and $id for you.

          You may be able to use either of these:

          $resource->get('id')
          $id
          


          See: https://bobsguides.com/modx-revolution-events.html#OnBeforeDocFormSave

          Keep in mind that the plugin will also fire when you save new resources, which have no ID yet at this point, but if you change the event to OnDocFormSave, I think they do -- it's often a better choice, even if it's not quite as fast.

          If you use OnBeforeDocFormSave, you'll need to test if the resource is new (you may want to check that anyway.

          if ($modx == modSystemEvent::MODE_NEW) {
             // It's new
          }


          We could probably give you better advice if we knew why you want to get the image in the plugin.

          Finally, it's really important in snippets and plugins (especially plugins) to have sanity checks before you call any object methods like get().

          if ($resource) {
             $docId = $resource->get('id');
          } else {
             $modx->log(modX::LOG_LEVEL_ERROR, '[MyPlugin] no resource');
          }


            • 13428 ☆ A M B ☆
            • 1,031 Posts
            Filling that opengraph tag with a imageplus snippet call in the template is not an option?

            The way the snippet gets the default value is not suitable to run it in a plugin, since it executes $modx->resource->get(‘id’) every time.
              • 22197
              • 40 Posts
              Quote from: Jako at Dec 13, 2018, 12:20 PM
              Filling that opengraph tag with a imageplus snippet call in the template is not an option?

              Why haven't I thought of that option before?! It looks so obvious! Thank you very much for the suggestion.


              Quote from: Jako at Dec 13, 2018, 12:20 PM
              The way the snippet gets the default value is not suitable to run it in a plugin, since it executes $modx->resource->get(‘id’) every time.

              That's what I don't understand. Why is it executing that command even if the variable $docid gets a value from scriptproperties?

                • 13428 ☆ A M B ☆
                • 1,031 Posts
                The full line is executed. That means the $modx->resource->get('id') part too.