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

    I know this topic has come up before, but I'm struggling to make sense of some of the (now quite old) threads that cover it.

    What I'd like to know is how to set up a restriction that forces Modx to set the template of a newly created document, based on the template that its parent uses. There are only two areas of the site that I would like this to be in place, so flexibility isn't an immediate concern; but if there's a programmatic way of doing it, all the better.

    Chris
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        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
        • 5143
        • 116 Posts
        Thanks for the link - it seems close but I'm not sure it's exactly what I need.

        Say I have a "Level One" document - I can easily set all children to have "Level Two" templates from this one folder, even as the client adds more level two documents.
        But is there a way to make all children of "Level Two" documents have "Level Three" templates? It seems as though this requires that the client manually sets this, each time they create a level two parent document.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I seem to remember a plugin for assigning the same template as a sibling, which would help as then only the first child would be manually set, but I can't find it now.

          I usually solve this problem by simply duplicating a resource whose settings I want duplicated instead of creating a new one.
            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
            • 3749
            • 24,544 Posts
            I think this plugin might do it (attached to OnBeforeDocFormSave):


            if ($mode === modSystemEvent::MODE_UPD) return '';
            
            $parent = $resource->get('parent');
            
            $sibling = $modx->getObject('modResource', array('parent' => $parent));
            
            if ($sibling) {
                $resource->set('template', $sibling->get('template'));
            }



              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
              • 5143
              • 116 Posts
              I usually solve this problem by simply duplicating a resource whose settings I want duplicated instead of creating a new one.
              If it were just me, then I'd probably do the same but there will be a few too many fields that may need altering with each duplication. I like to try and make my manager pages as fool-proof as possible for the client.

              I decided to go down the plugin route - seemed like the only viable option - but I'm having issues. The intention of the following should be fairly self-explanatory, but it's not working. Not quite sure what I'm doing wrong...

              switch ($modx->event->name) {
              
              	case 'OnBeforeDocFormRender':
              
              		// Creating a new document
              		if ($mode == modSystemEvent::MODE_NEW) {
              
              			// Get parent resource id
              			$parentid = $modx->resource->get('parent');
              
              			// Get parent resource object
              			$parent = $modx->getObject('modResource',array(
              				'id' => $parentid
              			));
              
              			// Get parent resource template id
              			$tpl = $parent->get('template');
              
              			// If it's a descendant of the projects folder, make it a project
              			if ($parentid == 2) {
              				$resource->set('template','2');
              			};
              			// If it's a descendant of a project, make it an event
              			if ($tpl == 2) {
              				$resource->set('template','3');
              			};
              		};
              
              	break;
              };
              
              return;


              Any clues?

              EDIT: I'm aware there's only one case - I'm trying to get it working bit-by-bit.
              Also, I'm attempting to set the template when the new document form is created. But I can't find much info about this system event - is it even possible? [ed. note: chr.s last edited this post 12 years, 5 months ago.]
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                It should be possible, you would need to know the field name and the ID of the template you want. There must be something available in the way of a $resource object even at this point, since the form is generated with the parent and the default template, as well as the published and other settings.

                According to manager/controllers/default/resource/resource.class.php fields are set using $this->object->set(), but I can't figure out what "$this" should be in a plugin.
                $this->resource->set('parent',$parentId);
                  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
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Hmm. It looks like these things are actually set in /manager/controllers/default/resource/create.class.php. It does something weird with a "parent" object in order to set the new resource's template to be that of its parent. And it looks like Smarty templates are intimately involved.
                    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
                    • 5143
                    • 116 Posts
                    It should be possible, you would need to know the field name and the ID of the template you want.
                    Yeah, that's what I thought. I know the ID - in this instance it won't be changing either, so it's fine to hard-code it. Not entirely sure what you're referring to by the field name though... the actual name of the template?

                    There must be something available in the way of a $resource object even at this point, since the form is generated with the parent and the default template, as well as the published and other settings.

                    According to manager/controllers/default/resource/resource.class.php fields are set using $this->object->set(), but I can't figure out what "$this" should be in a plugin.
                    This has been bugging me too - there's not a lot of info or examples relating to it. Referencing "$this" just throws an error.

                    I've looked into ext.js too, to try and set the values that way. I can read them just fine - but can't set them for some reason.

                    This is turning into quite a headache.
                      • 5143
                      • 116 Posts
                      Turns out this is ALL possible with form customisation settings.

                      If anyone has a similar issue, I'll try and write a set of instructions on how to set the template of newly created documents, based on the template or id of the parent.