From 2d0a3544055dfa8f2b7147c09197478c5ade95f7 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 2 Oct 2013 19:06:54 +1300 Subject: [PATCH] API: Add attributes argument for CMSMenuItem. Currently help menu item is the only external link in the CMS and the ability for it to work is hardcoded in the template. This request makes the target attribute definable by CMSMenu::add_link(). Adds documentation for how to add a basic external link to the CMS. --- admin/code/CMSMenu.php | 51 ++++++++---- admin/code/CMSMenuItem.php | 61 ++++++++++++-- admin/code/LeftAndMain.php | 9 ++- admin/javascript/LeftAndMain.Menu.js | 7 ++ admin/templates/Includes/LeftAndMain_Menu.ss | 2 +- admin/tests/CMSMenuTest.php | 23 +++++- docs/en/howto/customize-cms-menu.md | 85 ++++++++++++++++---- 7 files changed, 198 insertions(+), 40 deletions(-) diff --git a/admin/code/CMSMenu.php b/admin/code/CMSMenu.php index af67468d5..f791d64f8 100644 --- a/admin/code/CMSMenu.php +++ b/admin/code/CMSMenu.php @@ -1,16 +1,21 @@ setAttributes($attributes); + } + self::$menu_item_changes[] = array( 'type' => 'add', 'code' => $code, - 'item' => new CMSMenuItem($menuTitle, $url, $controllerClass, $priority), + 'item' => $item, ); } /** - * Add a previously built menuitem object to the menu + * Add a previously built menu item object to the menu */ protected static function add_menu_item_obj($code, $cmsMenuItem) { self::$menu_item_changes[] = array( diff --git a/admin/code/CMSMenuItem.php b/admin/code/CMSMenuItem.php index 97cd75778..5239df26c 100644 --- a/admin/code/CMSMenuItem.php +++ b/admin/code/CMSMenuItem.php @@ -1,12 +1,18 @@ url = $url; $this->controller = $controller; $this->priority = $priority; + parent::__construct(); } - + + /** + * @param array $attributes + */ + public function setAttributes($attributes) { + $this->attributes = $attributes; + } + + /** + * @param array + * + * @return HTML + */ + public function getAttributesHTML($attrs = null) { + $exclude = (is_string($attrs)) ? func_get_args() : null; + + if(!$attrs || is_string($attrs)) { + $attrs = $this->attributes; + } + + // Remove empty + $attrs = array_filter((array)$attrs, function($v) { + return ($v || $v === 0 || $v === '0'); + }); + + // Create markkup + $parts = array(); + + foreach($attrs as $name => $value) { + $parts[] = ($value === true) ? "{$name}=\"{$name}\"" : "{$name}=\"" . Convert::raw2att($value) . "\""; + } + + return implode(' ', $parts); + } } diff --git a/admin/code/LeftAndMain.php b/admin/code/LeftAndMain.php index 04c62ae77..e795da913 100644 --- a/admin/code/LeftAndMain.php +++ b/admin/code/LeftAndMain.php @@ -219,7 +219,10 @@ class LeftAndMain extends Controller implements PermissionProvider { 'Help', _t('LeftAndMain.HELP', 'Help', 'Menu title'), $this->config()->help_link, - -2 + -2, + array( + 'target' => '_blank' + ) ); // Allow customisation of the access check by a extension @@ -616,6 +619,7 @@ class LeftAndMain extends Controller implements PermissionProvider { if($menuItems) { foreach($menuItems as $code => $menuItem) { // alternate permission checks (in addition to LeftAndMain->canView()) + if( isset($menuItem->controller) && $this->hasMethod('alternateMenuDisplayCheck') @@ -661,9 +665,10 @@ class LeftAndMain extends Controller implements PermissionProvider { $menuIcon = LeftAndMain::menu_icon_for_class($menuItem->controller); if (!empty($menuIcon)) $menuIconStyling .= $menuIcon; } - + $menu->push(new ArrayData(array( "MenuItem" => $menuItem, + "AttributesHTML" => $menuItem->getAttributesHTML(), "Title" => Convert::raw2xml($title), "Code" => DBField::create_field('Text', $code), "Link" => $menuItem->url, diff --git a/admin/javascript/LeftAndMain.Menu.js b/admin/javascript/LeftAndMain.Menu.js index 549e53aa9..0f4c02e42 100644 --- a/admin/javascript/LeftAndMain.Menu.js +++ b/admin/javascript/LeftAndMain.Menu.js @@ -230,6 +230,13 @@ // Ignore external links, fallback to standard link behaviour var isExternal = $.path.isExternal(this.attr('href')); if(e.which > 1 || isExternal) return; + + // if the developer has this to open in a new window, handle + // that + if(this.attr('target') == "_blank") { + return; + } + e.preventDefault(); var item = this.getMenuItem(); diff --git a/admin/templates/Includes/LeftAndMain_Menu.ss b/admin/templates/Includes/LeftAndMain_Menu.ss index 1f1da0c03..76903219c 100644 --- a/admin/templates/Includes/LeftAndMain_Menu.ss +++ b/admin/templates/Includes/LeftAndMain_Menu.ss @@ -24,7 +24,7 @@