MODX Revolution Plugin: ChangeTemplate

Post by Salvatore Tedde, 16/01/2012, 6. Category: MODX Revolution

ChangeTemplate is a Plugin for MODX Revolution (2.2.0-pl2). This Plugin is particularly useful when we need to control which Template is inherited by children documents of a certain parent document.

With ChangeTemplate the user doesn't need to remember to select any Template because that's done automatically by the Plugin, thus eliminating/reducing the risk of breaking the website.

ChangeTemplate is also on GitHub, click here to open the repository.

1. Installation

To install ChangeTemplate Plugin go to  "Elements > Plugins > New Plugin" and write as follows:
Plugin Name: ChangeTemplate
System Events: OnDocFormRender
Plugin code (php):

<?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 <lorenzo.stanco@gmail.com>
* 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;
        }

      }
    }

  }

}

Now create this Template Variable:
TV Name: changeTemplate
TV Input Options: Text
TV Template Access: here select in which Template you wish to use this Plugin

2. Usage

This Plugin is very simple to use. Let's suppose that we have Template 1 (1), Template 2 (4), Template 3 (5), Template 4 (6), and Template 5 (7), as in the following screenshot:

The first step is to go to the "Resources" Tab, open the "Home" document (or any other document) and set the value for the "changeTemplate" Template Variable to "4,5,6,7". These numbers are the IDs of our Templates. Here's a screenshot of my "changeTemplate" TV:

Once that the "Home" document is saved, we can create nested documents of "Home", and the Plugin "ChangeTemplate" will take care of selecting the requested Template for each tree level, so:
Tree Level 2 => Template 2 (ID 4)
Tree Level 3 => Template 3 (ID 5)
Tree Level 4 => Template 4 (ID 6)
Tree Level 5 => Template 5 (ID 7)

That's all! You can find further details about this plugin on this forum thread.

Write a comment

#
on 17/01/2012, at 12:24:43
Lorenzo, thanks for that nice addition to the plugin!
Would you like to make a project at github out of this? That way, anyone might contribute. I think it would be nice to make it available through modX packet-managment.
#
on 17/01/2012, at 20:12:57
@boundaryfunctions, feel free to use this code on github. I would do it myself but I am not familiar with it.
#
on 25/02/2012, at 09:00:11
This is a nice plugin! Although I think I've found a couple of bugs.

If the user selects the "add another" option when saving the plugin doesn't select the correct child template. Another bug seems to trigger an infinite save loop when a child template is chosen below another template which is already using ChangeTemplate.

For example, if the top resource has ChangeTemplate set to 2, 3, 4, and then on the next level down I set the ChangeTemplate to 5 you get an infinite save loop.
#
on 25/02/2012, at 09:02:01
and I also agree this plugin should be on Git Hub... we can at least then keep track of bugs :)
#
on 25/02/2012, at 09:39:35
Thanks for the feedback and for reporting the bugs, usually I create pages from the tree so I didn't notice that. Anyway, I am opening right now a GitHub Repository for this plugin.
#

Benjamin Morrison

on 21/09/2012, at 20:10:59
THANKS! Very needed!