Skip to content

Instantly share code, notes, and snippets.

@jrotering
Last active December 17, 2015 03:59
Show Gist options
  • Save jrotering/5547703 to your computer and use it in GitHub Desktop.
Save jrotering/5547703 to your computer and use it in GitHub Desktop.
Fires on 'OnHandleRequest' event. Sets the template of new resources based on a property in the template of the parent resource.
<?php
if (isset($_GET['a'])) {
$action = $modx->getObject('modAction', $_GET['a']);
if (is_object($action) && $action->get('controller') == 'resource/create') {
$parentID = isset($_REQUEST['parent']) ? (int) $_REQUEST['parent'] : 0;
if ($parent = $modx->getObject('modResource', $parentID)) {
$parentTpl = $parent->get('template');
if ($parentTplObj = $modx->getObject('modTemplate', $parentTpl)) {
if ($props = $parentTplObj->getProperties()) {
$tpl = ($ff = $modx->fromJSON($props['childTemplate'])) ? $ff : null;
if ($tpl) {
$_GET['template'] = $tpl;
}
}
}
}
}
}
@sonicpunk
Copy link

I love this one! I use it on almost every project!

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