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();
}
}
}
[ed. note: BobRay last edited this post 13 years, 5 months ago.]