Merge pull request #5881 from open-sausages/pulls/4.0/cmsmenu-byclass

API Add CMSMenu::remove_menu_class to remove items by class instead of code
This commit is contained in:
Hamish Friedlander 2016-08-11 08:03:26 +07:00 committed by GitHub
commit f95d577c87
3 changed files with 41 additions and 4 deletions

View File

@ -35,4 +35,4 @@ HTMLEditorConfig::get('cms')
'ssbuttons' => FRAMEWORK_DIR . '/client/dist/js/TinyMCE_SSPlugin.js' 'ssbuttons' => FRAMEWORK_DIR . '/client/dist/js/TinyMCE_SSPlugin.js'
)); ));
CMSMenu::remove_menu_item('CMSProfileController'); CMSMenu::remove_menu_class('CMSProfileController');

View File

@ -77,7 +77,9 @@ CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
$menuPriority = Config::inst()->get($controllerClass, 'menu_priority', Config::FIRST_SET); $menuPriority = Config::inst()->get($controllerClass, 'menu_priority', Config::FIRST_SET);
// Don't add menu items defined the old way // Don't add menu items defined the old way
if($urlSegment === null && $controllerClass != "CMSMain") return; if (!$urlSegment) {
return null;
}
$link = Controller::join_links($urlBase, $urlSegment) . '/'; $link = Controller::join_links($urlBase, $urlSegment) . '/';
@ -126,7 +128,7 @@ CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
$attributes = null) { $attributes = null) {
// If a class is defined, then force the use of that as a code. This helps prevent menu item duplication // If a class is defined, then force the use of that as a code. This helps prevent menu item duplication
if($controllerClass) { if($controllerClass) {
$code = $controllerClass; $code = self::get_menu_code($controllerClass);
} }
return self::replace_menu_item($code, $menuTitle, $url, $controllerClass, $priority, $attributes); return self::replace_menu_item($code, $menuTitle, $url, $controllerClass, $priority, $attributes);
@ -143,6 +145,16 @@ CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
return (isset($menuItems[$code])) ? $menuItems[$code] : false; return (isset($menuItems[$code])) ? $menuItems[$code] : false;
} }
/**
* Get menu code for class
*
* @param string $cmsClass Controller class name
* @return string
*/
public static function get_menu_code($cmsClass) {
return Convert::raw2htmlname(str_replace('\\', '-', $cmsClass));
}
/** /**
* Get all menu entries. * Get all menu entries.
* *
@ -156,7 +168,10 @@ CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
$cmsClasses = self::get_cms_classes(); $cmsClasses = self::get_cms_classes();
foreach($cmsClasses as $cmsClass) { foreach($cmsClasses as $cmsClass) {
$menuItem = self::menuitem_for_controller($cmsClass); $menuItem = self::menuitem_for_controller($cmsClass);
if($menuItem) $menuItems[Convert::raw2htmlname(str_replace('\\', '-', $cmsClass))] = $menuItem; $menuCode = self::get_menu_code($cmsClass);
if($menuItem) {
$menuItems[$menuCode] = $menuItem;
}
} }
} }
@ -230,6 +245,16 @@ CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
self::$menu_item_changes[] = array('type' => 'remove', 'code' => $code); self::$menu_item_changes[] = array('type' => 'remove', 'code' => $code);
} }
/**
* Remove menu item by class name.
*
* @param string $className Name of class
*/
public static function remove_menu_class($className) {
$code = self::get_menu_code($className);
self::remove_menu_item($code);
}
/** /**
* Clears the entire menu * Clears the entire menu
*/ */

View File

@ -45,6 +45,18 @@ class CMSMenuTest extends SapphireTest implements TestOnly {
CMSMenu::clear_menu(); CMSMenu::clear_menu();
} }
public function testRemove() {
CMSMenu::clear_menu();
CMSMenu::add_menu_item('custom', 'Custom Title', 'custom');
CMSMenu::add_menu_item('other', 'Other Section', 'other', 'CMSMenuTest_LeftAndMainController');
$this->assertNotEmpty(CMSMenu::get_menu_items());
CMSMenu::remove_menu_class('CMSMenuTest_LeftAndMainController');
CMSMenu::remove_menu_item('custom');
$this->assertEmpty(CMSMenu::get_menu_items());
}
public function testLinkWithExternalAttributes() { public function testLinkWithExternalAttributes() {
CMSMenu::clear_menu(); CMSMenu::clear_menu();