• "Inherit Selected Template" in Revolution?#

  • heatopher Reply #1, 1 year, 1 month ago

    Reply
    Hi, I was looking for a way to make the children of a containing page use a particular default template (ie not use the parent's template), and I found this:

    http://modx.com/extras/package/inheritselectedtemplatefixed

    which looks like what I'm looking for. So, I was wondering whether either:

    1. it's safe to use this plugin in Revolution (and if so, how do I go about doing that?)

    or

    2. there's a similar extension for Revolution, or a way to do this without needing to get an extension.

    Thanks in advance to anyone who can give me a quick pointer here :~}



  • Zaigham (aka zi) Reply #2, 1 year, 1 month ago

    Reply
    It can't be used with Revo unless you modify it to make it Revo compatible.

    I don't think there is something available for Revo like this, but I could be wrong. Lets see if someone chimes in with something.


  • BobRay Reply #3, 1 year, 1 month ago

    Reply
    I just created this plugin for Revo and it's untested. Unlike the Evo plugin, it sets the template when a new resource is saved (based on any ancestor's setting for a TV called ChildTemplate). That will make for faster page loads than setting the template every time the resource is viewed.

    It's not necessary, but if you use an @INHERIT for the TV's default value, it should speed up processing for resources further down in the tree.

    <?php
    //remove this line before pasting
    /**
     * ChildTemplate plugin
     *
     * Sets a new doc's template based on the ChildTemplate TV setting of the nearest
     * ancestor. Set the default value of the ChildTemplate TV to @INHERIT
     *
     * Author: Bob Ray
     *
     * Connect to OnDocFormSave (*not* OnBeforeDocFormSave)
     *
     * looks up the tree for an ancestor that has a ChildTemplate TV setting and,
     * if found, sets it as the template. Uses the nearest ancestor with a setting
     * for that TV.
     *
     * Note: can be used with InheritParentTemplate as long as
     * InheritParentTemplate executes first (set the plugin priority on the System
     * Events tab of the plugin.).
     *
     * The TV setting, (if any) will override the InheritParentTemplate setting.
     * The ChildTemplate TV can contain the name or id of the template, but using
     * the ID will be faster.
     *
     *
     */
    
    
    /* Only act on new documents -- remove this line if you want to be
    * able to change the TV value and have the template updated when
    * you edit and save existing resources */
    if ($mode != modSystemEvent::MODE_NEW) return;
    
    /* Get parent ID */
    $parentId = $resource->get('parent');
    
    /* Get the TV object */
    $tvObj = $modx->getObject('modTemplateVar', array('name' => 'ChildTemplate'));
    
    /* Act only if there is a parent ID (i.e., doc is not at the root) and the TV object exists */
    
    if (tvObj) {
     
        /* Get value of TV for parent */
            if ($parentId) {
    
                /* Get the TV's value for that ancestor */
                $tv = $tvObj->getValue($parentId);
    
                if ($tv) {
                    /* $tv is set -- use it for current resource's template value */
                    if (is_numeric($tv)) {
                        /* It's a number -- user it */
                        $resource->set('template', $tv);
                    } else {
                        /* It's a template name -- get the ID */
                        $t = $modx->getObject('modTemplate', array('templateName' => $tv));
                        if ($t) {
                            $resource->set('template', $t->get('id'));
                        }
                    }
                    $resource->save();
                 }
    
            }
    }
    
    


  • featherodd Reply #4, 1 year ago

    Reply
    BobRay: just tried to use this and MODx now hangs on saving a document resource. Nothing in the error logs. MODx 2.0.8-pl.

    Does the ChildTemplate TV have to be assigned to all templates? I only assigned the TV to the parent and child templates for the container and sub-pages where I tested it.


  • rthrash Reply #5, 1 year ago

    Reply
    Honestly the behavior of fresh installs of Evo 1.0.5 is ideal: it inherits the same template as any sibling docs it finds at any level. You'd need to craft a plugin to do the other bits.


  • featherodd Reply #6, 1 year ago

    Reply
    rthrash: but revolution seems to inherit the parent template when creating a new resource, or use the default template if creating off a root context...


  • BobRay Reply #7, 1 year ago

    Reply
    I made a change to the code. See if it works now (be sure not to paste the <?php line).


  • featherodd Reply #8, 1 year ago

    Reply
    no longer freezes on saving, but doesn't seem to work.

    got a TV called ChildTemplate attached to two templates with ID's 12 & 14.
    saved the value "14" to a container using template (12).
    on creating a new child page under said container the parent template (12) is still selected.
    on saving the page, parent template (12) still selected.

    also tried using template name as ChildTemplate TV value. same result.


  • BobRay Reply #9, 1 year ago

    Reply
    What system event did you connect the plugin to?


  • featherodd Reply #10, 1 year ago

    Reply
    just "OnDocFormSave" (group = resources, property set = 0, priority = 0)