Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ENHANCEMENT: add optional custom menu icon to CMS main menu #725

Closed
wants to merge 9 commits into from

4 participants

@Martimiz

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 for the icon in the right-side (GridField) pane header.

@Martimiz Martimiz ENHANCEMENT: add optional custom menu icon to 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.
e347295
@travisbot

This pull request passes (merged e347295 into 4916b36).

@chillu
Owner

Good change! Can you please write a "Howto: Customize the CMS Menu" for this as well? The feature will only be half as useful if its not documented :)

@Martimiz

Sure... Should I create a seperate pull request for the howto, or combine the two in a new pull request?

@chillu
Owner
@sminnee
Owner

You should be able to simply commit to this branch and it wi be added to this pull requestz

Martimiz added some commits
@Martimiz Martimiz ENHANCEMENT: add optional custom menu icon to 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.
d30ed41
@Martimiz Martimiz Merge branch '3.0-cms-menu-icon' of https://github.com/Martimiz/sapphire
 into 3.0-cms-menu-icon
df10fe4
@travisbot

This pull request passes (merged df10fe4 into 69182c2).

Martimiz added some commits
@Martimiz Martimiz DOCS: added doc how to customize the cms menu
Added doc on how to customize the cms to en/howto,
068e42d
@Martimiz Martimiz Update docs/en/howto/index.md
UPDATE: Added 5 missing links to howto/index.md, including one for Howto customize the CMS menu.
c422343
@Martimiz Martimiz Update docs/en/howto/customize-cms-menu.md 0682184
@travisbot

This pull request passes (merged 068e42d into 69182c2).

@travisbot

This pull request passes (merged c422343 into 69182c2).

@travisbot

This pull request passes (merged 0682184 into 69182c2).

@Martimiz

I wrote a 'Howto customize the CMS menu' and added the link to howto/index.md. While doing that I also added the other missing links to that file.

I hope this is what you had in mind. Maybe, if this gets merged, ModelAdmin and its doc should be updated as well with the $menu_icon? Also please check the syntax and grammar of my doc, since I'm not a native :-)

(BTW: posted a small problem with my branch earlier, but that is now fixed, so I removed it)

@travisbot

This pull request passes (merged 2447b55 into ae9c2e7).

@Martimiz Martimiz Update docs/en/howto/customize-cms-menu.md
Updated the topic on Language files (translation). When changing the $menu_title, existing language files stay the same, and, even for default language, SS will display the old MENUTITLE entity from the language file, so the changes won't reflect in the CMS menu unless you change that too.
2961db6
@Martimiz

Hi, I've changed the topic on language files and translations in the howto, because there was something else going on then I originally thought. It should be correct now. Please let me know if this is what you had in mind.

@travisbot

This pull request passes (merged 2961db6 into ae9c2e7).

@chillu
Owner

Collapsed into one changeset, and committed in 82500dd.
Thanks Martimiz!

A couple of points though:

@chillu chillu closed this
@Martimiz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 19, 2012
  1. @Martimiz

    ENHANCEMENT: add optional custom menu icon to CMS main menu

    Martimiz authored
    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.
Commits on Aug 22, 2012
  1. @Martimiz

    ENHANCEMENT: add optional custom menu icon to CMS main menu

    Martimiz authored
    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.
  2. @Martimiz
  3. @Martimiz

    DOCS: added doc how to customize the cms menu

    Martimiz authored
    Added doc on how to customize the cms to en/howto,
  4. @Martimiz

    Update docs/en/howto/index.md

    Martimiz authored
    UPDATE: Added 5 missing links to howto/index.md, including one for Howto customize the CMS menu.
  5. @Martimiz
  6. @Martimiz
  7. @Martimiz
Commits on Aug 24, 2012
  1. @Martimiz

    Update docs/en/howto/customize-cms-menu.md

    Martimiz authored
    Updated the topic on Language files (translation). When changing the $menu_title, existing language files stay the same, and, even for default language, SS will display the old MENUTITLE entity from the language file, so the changes won't reflect in the CMS menu unless you change that too.
This page is out of date. Refresh to see the latest.
View
36 admin/code/LeftAndMain.php
@@ -36,6 +36,11 @@ class LeftAndMain extends Controller implements PermissionProvider {
* @var string
*/
static $menu_title;
+
+ /**
+ * @var string
+ */
+ static $menu_icon;
/**
* @var int
@@ -432,6 +437,22 @@ static function menu_title_for_class($class) {
if(!$title) $title = preg_replace('/Admin$/', '', $class);
return $title;
}
+
+ /**
+ * Return styling for the menu icon, if a custom icon is set for this class
+ *
+ * Example: static $menu-icon = '/path/to/image/';
+ * @param type $class
+ * @return string
+ */
+ static function menu_icon_for_class($class) {
+ $icon = Config::inst()->get($class, 'menu_icon', Config::FIRST_SET);
+ if (!empty($icon)) {
+ $class = strtolower($class);
+ return ".icon.icon-16.icon-{$class} { background: url('{$icon}'); } ";
+ }
+ return '';
+ }
public function show($request) {
// TODO Necessary for TableListField URLs to work properly
@@ -486,6 +507,10 @@ public function MainMenu($cached = true) {
// Encode into DO set
$menu = new ArrayList();
$menuItems = CMSMenu::get_viewable_menu_items();
+
+ // extra styling for custom menu-icons
+ $menuIconStyling = '';
+
if($menuItems) {
foreach($menuItems as $code => $menuItem) {
// alternate permission checks (in addition to LeftAndMain->canView())
@@ -526,6 +551,14 @@ public function MainMenu($cached = true) {
} else {
$title = $menuItem->title;
}
+
+ // Provide styling for custom $menu-icon. Done here instead of in
+ // CMSMenu::populate_menu(), because the icon is part of
+ // the CMS right pane for the specified class as well...
+ if($menuItem->controller) {
+ $menuIcon = LeftAndMain::menu_icon_for_class($menuItem->controller);
+ if (!empty($menuIcon)) $menuIconStyling .= $menuIcon;
+ }
$menu->push(new ArrayData(array(
"MenuItem" => $menuItem,
@@ -536,6 +569,7 @@ public function MainMenu($cached = true) {
)));
}
}
+ if ($menuIconStyling) Requirements::CustomCSS($menuIconStyling);
$this->_cache_MainMenu = $menu;
}
@@ -1614,4 +1648,4 @@ function setIsCurrent($bool) {
return $this;
}
-}
+}
View
54 docs/en/howto/customize-cms-menu.md
@@ -0,0 +1,54 @@
+# Howto customize the CMS Menu #
+
+## Introduction ##
+
+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, title and the default 'cogs' icon included.
+But we can easily change that behaviour by using the static `$menu-title` and `$menu-icon` variables to
+provide a custom title and icon.
+
+The most popular extension of LeftAndMain is by far the `api:ModelAdmin` class, so we'll use that for an example.
+We'll take the ProductAdmin class used in the [ModelAdmin reference](../reference/modeladmin#setup).
+
+## First: the icon ##
+
+First we'll need a custom icon. For this purpose SilverStripe uses 16x16 black-and-transparent png icons.
+In this case we'll place the icon in mysite/images, but you are free to use any location, as long as you
+provide the right path.
+
+## ProductAdmin ##
+
+ :::php
+ class ProductAdmin extends ModelAdmin {
+ public static $managed_models = array('Product','Category'); // Can manage multiple models
+ static $url_segment = 'products'; // Linked as /admin/products/
+
+ static $menu_title = 'My Product Admin';
+ static $menu-icon = 'mysite/images/product-icon.png';
+ }
+
+## Language file: MENUTITLE ##
+
+By default, when displaying the title for the menu item, SilverStripe will look for the `MENUTITLE` entity in
+your module's current language file and use that. This is true even for the default language (lang/en.yml):
+
+ :::yml
+ ...
+ ProductAdmin:
+ MENUTITLE: 'My Product Admin'
+
+Only when no language file is found for the current language, or the MENUITEM entity is not present in them, will
+$menu_title be used directly.
+
+The correct value for MENUTITLE will be created automatically if you use
+[i18nTextCollector](../reference/ii8n#collecting-text) to create your languagefile.
+If you're changing the menu title for a module with existing languagefiles, please check the values for
+MENUTITLE and adapt.
+
+For more information on language and translations,please refer to [i18n](../reference/ii8n).
+
+## Other changes ##
+
+Other changes to the appearance of the menu buttons, or any other parts of the CMS for that matter, should
+preferrably be done using stylesheets. Have a look at [How to extend the CMS interface](extend-cms-interface) for
+an extensive howto on extending the CMS.
View
5 docs/en/howto/index.md
@@ -12,6 +12,11 @@ the language and functions which are used in the guides.
* [PHPUnit Configuration](phpunit-configuration). How to setup your testing environment with PHPUnit
* [Extend the CMS Interface](extend-cms-interface).
* [How to customize CMS Tree](customize-cms-tree).
+* [Cache control](cache-control). Override the default PHP cache-control settings.
+* [Howto customize the CMS menu](customize-cms-menu).
+* [How to create a navigation menu](navigation-menu). Create primary navigation for your website.
+* [Paginating A List](pagination). Add pagination for an SS_List object.
+* [How to make a simple contact form](simple-contact-form).
## Feedback
Something went wrong with that request. Please try again.