Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.