4
votes

I have created a custom ModelAdmin like this:

class CompanyAdmin extends ModelAdmin {
    // private static $menu_title = 'Companies';
    // private static $url_segment = 'companies';
    private static $managed_models = 'Company';
    private static $menu_icon = 'mysite/images/icons/company-icon.png'; 
}

In the above code I have commented out the static properties $menu_title and $url_segment because I want them to be dynamic (i.e. these properties should be dependent upon the domain name).

For this I created an extension like this:

class CompanyMenu extends LeftAndMainExtension {

    public function init() {
        $id = 'Company';
        $title = $_SERVER["HTTP_HOST"] == "login.example.com" ? "Companies" : "Profile";
        $link =  $_SERVER["HTTP_HOST"] == "login.example.com" ? "admin/companies" : "admin/profile";

        CMSMenu::add_menu_item($id, $title, $link);
    }
}

In the _config.php I added following code to activate the extension:

LeftAndMain::add_extension('CompanyMenu');

Problem

All this code renders the menu in the CMS correctly but:

  1. The icon defined in ModelAdmin does not come
  2. Upon clicking Menu I get "Not Found" popup.

If I uncomment two lines in CompanyAdmin and comment the extension code in _config.php everything works fine.

Where am I doing wrong?

2

2 Answers

4
votes

You can dynamically set the CompanyAdmin config settings in your _config.php file:

if ($_SERVER['HTTP_HOST'] == 'login.example.com') {
    CompanyAdmin::config()->menu_title = 'Companies';
    CompanyAdmin::config()->url_segment = 'companies';
} else {
    CompanyAdmin::config()->menu_title = 'Profile';
    CompanyAdmin::config()->url_segment = 'profile';
}

I would suggest to still set the $menu_title and $url_segment in the CompanyAdmin class so there is a default fallback setting. The config settings will override these:

class CompanyAdmin extends ModelAdmin {
    private static $menu_title = 'Companies';
    private static $url_segment = 'company';
    private static $managed_models = 'Company';
    private static $menu_icon = 'mysite/images/icons/company-icon.png'; 
}
1
votes

I think you're muddying the waters a bit by extending LeftAndMainExtension when you probably don't have to.

Going along with your idea of extending LeftAndMainExtension, there's a few things you're missing.

First, your ModelAdmin isn't being used, so the icon and controller are not being picked up from it.

Second, you're missing a block which should look something like this to add an icon to your menu. This is a method on LeftAndMain which creates the css to add icons to menu items.

public static function menu_icon_for_class($class) {
    $icon = Config::inst()->get($class, 'menu_icon', Config::FIRST_SET);
    if (!empty($icon)) {
        $class = strtolower(Convert::raw2htmlname(str_replace('\\', '-', $class)));
        return ".icon.icon-16.icon-{$class} { background-image: url('{$icon}'); } ";
    }
    return '';
}

The CSS returned from this is added to the requirements for the admin area in this line of LeftAndMain::MainMenu()

if ($menuIconStyling) Requirements::customCSS($menuIconStyling);

Regarding the "not found" error you're receiving, it's caused by you not specifying what should happen when the icon is clicked. This is something you need to specify in either CMSMenu::add_menu_item() or your LeftAndMainExtension depending on how you want to handle it.