mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
NEW: Allow pages to specify the controller they use
This commit is contained in:
parent
c46a6c3e9b
commit
3d827543a8
@ -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() {
|
||||
|
@ -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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user