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

    I have a plugin that needs to be executed every time a resource gets duplicated, so I subscribed it to the OnResourceDuplicate event. If I duplicate a context, all of the source context's resources including their children get duplicated, but the OnResourceDuplicate event isn't fired.

    core/model/modx/processors/context/duplicate.class.php calls core/model/modx/modresource.class.php's duplicate-function (line 927), that doesn't seem to invoke the event. core/model/modx/processors/resource/duplicate.class.php does invoke the event however. I'm a bit lost, because I don't understand, how this works all together...

    What can I do to invoke OnResourceDuplicate for every affected resource when duplicating a context?

    Thanks in advance for any help!
      • 3749
      • 24,544 Posts
      Looking at the code, you are correct that the modResource class's duplicate() method doesn't invoke that event.

      It would be a good feature request to make sure that event is invoked in this process. (I think it has already been submitted -- maybe by me). wink

      As a workaround, you'd have to put your own invokeEvent() code in the method. The method is recursive, so it would only have to go in one place. I'd put it here:

       if (!$newResource->save()) {
           return $this->xpdo->lexicon('resource_err_duplicate');
       } else {
          $this->xpdo->invokeEvent('OnResourceDuplicate', array(
                'newResource' => &$newResource);
          ));
       }



      The new resource object would be available in your plugin as $newResource and you could get any of its fields with $newResource->get('fieldname'), you may or may not be able to get the id this way.



      [ed. note: BobRay last edited this post 12 years, 2 months ago.]
        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