Skip to content

Instantly share code, notes, and snippets.

@l1a7t
Created September 27, 2016 09:28
Show Gist options
  • Save l1a7t/623df19b7a6d86dd389fd42b3d680301 to your computer and use it in GitHub Desktop.
Save l1a7t/623df19b7a6d86dd389fd42b3d680301 to your computer and use it in GitHub Desktop.
<?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