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

    On several projects, I use a plugin I got from here which automatically sets the desired template on a resource depending on its parent.

    Which is
    <?php
    if ($_GET['a'] = 47) {
    $parentID = (int) $_REQUEST['parent'];
     if ($parent = $modx->getObject('modResource', $parentID)){
     
     
     if ($parentID == 4 || $parentID == 3) {
     $produitTpl = 1; 
     $_GET['template'] = $produitTpl ;
     }
    
    
    }
    }


    and works fine.


    Wielding the sword like if I was blind, I tried to... adjust... this plugin and make it change the Resource Type ( [[*class_key]] ), but uhh.. is this about... xPDO, right ? Well, I tried, I fiddled, I went there :

    <?php
    if ($_GET['a'] = 47) {
    $parentID = (int) $_REQUEST['parent'];
     if ($parent = $modx->getObject('modResource', $parentID)){
     
     
     
     if ($parentID == 10 || $parentID == 11) {
     $classKeyGallery = mgResource;
     $_GET['classKey'] = $classKeyGallery ;
     }
    
    
    }


    I checked OnHandleRequest. Only.

    Of course, it doesn't work. Now can you please tell me if it's just a fine-tuning I can expect, or is it just a mess ? Thank you

    This question has been answered by multiple community members. See the first response.

      Sorry for that ⇧ , I'm no dev but I wanna try
      • 15558
      • 108 Posts
      wait, it was obvious for me but it's not for you, reader :

      I want to do this because I installed Mark's MoreGallery - which allows you to change a resource's type to Gallery (so you can upload a whole gallery directly into the resource).

      MoreGallery outputs
      mgResource
      as the [[*class_key]]
        Sorry for that ⇧ , I'm no dev but I wanna try
        • 3749
        • 24,544 Posts
        Would it work to do this in a plugin attached to OnDocFormSave?

        $parentId = $resource->get('parent');
        if ($parentId == 10 || $parentId == 11) {
            $resource->set('class_key', 'mgResource');
            $resource->save();
        }
        return '';
        
        [ed. note: BobRay last edited this post 9 years, 7 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
        • Might want to add a check for "new" or "update" too, just to cut down on unnecessary processing. Of course, that would be presuming that any child resources being updated are already using the correct class_key.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 15558
            • 108 Posts
            Thanks both - actually Bob's suggestion won't work yet :

            Fatal error: Call to a member function get() on a non-object in /.../core/cache/includes/elements/modplugin/10.include.cache.php on line 3

            It would be great if, as on the template-plugin, the modification gets made as soon as the resource is created... is that what you meant Susan ? which System Events are concerned then ?

            (This is about my client ; that way he wouldn't have to click on the Config tab, click on Content Type, get lost, click on resource type, find Gallery, click, wait for reload... then proceed.) But wait, I wish I would not seem demanding !
              Sorry for that ⇧ , I'm no dev but I wanna try
              • 15558
              • 108 Posts
              oops, detail :

              I forgot to uncheck OnHandleRequest in the plugin Bob suggested. Now it's done :

              There's no more fatal error displayed, there's no error at all, but it just doesn't work.
                Sorry for that ⇧ , I'm no dev but I wanna try
              • Do you have OnDocFormSave checked?
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                • discuss.answer
                  Actually, you might be better off using OnBeforeDocFormSave. According to the docs, you can do something like
                  if ($resource->get('parent') == 123) {
                          $resource->set('template') = 4;
                  }

                  So you should be able to use
                  if ($resource->get('parent') == 123) {
                          $resource->set('class_key', 'mgResource');
                  }

                  http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onbeforedocformsave
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                  • For the OnDocFormSave plugin,
                    $parentId = $resource->get('parent');
                    if ($parent == 10 || $parent == 11) {
                        $resource->set('class_key', 'mgResource');
                        $resource->save();
                    }

                    Shouldn't that be
                    $parentId = $resource->get('parent');
                    if ($parentId == 10 || $parentId == 11) {
                        $resource->set('class_key', 'mgResource');
                        $resource->save();
                    }
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 15558
                      • 108 Posts
                      Both of your plugins work, Susan, thank you.

                      The user needs to enter a title at least then hit Save, and that's okay.

                      OnBeforeDocSave is then more convenient : you experience only one page reload after hitting Save, instead of two with OnDocFormSave.
                        Sorry for that ⇧ , I'm no dev but I wanna try