Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/789ae95c042e63a4d3a8 to your computer and use it in GitHub Desktop.
Save kinglozzer/789ae95c042e63a4d3a8 to your computer and use it in GitHub Desktop.
SilverStripe Page::$disallowed_children
CMSMain:
extensions:
- DisallowedChildrenExtension
<?php
/**
* Implements "disallowed children" functionality for page classes. Usage:
* private static $disallowed_children = array(
* 'SomePageType',
* 'SomeOtherPageType'
* );
* Note: not fully tested (e.g. does this prevent changing parent in "Settings" tab?)
* @author Loz Calver <kinglozzer@gmail.com>
*/
class DisallowedChildrenExtension extends Extension {
/**
* @param array &$def
*/
public function updateSiteTreeHints(array &$def) {
if(empty($def)) continue;
foreach($def as $class => $data) {
if(in_array($class, array('All', 'Root'))) continue;
$singleton = singleton($class);
$disallowed = $singleton->stat('disallowed_children');
if(isset($disallowed) && ! empty($disallowed)) {
$orig = isset($data['disallowedChildren']) ? $data['disallowedChildren'] : array();
$disallowed = array_unique(array_merge($orig, $disallowed));
$def[$class]['disallowedChildren'] = array_values($disallowed);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment