Skip to content

Instantly share code, notes, and snippets.

@bfncs
Created December 13, 2011 23:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bfncs/1474540 to your computer and use it in GitHub Desktop.
Save bfncs/1474540 to your computer and use it in GitHub Desktop.
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
Copy link

hansek commented Jan 17, 2013

Great, thanks for this ;-)

@mindeffects
Copy link

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

@mindeffects
Copy link

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
Copy link

boformer commented Aug 2, 2015

This is a must-have.

Upload it to the modx extras catalog!

@sonicpunk
Copy link

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