From 1ac1ea73f3384a55eb1c1553de79b67edc7168dd Mon Sep 17 00:00:00 2001 From: Andrew Aitken-Fincham Date: Thu, 1 Mar 2018 15:31:50 +0000 Subject: [PATCH] add controller_name config var to SiteTree for easier override --- code/Model/SiteTree.php | 14 ++++++++++++++ tests/php/Model/SiteTreeTest.php | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 82b8f595..e38f2e85 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -185,6 +185,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi */ private static $hide_ancestor = null; + /** + * You can define the class of the controller that maps to your SiteTree object here if + * you don't want to rely on the magic of appending Controller to the Classname + * + * @config + * @var string + */ + private static $controller_name = null; + private static $db = array( "URLSegment" => "Varchar(255)", "Title" => "Varchar(255)", @@ -2763,11 +2772,16 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi /** * Find the controller name by our convention of {$ModelClass}Controller + * Can be overriden by config variable * * @return string */ public function getControllerName() { + if ($controller = Config::inst()->get(static::class, 'controller_name')) { + return $controller; + } + //default controller for SiteTree objects $controller = ContentController::class; diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index 067e9284..097fcc08 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -1504,6 +1504,16 @@ class SiteTreeTest extends SapphireTest $this->assertSame('PageController', $class->getControllerName()); } + /** + * Test that the controller name for a SiteTree instance can be gathered when set directly via config var + */ + public function testGetControllerNameFromConfig() + { + Config::inst()->update(Page::class, 'controller_name', 'This\\Is\\A\\New\\Controller'); + $class = new Page; + $this->assertSame('This\\Is\\A\\New\\Controller', $class->getControllerName()); + } + /** * Test that underscored class names (legacy) are still supported (deprecation notice is issued though). */