We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19369
    • 1,098 Posts
    Thank you!

    Btw, if anyone is interested, there is also this interesting blog post (http://www.gregorysmart.com/2011/06/18/revo-template-inheritance-to-grandchildren/) about selecting a template based on tree depth.
      • 36932
      • 39 Posts
      Hello,

      I just fixed that problem with the plugin in 2.2.0. I tested it with 2.2.0-pl2 and 2.1.3-pl and it works with both of them. There was some slight change in the API, that caused it not to work any more. As before, you can finde the corrected version here as a gist with instructions:

      https://gist.github.com/1474540


      By the way, for sure you can do this with form customization, too! I just had a project, where I needed form customization for some other things already, and it would have been a lot of redundant work to realize both things with fc.

      I hope this helps anyone.
        • 19369
        • 1,098 Posts
        Hi, thank you very very much! Now it works fine!

        I'll report back in this thread when I find out how to set up correctly grandchildren templates and grand-grand children templates.
          • 19369
          • 1,098 Posts
          Below is a modified version of "defaultTemplateByParentTv". With this version you can set children and also grand children and grand grand children Template (and so on).
          Little tutorial for this plugin at: http://www.jertix.org/en/blog/modx-revolution/modx-revolution-plugin-changetemplate.html

          <?php
          /**
          * =========================
          * ChangeTemplate
          * =========================
          *
          * Plugin for MODX Revolution
          * Set which template is inherited by children
          * documents of a certain parent document
          *
          * Author:
          * Marc Loehe (boundaryfunctions)
          * marcloehe.de
          *
          * Modified by:
          * Lorenzo Stanco <[email protected]>
          * Lorenzostanco.com
          *
          * Usage:
          *
          * 1. Paste this as new plugin and connect it to system event
          * 'OnDocFormRender'.
          *
          * 2. Assign a new TV 'changeTemplate' to each template
          * for which you want to define the default children template.
          *
          * 3. Set the newly created TV to input type "Text"
          *
          * 4. Open a document and in the 'changeTemplate' TV type a
          * comma separated list of template IDs.
          *
          * 5. Have fun!
          *
          */
           
          // Check Event
          if ($modx->event->name == OnDocFormRender && $mode == modSystemEvent::MODE_NEW) {
             
            // Get current document ID
            if ($id = $_REQUEST['id']) {
           
              // Document Chain
              $resources = array($id);
           
              // Get parent ID
              foreach ($modx->getParentIds($id, 10, array('context' => $_REQUEST['context_key'])) as $parentId) {
                if ($parentId) array_push($resources, $parentId);
              }
               
              // Search changeTemplate in the chain
              $level = 0;
              $childTemplates = array();
              foreach ($resources as $resourceId) {
                $resource = $modx->getObject('modResource', $resourceId);
                if ($childTemplatesTV = $resource->getTVValue('changeTemplate')) {
                   
                  // Create template array for each tree level
                  $childTemplates = @explode(',', $childTemplatesTV);
                  if (empty($childTemplates)) break;
                  foreach ($childTemplates as $k => $v) $childTemplates[$k] = intval(trim($v));
                   
                  break;
           
                }
           
                $level++;
           
              }
           
              // Set template based on tree level
              if (!empty($childTemplates)) {
                $useTemplate = $childTemplates[$level];
                if (!empty($useTemplate)) {
                  
                  // Set default template
                  if (isset($modx->controller)) {
                    $modx->controller->setProperty('template', $useTemplate);
                  } else { // modX < 2.2.0
                    $_REQUEST['template'] = $useTemplate;
                  }
           
                }
              }
           
            }
           
          }
          • Thanks for this useful solution wink
              Freelance Web Developer who likes MODX, PHP, jQuery, ExtJS, SASS and other cool web things. Currenty doing great stuff at COEX CZ.

              GitHub: https://github.com/hansek
              Follow me: https://twitter.com/jantezner
              Or follow us: https://twitter.com/coexcz

              http://www.modx.cz || http://www.coex.cz
              • 36425
              • 49 Posts
              noticed a small mistake

              for example we have:
              - res 1
              ---child 1
              -------child 1

              then i cr8
              - res 1
              ---child 1
              -------child 1
              ---child 2

              it's okey, but if at once cr8
              - res 1
              ---child 1
              -------child 1
              ---child 2
              -------child 1

              plugin not work, its begin working only after clearing cache...

              i solved it by plugin:
              <?php
              $modx->cacheManager->clearCache();

              and set on OnDocFormSave

              but thinking it can be solving anouther way...

              the problem is in: when we cr8 resources there is not active checkbox "clear cache"..
              how to set it to true?

              any ideas? [ed. note: gaserge last edited this post 11 years, 9 months ago.]
                • 25839
                • 23 Posts
                this functionality can be archieved without any plugins - it is already in place in Revolution itself

                go to form customisation, create set of rules for your parent template, and inside, set default template id to child template

                from now, when you use right click on document and select 'create new document here' it will have desired template set

                cascade of templates is also possible this way - you simply need more rules

                note:
                there is a bug (probably, maybe I am missing something)
                just setting template from dropdown does not work always as desired, I had whole array of strange events happeninng

                to omit that, you need 2 'create' rules for each parent template:
                'resource/create' action one with template chosen from dropdown
                'resource/create' action without template (emty template) but constrainst set as follows:
                modResource.template = 11
                where 11 is id of your template

                I have designed product catalogue with that and custom forms for entering products in backend. Works fantastic! [ed. note: rygar last edited this post 11 years, 8 months ago.]
                  • 24414
                  • 45 Posts
                  @rygar— thank you so much, it took a minute or two for that to make sense, but what a simple solution!
                    • 36932
                    • 39 Posts
                    Thanks for your solution rygar, I didn't get this to work with form customization and now I know why. This will save me a lot of hassle with in working with the plugin!
                      • 36724
                      • 13 Posts
                      @rygar: I tried your solution but it is not working for me in 2.2.4.

                      Not when just applying a fc w/ constraint modResource.template=parenttemplate, nor w/ parent template choosen from drop down, nor w/ both.

                      Unfortunately the plugin from #p15 does not work as well (testedw fc deactivated).

                      It looks as if the fc sets do not work or produce just weird strange behavior. Right now I'm trying to find out if it is a caching issue in the manager because what actually happens is that I get the same template on every parent where I create a new resource even w/ different templates on different parents. As if the value for template is chached when fc sets are activated. When all fc sets are deactivated, I get at least the parent template as the choosen template on resource create. [ed. note: chris@crnm last edited this post 11 years, 6 months ago.]