We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11927
    • 105 Posts
    I have a plugin that works in Evo. I’m trying to transfer it over to Revo.

    Would anyone know the system event equivalents?

    OnBeforeDocDuplicate = ?
    OnDocDuplicate = OnResourceDuplicate (is this the correct equivalent?)
    OnDocFormSave = OnDocFormSave (but is it going to change to something like OnResourceFormSave ?)
      You may or may not want to use the code I write. It's probably all against the syntax rules of php and MODx. smiley

      Carpet Cleaning
      • 11927
      • 105 Posts
      Or is there a different way now to tell if the Resource that is being saved is being duplicated?

      I’ll look into the core code some more to see if I can find where the pop-up window comes from when you duplicate a Resource.

      (I’m not trying to be a forum hog, I just have these last two questions that I don’t know.)
        You may or may not want to use the code I write. It's probably all against the syntax rules of php and MODx. smiley

        Carpet Cleaning
        • 3749
        • 24,544 Posts
        OnResourceDuplicate fires in Revo just after a resource has been duplicated. I don’t think there is an OnBefore event for this.

        There are only two available variables in OnResourceDuplicate, AFAIK:

        $oldResource and $newResource. They are references to the resource objects themselves.

        OnDocFormSave is still good in MODX 2.1.x.
          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
          • 11927
          • 105 Posts
          BobRay,
          Thanks. I have been trying to see what goes on in the processing for when a Resource is duplicated. The only System Event that I can’t find a Revo equivalent for is OnBeforeResourceDuplicate.

          Now I have to rethink how to change the plugin for what I need. I’ll do some thinking about how to do it before I figure out if I need to ask for more help. If I figure it out, I come back with an update.

          Sidenote:
          This is the plugin I’m trying to modify for Revo.
          http://modxcms.com/forums/index.php/topic,58264.msg335226.html#msg335226
            You may or may not want to use the code I write. It's probably all against the syntax rules of php and MODx. smiley

            Carpet Cleaning
            • 3749
            • 24,544 Posts
            I’m not totally clear on what you’re doing and I’m especially unclear on why, but hopefully this will help some.

            There is no OnBefore event for duplication, but I’m not sure you need it. You could certainly make that a feature request, but I don’t see the purpose since I don’t think there’s anything you could do there that you can’t do in OnResourceDuplicate. Maybe I’m missing something.

            I think you may have to give up the idea of only doing it the first time (which I’m not sure I understand the purpose of, since new children may be created for every duplication). You can easily tell when a resource is being saved for the first time, but it would be impossible to know whether a resource is being *duplicated* for the first time, IMO, without storing and checking all the IDs of previously duplicated resources, which kind of defeats the purpose. I guess you could create a boolean TV called "duplicated", but I think it would cost you more time than it saves.

            In converting your plugin to Revo, the first step would be to make sure that OnResourceDuplicate is actually firing. It’s in the duplicate processor and not all processors are "active" (actually used) in the current version. MODX may or may not be calling that processor when duplicating a resource with the right-click menu, for example. If not, I think you’re SOL until the processor becomes "active."

            FYI, there is no alert() in Revo, so you’ll have to write to the error log to see what’s happening using $modx->log().

            Bear in mind that TV values are actually independent of resources in the DB-- IOW, there’s no need to retrieve, change, or save a resource in order to set one of its TVs.

            What you’re doing should be much simpler with xPDO.

            At the point of OnResourceDuplicate, you have reference variables pointing to both the old and new resource and neither the new resource nor the children have been saved yet, but the new children do exist.

            You can use $newResource->getChildIds() and just loop through that array to set the TV values:

            $tv = $modx->getObject('modTemplateVar', $id); // $id = ID of the TV
            
            $childIds = $newResource->getChildIds();
            
            foreach($childIds as $childId) {
               $tv->setValue($childId, 'TEMP');
            }
            

            You’ll have $oldResource->hasTempalateVar() to see if the parent has the TV.

            As for modifying the parent ($oldResource), I think you can just get the TV and make your changes to it using $tv->setValue() as above.

            Hope this helps.

              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
              • 11927
              • 105 Posts
              BobRay,
              Thanks again. I finally got it working (and it’s working better than it did in EVO).


              http://modxcms.com/forums/index.php/topic,58264.msg361797.html#msg361797


              I’m not totally clear on what you’re doing and I’m especially unclear on why, but hopefully this will help some.
              You helped tremendously and that’s funny.


              This plugin will allow you to duplicate a document that has children, then when you save the newly created document, it will perpetuate the value of the specified TV to all the children.

              So if you duplicate a resource, it has children, they all have the TV ’your_tv_name_here’, when you edit and then save the duplicated parent resource for the first time, the value you give the TV in the parent resource will copy down to all the children.

              You can limit the plugin to run only when you copy resources that are in a certain folder.

              The reason I did all of this is because I have a folder that has a lot of resources. Some of these resources have lots of children and all the children of the parent resource need to have the same value in the TV ’my_tv’ as the parent resource does. Below is an example. The value of ’my_tv’ is in quotes.

              Folder
              - doc1 my_tv = ’ab’

              - doc2 my_tv = ’xy’
              --- child1 my_tv = ’xy’
              --- child2 my_tv = ’xy’

              - doc3 my_tv = ’jj’
              --- child1 my_tv = ’jj’
              ------ grandchild1 my_tv = ’jj’
              --- child2 my_tv = ’jj’

              Sometimes I needed to copy a resource, like ’doc3’, but change the value of the TV to a different value. Instead of going into each resource one at a time, I created the plugin (with a lot of help from BobRay, OpenGeek, and others) to update the TV value to be the same as the parent when I save the parent resource for the first time.


              Side notes:
              OnResourceDuplicate does fire (2.0.8 ).

              I couldn’t figure out how to output to the error log.
              <?php
              $my_error = 'This is my error message!';
              $modx->log(modX::LOG_LEVEL_DEBUG, $my_error);


              I also tried both of these but neither worked in OnResourceDuplicate or OnDocFormSave. I even tried hard coding the number instead, but all I got was an empty array. It may have something to do with what BobRay said here: http://modxcms.com/forums/index.php/topic,60096.msg341822.html#msg341822 (but I also couldn’t get either method to work when saving an existing resource)
              <?php
              $ids_array = $modx->getChildIds(123); // even tried in OnResourceDuplicate: $ids_array = $newResource->getChildIds();
              $ids_array = $modx->getParentIds(123);



              All I can say is wow, modx is amazing, but sometimes very hard to understand for a designer turned self-taught programmer (more like taught by everyone on the forums). I also want to say thank you.
                You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

                Carpet Cleaning
                • 3749
                • 24,544 Posts
                Glad to help, but is there a reason that just setting the TV’s default value to @INHERIT wouldn’t do the job? That will automatically give it the same value as the nearest ancestor with a value set unless you override that by entering a value in the child.
                  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
                  • 11927
                  • 105 Posts
                  Because I don’t know what I’m doing. Wow, that could have saved some time.

                  But at least now it makes the TV for my keywords and the resource description blank when I make a duplicate. That way I have to make a different description for SEO purposes. I also learned a lot more about modx.

                  (I updated the plugin code.)
                    You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

                    Carpet Cleaning
                    • 3749
                    • 24,544 Posts
                    Quote from: heavensbest at Apr 29, 2011, 01:24 PM

                    Because I don’t know what I’m doing. Wow, that could have saved some time.

                    Sure, but doing the plugin was a lot more fun, right? wink
                      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