Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Default Children Template by Parent TV - modX Revolution Plugin

defaultTemplateByParentTv

Plugin for modX Revolution

Set default template for children of a ressource

Author: Marc Loehe (boundaryfunctions)

Usage:

  1. Paste this as new plugin and connect it to system event 'OnDocFormRender'.
  2. Assign a new TV 'childDefaultTemplate' to each template, for which you want to define the default children template.
  3. Set the newly created TV to type List Box and its 'Input Option Values' to: @SELECT `templatename` AS `name`,`id` FROM `[[+PREFIX]]site_templates` This will fetch all available templates from the db.
  4. Have fun!

Changelog

2012-01-09
Fixed bug for compatibility with modX 2.2.0
2011-12-14
Initial release
<?php
/**
* =========================
* defaultTemplateByParentTv
* =========================
*
* Plugin for modX Revolution
* Set default template for children of a ressource
*
* Author:
* Marc Loehe (boundaryfunctions)
* marcloehe.de
*
*
* Usage:
*
* 1. Paste this as new plugin and connect it to system event
* 'OnDocFormRender'.
*
* 2. Assign a new TV 'childDefaultTemplate' to each template,
* for which you want to define the default children template.
*
* 3. Set the newly created TV to type List Box and its 'Input Option
* Values' to:
*
* @SELECT `templatename` AS `name`,`id` FROM `[[+PREFIX]]site_templates`
*
* This will fetch all available templates from the db.
*
* 4. Have fun!
*
*/
if ($modx->event->name == OnDocFormRender && $mode == modSystemEvent::MODE_NEW) {
if ($parentId = $_REQUEST['parent']) {
if ($parent = $modx->getObject('modResource', $parentId)) {
if ($childTemplate = $parent->getTVValue('childDefaultTemplate')) {
if (isset($modx->controller)) {
$modx->controller->setProperty('template', $childTemplate);
} else { // modX < 2.2.0
$_REQUEST['template'] = $childTemplate;
}
}
}
}
}

hansek commented Jan 17, 2013

Great, thanks for this ;-)

I would suggest this @select-binding to sort the template names like MODX does:
@SELECTtemplatenameASname,idFROM[[+PREFIX]]site_templatesORDER BYnameASC
Anyhow: Great script! It's in my standard toolbox. :-)

The event name should be placed in single quotes, because of the PHP notice: Use of undefined constant OnDocFormRender - assumed 'OnDocFormRender'.

This fixes it:

if ($modx->event->name == 'OnDocFormRender' && $mode ... 

boformer commented Aug 2, 2015

This is a must-have.

Upload it to the modx extras catalog!

Seems to no longer be working. Just tried it on MODX 2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment