mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
Merge pull request #2115 from andrewandante/pulls/4/add_controller_name_config_var
add controller_name config var to SiteTree for easier override
This commit is contained in:
commit
262236c3e2
@ -192,6 +192,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)",
|
||||
@ -2779,11 +2788,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;
|
||||
|
||||
|
@ -1506,6 +1506,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).
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user