From 6948267c41c486eb501049555df95903fb6573ec Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 2 May 2016 20:57:43 +1200 Subject: [PATCH] API LeftAndMain::menu_title can be overridden (#5423) --- admin/code/CMSMenu.php | 5 ++-- admin/code/LeftAndMain.php | 58 ++++++++++++++++++++++++++---------- admin/code/SecurityAdmin.php | 2 +- admin/tests/CMSMenuTest.php | 23 ++++++++++++++ 4 files changed, 68 insertions(+), 20 deletions(-) diff --git a/admin/code/CMSMenu.php b/admin/code/CMSMenu.php index 38b3dd39b..4dd657d2d 100644 --- a/admin/code/CMSMenu.php +++ b/admin/code/CMSMenu.php @@ -79,8 +79,7 @@ class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider { // doesn't work if called outside of a controller context (e.g. in _config.php) // as the locale won't be detected properly. Use {@link LeftAndMain->MainMenu()} to update // titles for existing menu entries - $defaultTitle = LeftAndMain::menu_title_for_class($controllerClass); - $menuTitle = _t("{$controllerClass}.MENUTITLE", $defaultTitle); + $menuTitle = LeftAndMain::menu_title($controllerClass); return new CMSMenuItem($menuTitle, $link, $controllerClass, $menuPriority); } @@ -335,7 +334,7 @@ class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider { $cmsClasses = self::get_cms_classes(); $entities = array(); foreach($cmsClasses as $cmsClass) { - $defaultTitle = LeftAndMain::menu_title_for_class($cmsClass); + $defaultTitle = LeftAndMain::menu_title($cmsClass, false); $ownerModule = i18n::get_owner_module($cmsClass); $entities["{$cmsClass}.MENUTITLE"] = array($defaultTitle, 'Menu title', $ownerModule); } diff --git a/admin/code/LeftAndMain.php b/admin/code/LeftAndMain.php index 6e7f687d5..c203e7b12 100644 --- a/admin/code/LeftAndMain.php +++ b/admin/code/LeftAndMain.php @@ -683,14 +683,40 @@ class LeftAndMain extends Controller implements PermissionProvider { } /** - * Returns the menu title for the given LeftAndMain subclass. - * Implemented static so that we can get this value without instantiating an object. - * Menu title is *not* internationalised. + * @deprecated 5.0 */ public static function menu_title_for_class($class) { + Deprecation::notice('5.0', 'Use menu_title() instead'); + return static::menu_title($class, false); + } + + /** + * Get menu title for this section (translated) + * + * @param string $class Optional class name if called on LeftAndMain directly + * @param bool $localise Determine if menu title should be localised via i18n. + * @return string Menu title for the given class + */ + public static function menu_title($class = null, $localise = true) { + if($class && is_subclass_of($class, __CLASS__)) { + // Respect oveloading of menu_title() in subclasses + return $class::menu_title(null, $localise); + } + if(!$class) { + $class = get_called_class(); + } + + // Get default class title $title = Config::inst()->get($class, 'menu_title', Config::FIRST_SET); - if(!$title) $title = preg_replace('/Admin$/', '', $class); - return $title; + if(!$title) { + $title = preg_replace('/Admin$/', '', $class); + } + + // Check localisation + if(!$localise) { + return $title; + } + return i18n::_t("{$class}.MENUTITLE", $title); } /** @@ -799,11 +825,10 @@ class LeftAndMain extends Controller implements PermissionProvider { // already set in CMSMenu::populate_menu(), but from a static pre-controller // context, so doesn't respect the current user locale in _t() calls - as a workaround, - // we simply call LeftAndMain::menu_title_for_class() again + // we simply call LeftAndMain::menu_title() again // if we're dealing with a controller if($menuItem->controller) { - $defaultTitle = LeftAndMain::menu_title_for_class($menuItem->controller); - $title = _t("{$menuItem->controller}.MENUTITLE", $defaultTitle); + $title = LeftAndMain::menu_title($menuItem->controller); } else { $title = $menuItem->title; } @@ -878,11 +903,9 @@ class LeftAndMain extends Controller implements PermissionProvider { * @return ArrayList */ public function Breadcrumbs($unlinked = false) { - $defaultTitle = LeftAndMain::menu_title_for_class($this->class); - $title = _t("{$this->class}.MENUTITLE", $defaultTitle); $items = new ArrayList(array( new ArrayData(array( - 'Title' => $title, + 'Title' => $this->menu_title(), 'Link' => ($unlinked) ? false : $this->Link() )) )); @@ -1885,12 +1908,15 @@ class LeftAndMain extends Controller implements PermissionProvider { * @return string */ public function SectionTitle() { - $class = get_class($this); - $defaultTitle = LeftAndMain::menu_title_for_class($class); - if($title = _t("{$class}.MENUTITLE", $defaultTitle)) return $title; + $title = $this->menu_title(); + if($title) { + return $title; + } foreach($this->MainMenu() as $menuItem) { - if($menuItem->LinkingMode != 'link') return $menuItem->Title; + if($menuItem->LinkingMode != 'link') { + return $menuItem->Title; + } } } @@ -1928,7 +1954,7 @@ class LeftAndMain extends Controller implements PermissionProvider { if($class == 'ModelAdmin') continue; if(ClassInfo::classImplements($class, 'TestOnly')) continue; - $title = _t("{$class}.MENUTITLE", LeftAndMain::menu_title_for_class($class)); + $title = LeftAndMain::menu_title($class); $perms["CMS_ACCESS_" . $class] = array( 'name' => _t( 'CMSMain.ACCESS', diff --git a/admin/code/SecurityAdmin.php b/admin/code/SecurityAdmin.php index 4b47ef0e5..fdd7d0cca 100755 --- a/admin/code/SecurityAdmin.php +++ b/admin/code/SecurityAdmin.php @@ -297,7 +297,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { } public function providePermissions() { - $title = _t("SecurityAdmin.MENUTITLE", LeftAndMain::menu_title_for_class($this->class)); + $title = $this->menu_title(); return array( "CMS_ACCESS_SecurityAdmin" => array( 'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => $title)), diff --git a/admin/tests/CMSMenuTest.php b/admin/tests/CMSMenuTest.php index a03af2cc9..15a787ada 100644 --- a/admin/tests/CMSMenuTest.php +++ b/admin/tests/CMSMenuTest.php @@ -25,6 +25,14 @@ class CMSMenuTest extends SapphireTest implements TestOnly { 'Controller menu item has the correct priority'); CMSMenu::clear_menu(); + // Add another controller + CMSMenu::add_controller('CMSMenuTest_CustomTitle'); + $menuItems = CMSMenu::get_menu_items(); + $menuItem = $menuItems['CMSMenuTest_CustomTitle']; + $this->assertInstanceOf('CMSMenuItem', $menuItem, 'Controller menu item is of class CMSMenuItem'); + $this->assertEquals('CMSMenuTest_CustomTitle (localised)', $menuItem->title); + CMSMenu::clear_menu(); + // Add a link to the menu CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com'); $menuItems = CMSMenu::get_menu_items(); @@ -107,3 +115,18 @@ class CMSMenuTest_LeftAndMainController extends LeftAndMain implements TestOnly private static $menu_priority = 50; } + +class CMSMenuTest_CustomTitle extends LeftAndMain implements TestOnly { + + private static $url_segment = 'CMSMenuTest_CustomTitle'; + + private static $menu_priority = 50; + + public static function menu_title($class = null, $localised = false) { + if($localised) { + return __CLASS__ . ' (localised)'; + } else { + return __CLASS__ . ' (unlocalised)'; + } + } +}