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

    here is a plugin to select from a listbox tv the template to apply to the childrens.

    HOW TO:

    1/ first create a tv (listbox) => named for instance inheritTpl
    2/ in defaut options:
    @SELECT templatename,id FROM [+PREFIX+]site_templates
    3/ attach this tv to the templates of your choice

    4/ create a new plugin named as you wish for instance inheritSelectedTpl
    Copy and paste this code


    /*

    Based on inherit Parent Plugin from Raymond Irving
    Author : heliotrope

    */

    global $content;
    $e = &$modx->Event;

    switch($e->name) {

    case ’OnDocFormPrerender’:
    if(($_REQUEST[’pid’] > 0) && ($id == 0)) {
    $tv = $params[’tvSource’];
    //check published status for parent
    $status = $modx->getPageInfo($_REQUEST[’pid’],0,’published’);
    if($inherit = $modx->getTemplateVar($tv,’*’,$_REQUEST[’pid’],$status[’published’])) {
    $content[’template’] = $inherit[’value’];
    }
    }
    break;

    default:
    return;
    break;
    }

    5/ in the configuration tab add in params field
    &tvSource=tv source;string;
    Update the param display
    Fill in the field with your tv name (in our example "inheritTpl")

    6/ in the event tabs, check OnDocFormPrerender

    7/ Save the plugin


    :-)

    PS: If the inherit parent tpl plugin is running, you have to change the order of execution of the plugins.
    This one should be after inheritParentTpl
      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
      • 7231
      • 4,205 Posts
      This rocks, I have been banging my head trying to do this...it is exactly what I asked Santa for grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 27330
        • 884 Posts
        nice work. can you elaborate some on PS: If the inherit parent tpl plugin is running, you have to change the order of execution of the plugins.
        This one should be after inheritParentTpl


        I click the "Edit Plugin Execution Order by Event" link but not sure which ones I need to sort there ?
          • 11975
          • 2,542 Posts
          Hi,

          here is a screenshot.
          Both InheritParent plugin and mine are using onDocFormPreRender event.
          InheritSelectedTemplate(categoryTpl on my install) must be executed after inheritParent.

          :-)
            Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
            • 27330
            • 884 Posts
            silly me. I clicked the link with only 1 plugin enables so I never saw both listed smiley
            thanks for the fast reply!
              • 5628
              • 44 Posts
              Quote from: heliotrope at Dec 26, 2007, 11:12 AM

              2/ in defaut options:
              @SELECT templatename,id FROM [+PREFIX+]site_templates

              to clarify for thick numptys like me,
              @SELECT templatename,id FROM [+PREFIX+]site_templates
              should be entered into Input Option Values and not Default Value

              Cole
              • Why isn’t this in the repository; it’s a great resource! How’s it differ from http://modxcms.com/automaticTpl-2155.html which I just found in searching for this thread? tongue
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 30223
                  • 1,010 Posts
                  Just bumped into this ...
                  Why isn’t this in the repository; it’s a great resource! How’s it differ from http://modxcms.com/automaticTpl-2155.html which I just found in searching for this thread? Tongue
                  The main difference I can see so quickly is that automaticTpl will crawl up the tree to find a parent which has the TV set instead of only looking at the direct parent. You can also set multiple templates for several levels of parents. So if you create a page two levels down it can inherit a different template than if you create a page 1 level down, while you only set one TV in the topmost parent. The disadvantage is that filling in the TV is more archaic, you need to know the template ids and fill them in manually.

                  I can see one disadvantage with heliotropes TV setting with the @SELECT binding though. For a newly created document the TV will have the value of the first template returned from the @SELECT. This might not be what is expected or what users may want.

                  I’ve changed the TV value to an @EVAL which basically adds an empty item to the select box so that by default the TV has an empty value. There might be a better way to do this but my brain’s to slow...

                  @EVAL $sql = 'SELECT templatename,id FROM '.$modx->db->config['table_prefix'].'site_templates';$v = $modx->db->query($sql);while ($rows = mysql_fetch_row($v)) $a[] = $rows;	array_unshift($a,array('','')); return $a;
                  


                  I’ve also adapted the plugin code so it crawls through the list of parents, selecting the first encountered parent which has a value for the TV in question. Oh and you can add another cofiguration parameter to the plugin to limit the number of levels of parents it checks: &levels=Parent Levels;string;5

                  <?php
                  /*
                  Based on inherit Parent Plugin from Raymond Irving
                  Author : heliotrope
                  Checking multiple levels of parents added by TobyL
                  */
                  
                  global $content;
                  $e = &$modx->Event;
                  
                  switch($e->name) {
                     case 'OnDocFormPrerender':
                       $pid = intval($_REQUEST['pid']);
                       if(($pid>0) && ($id == 0)) {
                         $tv = $params['tvSource'];
                         $levels = $params['levels']?intval($params['levels']):10;
                         $parentIds= $modx->getParentIds($pid,$levels);
                         array_unshift($parentIds,$pid); //add current pid to top of parents
                         foreach($parentIds as $id){
                           //check published status for parent
                           $status = $modx->getPageInfo($id,0,'published');
                           if($inherit = $modx->getTemplateVar($tv,'*',$id,$status['published'])) {
                             $content['template'] = $inherit['value'];
                             break;
                           }
                         }
                       }
                       break;
                  
                     default:
                       return;
                       break;
                  }
                  ?>
                  
                    • 34017
                    • 898 Posts
                    I just realized what this is for. It’s great!

                    Thanks
                      Chuck the Trukk
                      ProWebscape.com :: Nashville-WebDesign.com
                      - - - - - - - -
                      What are TV's? Here's some info below.
                      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                      • 27330
                      • 884 Posts
                      Toby thanks for the updated version. this solved my problem with this kewl plugin.

                      This discussion is closed to further replies. Keep calm and carry on.