diff --git a/code/Controllers/ModelAsController.php b/code/Controllers/ModelAsController.php index ee5409b5..7984a8b2 100644 --- a/code/Controllers/ModelAsController.php +++ b/code/Controllers/ModelAsController.php @@ -37,25 +37,13 @@ class ModelAsController extends Controller implements NestedController { * @return ContentController */ public static function controller_for(SiteTree $sitetree, $action = null) { - if ($sitetree->class == 'SilverStripe\\CMS\\Model\\SiteTree') { - $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"; - } + $controller = $sitetree->getControllerName(); - if($action && class_exists($controller . '_' . ucfirst($action))) { + if ($action && class_exists($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() { diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 175ae86e..010ee8cb 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -2712,6 +2712,26 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid 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. *