Skip to content
This repository
Browse code

NEW Custom menu icons for the CMS main menu

Add optional custom menu icons to the CMS main menu for every class
extending LeftAndMain (ModelAdmin). Works by setting optional static
$menu_icon = '/path/to/image' and providing custom styling for added
icons. Works for the menu as well as icon in the right-side (GridField) pane header.
  • Loading branch information...
commit 82500dd4c4c608d858fce3683035e130be6b1a3b 1 parent 14759b6
Martimiz authored August 19, 2012 chillu committed August 29, 2012
34  admin/code/LeftAndMain.php
@@ -36,6 +36,11 @@ class LeftAndMain extends Controller implements PermissionProvider {
36 36
 	 * @var string
37 37
 	 */
38 38
 	static $menu_title;
  39
+
  40
+	/**
  41
+	 * @var string
  42
+	 */
  43
+	static $menu_icon;
39 44
 	
40 45
 	/**
41 46
 	 * @var int
@@ -432,6 +437,22 @@ static function menu_title_for_class($class) {
432 437
 		if(!$title) $title = preg_replace('/Admin$/', '', $class);
433 438
 		return $title;
434 439
 	}
  440
+
  441
+	/**
  442
+	 * Return styling for the menu icon, if a custom icon is set for this class
  443
+	 *
  444
+	 * Example: static $menu-icon = '/path/to/image/';
  445
+	 * @param type $class
  446
+	 * @return string
  447
+	 */
  448
+	static function menu_icon_for_class($class) {
  449
+		$icon = Config::inst()->get($class, 'menu_icon', Config::FIRST_SET);
  450
+		if (!empty($icon)) {
  451
+			$class = strtolower($class);
  452
+			return ".icon.icon-16.icon-{$class} { background: url('{$icon}'); } ";
  453
+		}
  454
+		return '';
  455
+	}
435 456
 	
436 457
 	public function show($request) {
437 458
 		// TODO Necessary for TableListField URLs to work properly
@@ -486,6 +507,10 @@ public function MainMenu($cached = true) {
486 507
 			// Encode into DO set
487 508
 			$menu = new ArrayList();
488 509
 			$menuItems = CMSMenu::get_viewable_menu_items();
  510
+
  511
+			// extra styling for custom menu-icons
  512
+			$menuIconStyling = '';
  513
+
489 514
 			if($menuItems) {
490 515
 				foreach($menuItems as $code => $menuItem) {
491 516
 					// alternate permission checks (in addition to LeftAndMain->canView())
@@ -526,6 +551,14 @@ public function MainMenu($cached = true) {
526 551
 					} else {
527 552
 						$title = $menuItem->title;
528 553
 					}
  554
+
  555
+					// Provide styling for custom $menu-icon. Done here instead of in
  556
+					// CMSMenu::populate_menu(), because the icon is part of
  557
+					// the CMS right pane for the specified class as well...
  558
+					if($menuItem->controller) {
  559
+						$menuIcon = LeftAndMain::menu_icon_for_class($menuItem->controller);
  560
+						if (!empty($menuIcon)) $menuIconStyling .= $menuIcon;
  561
+					}
529 562
 					
530 563
 					$menu->push(new ArrayData(array(
531 564
 						"MenuItem" => $menuItem,
@@ -536,6 +569,7 @@ public function MainMenu($cached = true) {
536 569
 					)));
537 570
 				}
538 571
 			}
  572
+			if ($menuIconStyling) Requirements::customCSS($menuIconStyling);
539 573
 
540 574
 			$this->_cache_MainMenu = $menu;
541 575
 		}
39  docs/en/howto/customize-cms-menu.md
Source Rendered
... ...
@@ -0,0 +1,39 @@
  1
+# How to customize the CMS Menu #
  2
+
  3
+## Defining a Custom Icon ##
  4
+
  5
+Every time you add a new extension of the `api:LeftAndMain` class to the CMS, SilverStripe will automatically create a new menu-item for it, with a default title and icon.
  6
+We can easily change that behaviour by using the static `$menu_title` and `$menu_icon` statics to
  7
+provide a custom title and icon.
  8
+
  9
+The most popular extension of LeftAndMain is the `api:ModelAdmin` class, so we'll use that for an example. 
  10
+We'll take the `ProductAdmin` class used in the [ModelAdmin reference](../reference/modeladmin#setup).
  11
+
  12
+First we'll need a custom icon. For this purpose SilverStripe uses 16x16 black-and-transparent PNG graphics.
  13
+In this case we'll place the icon in `mysite/images`, but you are free to use any location.
  14
+
  15
+	:::php
  16
+	class ProductAdmin extends ModelAdmin {
  17
+		// ...
  18
+		static $menu_icon = 'mysite/images/product-icon.png'; 
  19
+	}
  20
+
  21
+## Defining a Custom Title ##
  22
+
  23
+The title of menu entries is configured through the `$menu_title` static.
  24
+If its not defined, the CMS falls back to using the class name of the controller,
  25
+removing the "Admin" bit at the end.
  26
+
  27
+	:::php
  28
+	class ProductAdmin extends ModelAdmin {
  29
+		// ...
  30
+		static $menu_title = 'My Custom Admin'; 
  31
+	}
  32
+ 
  33
+In order to localize the menu title in different languages, use the `<classname>.MENUTITLE`
  34
+entity name, which is automatically created when running the i18n text collection.
  35
+For more information on language and translations, please refer to the [i18n](../reference/ii8n) docs.
  36
+	
  37
+## Related
  38
+
  39
+ * [How to extend the CMS interface](extend-cms-interface)
5  docs/en/howto/index.md
Source Rendered
@@ -12,6 +12,11 @@ the language and functions which are used in the guides.
12 12
 * [PHPUnit Configuration](phpunit-configuration). How to setup your testing environment with PHPUnit
13 13
 * [Extend the CMS Interface](extend-cms-interface). 
14 14
 * [How to customize CMS Tree](customize-cms-tree).
  15
+* [Cache control](cache-control). Override the default PHP cache-control settings.
  16
+* [Howto customize the CMS menu](customize-cms-menu).
  17
+* [How to create a navigation menu](navigation-menu). Create primary navigation for your website.
  18
+* [Paginating A List](pagination). Add pagination for an SS_List object.
  19
+* [How to make a simple contact form](simple-contact-form).
15 20
 
16 21
 ## Feedback
17 22
 

0 notes on commit 82500dd

Please sign in to comment.
Something went wrong with that request. Please try again.