We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17667
    • 108 Posts
    I need to have a template that automatically sets its parent ID to a particular resource (MODX Revolution). I have figured out how to do this with a plugin I created that runs a script with the OnDocFormSave system event:
    <?php
    // only run this code for the template with the ID of 6
    if ($resource->get('template') != 6) {
    	return;
    }
    
    // set parent resource
    $parent_resource_id = 81;
    $my_parent = $modx->getObject('modResource',$parent_resource_id);
    $my_parent_title = $my_parent->get('pagetitle');
    $resource->set('parent-cmb', $my_parent_title . ' (' . $parent_resource_id . ')');
    


    The code works but the issue is I have a user who is limited by permissions from creating a resource at the root level. This limitation takes precedence before the plugin runs so they get an error if they do not manually set the Parent Resource (and setting the Parent Resource is a step many clients seem to have trouble remembering).

    I know there are system events for when the template is selected, ideally I could figure out a way to set the parent resource at this stage instead of when it is saved. Hoping someone can point me in the right direction.
      • 3749
      • 24,544 Posts
      Is this upgrading an existing doc, or creating a new one. If it’s upgrading, you should be able to do it in OnDocFormPrerender. I’m not sure if that would work for new docs or not.
        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
        • 17667
        • 108 Posts
        This is for a new document. I tried the OnDocFormPrerender system event but don’t know how to make it template specific. I started with the code below and it kills Manager when I go to create a new document:
        if ($resource->get('template') != 6) {
        	return;
        }

        I assume a new document is not capable of generating a resource object so this creates an error.
          • 3749
          • 24,544 Posts
          Right, the $resource object is not available there for new objects. I think the $id is, however, so you might be able to use this first:

          $resource = $modx->getObject('modResource', $id);


          Test $resource to make sure it’s not null before calling $resource->get() or $resource->set().
            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
            • 3749
            • 24,544 Posts
            Hmm. I just looked at the create processor code. The parent is set after OnDocFormPrerender is invoked and the code ignores the resource fields, so the method above won’t work.

            However, the parent ID is taken from $_REQUEST[’parent’], so you might be able to set that in your plugin attached to OnDocFormPrerender. I’m not sure you can count on this working in future versions because the $_REQUEST variables tend to be going away. In the future, it might be in $scriptProperties[’parent’].
              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
              • 17667
              • 108 Posts
              Thanks for the help. Since there isn’t a clean solution for doing this I probably need to work on reminding the client to set the Parent Resource themselves for now.
                • 3749
                • 24,544 Posts
                Quote from: lukemcd at Apr 20, 2011, 03:50 PM

                Thanks for the help. Since there isn’t a clean solution for doing this I probably need to work on reminding the client to set the Parent Resource themselves for now.

                I’d give the $_REQUEST method a try. It should work now, and if it ever stops working it should be easy to fix.
                  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
                  • 17667
                  • 108 Posts
                  Tested it and setting $_REQUEST[’parent’] with the OnDocFormPrerender system event does work. But how can I set the code to only implement when a certain template is selected?
                    • 17667
                    • 108 Posts
                    I found the OnResourceTVFormPrerender system event which fires both when you open a resource (or select New Resource) and when you change the template of a resource. Ideally this would be where the code would check the selected template and update the parent id accordingly. But it doesn’t look like the Parent Resource field re-renders on this event.
                      • 3749
                      • 24,544 Posts
                      Quote from: lukemcd at Apr 20, 2011, 09:34 PM

                      Tested it and setting $_REQUEST[’parent’] with the OnDocFormPrerender system event does work. But how can I set the code to only implement when a certain template is selected?

                      You could get the parent object and check its template if that would help. Otherwise, I’m not sure it’s possible because I think the template is set after the return from OnDocFormPrerender. You might check $_REQUEST[’template’] to see if there’s anything there.

                      You might also be able to step in again in OnDocFormRender. I think the template has been set at that point, and I think the form still hasn’t been displayed.
                        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