API LeftAndMain::menu_title can be overridden (#5423)

This commit is contained in:
Damian Mooyman 2016-05-02 20:57:43 +12:00 committed by Ingo Schommer
parent feca77c902
commit 6948267c41
4 changed files with 68 additions and 20 deletions

View File

@ -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) // 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 // as the locale won't be detected properly. Use {@link LeftAndMain->MainMenu()} to update
// titles for existing menu entries // titles for existing menu entries
$defaultTitle = LeftAndMain::menu_title_for_class($controllerClass); $menuTitle = LeftAndMain::menu_title($controllerClass);
$menuTitle = _t("{$controllerClass}.MENUTITLE", $defaultTitle);
return new CMSMenuItem($menuTitle, $link, $controllerClass, $menuPriority); return new CMSMenuItem($menuTitle, $link, $controllerClass, $menuPriority);
} }
@ -335,7 +334,7 @@ class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
$cmsClasses = self::get_cms_classes(); $cmsClasses = self::get_cms_classes();
$entities = array(); $entities = array();
foreach($cmsClasses as $cmsClass) { foreach($cmsClasses as $cmsClass) {
$defaultTitle = LeftAndMain::menu_title_for_class($cmsClass); $defaultTitle = LeftAndMain::menu_title($cmsClass, false);
$ownerModule = i18n::get_owner_module($cmsClass); $ownerModule = i18n::get_owner_module($cmsClass);
$entities["{$cmsClass}.MENUTITLE"] = array($defaultTitle, 'Menu title', $ownerModule); $entities["{$cmsClass}.MENUTITLE"] = array($defaultTitle, 'Menu title', $ownerModule);
} }

View File

@ -683,14 +683,40 @@ class LeftAndMain extends Controller implements PermissionProvider {
} }
/** /**
* Returns the menu title for the given LeftAndMain subclass. * @deprecated 5.0
* Implemented static so that we can get this value without instantiating an object.
* Menu title is *not* internationalised.
*/ */
public static function menu_title_for_class($class) { 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); $title = Config::inst()->get($class, 'menu_title', Config::FIRST_SET);
if(!$title) $title = preg_replace('/Admin$/', '', $class); if(!$title) {
return $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 // 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, // 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 we're dealing with a controller
if($menuItem->controller) { if($menuItem->controller) {
$defaultTitle = LeftAndMain::menu_title_for_class($menuItem->controller); $title = LeftAndMain::menu_title($menuItem->controller);
$title = _t("{$menuItem->controller}.MENUTITLE", $defaultTitle);
} else { } else {
$title = $menuItem->title; $title = $menuItem->title;
} }
@ -878,11 +903,9 @@ class LeftAndMain extends Controller implements PermissionProvider {
* @return ArrayList * @return ArrayList
*/ */
public function Breadcrumbs($unlinked = false) { public function Breadcrumbs($unlinked = false) {
$defaultTitle = LeftAndMain::menu_title_for_class($this->class);
$title = _t("{$this->class}.MENUTITLE", $defaultTitle);
$items = new ArrayList(array( $items = new ArrayList(array(
new ArrayData(array( new ArrayData(array(
'Title' => $title, 'Title' => $this->menu_title(),
'Link' => ($unlinked) ? false : $this->Link() 'Link' => ($unlinked) ? false : $this->Link()
)) ))
)); ));
@ -1885,12 +1908,15 @@ class LeftAndMain extends Controller implements PermissionProvider {
* @return string * @return string
*/ */
public function SectionTitle() { public function SectionTitle() {
$class = get_class($this); $title = $this->menu_title();
$defaultTitle = LeftAndMain::menu_title_for_class($class); if($title) {
if($title = _t("{$class}.MENUTITLE", $defaultTitle)) return $title; return $title;
}
foreach($this->MainMenu() as $menuItem) { 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($class == 'ModelAdmin') continue;
if(ClassInfo::classImplements($class, 'TestOnly')) 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( $perms["CMS_ACCESS_" . $class] = array(
'name' => _t( 'name' => _t(
'CMSMain.ACCESS', 'CMSMain.ACCESS',

View File

@ -297,7 +297,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
} }
public function providePermissions() { public function providePermissions() {
$title = _t("SecurityAdmin.MENUTITLE", LeftAndMain::menu_title_for_class($this->class)); $title = $this->menu_title();
return array( return array(
"CMS_ACCESS_SecurityAdmin" => array( "CMS_ACCESS_SecurityAdmin" => array(
'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => $title)), 'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => $title)),

View File

@ -25,6 +25,14 @@ class CMSMenuTest extends SapphireTest implements TestOnly {
'Controller menu item has the correct priority'); 'Controller menu item has the correct priority');
CMSMenu::clear_menu(); 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 // Add a link to the menu
CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com'); CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com');
$menuItems = CMSMenu::get_menu_items(); $menuItems = CMSMenu::get_menu_items();
@ -107,3 +115,18 @@ class CMSMenuTest_LeftAndMainController extends LeftAndMain implements TestOnly
private static $menu_priority = 50; 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)';
}
}
}