add controller_name config var to SiteTree for easier override

This commit is contained in:
Andrew Aitken-Fincham 2018-03-01 15:31:50 +00:00
parent 5735beeb90
commit 1ac1ea73f3
2 changed files with 24 additions and 0 deletions

View File

@ -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;

View File

@ -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).
*/