NEW: Allow pages to specify the controller they use

This commit is contained in:
Loz Calver 2016-09-08 15:46:48 +01:00
parent c46a6c3e9b
commit 3d827543a8
2 changed files with 23 additions and 15 deletions

View File

@ -37,25 +37,13 @@ class ModelAsController extends Controller implements NestedController {
* @return ContentController * @return ContentController
*/ */
public static function controller_for(SiteTree $sitetree, $action = null) { public static function controller_for(SiteTree $sitetree, $action = null) {
if ($sitetree->class == 'SilverStripe\\CMS\\Model\\SiteTree') { $controller = $sitetree->getControllerName();
$controller = "SilverStripe\\CMS\\Controllers\\ContentController";
} else {
$ancestry = ClassInfo::ancestry($sitetree->class);
while ($class = array_pop($ancestry)) {
if (class_exists($class . "_Controller")) {
break;
}
}
$controller = ($class !== null)
? "{$class}_Controller"
: "SilverStripe\\CMS\\Controllers\\ContentController";
}
if($action && class_exists($controller . '_' . ucfirst($action))) { if ($action && class_exists($controller . '_' . ucfirst($action))) {
$controller = $controller . '_' . ucfirst($action); $controller = $controller . '_' . ucfirst($action);
} }
return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree; return Injector::inst()->create($controller, $sitetree);
} }
public function init() { public function init() {

View File

@ -2712,6 +2712,26 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
return 1; return 1;
} }
/**
* @return string
*/
public function getControllerName() {
if ($this->class === SiteTree::class) {
$controller = ContentController::class;
} else {
$ancestry = ClassInfo::ancestry($this->class);
while ($class = array_pop($ancestry)) {
if (class_exists($class . "_Controller")) {
break;
}
}
$controller = ($class !== null) ? "{$class}_Controller" : ContentController::class;
}
return $controller;
}
/** /**
* Return the CSS classes to apply to this node in the CMS tree. * Return the CSS classes to apply to this node in the CMS tree.
* *