SilverStripe dynamic URL ModelAdmin and title not working

I created a custom ModelAdminlike 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 commented on the static properties $menu_titleand $url_segmentbecause I want them to be dynamic (i.e. these properties should depend on the domain name).

To do this, I created the extension as follows:

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 _config.phpI added the following code to activate the extension:

LeftAndMain::add_extension('CompanyMenu');

Problem

All this code correctly displays the menu in the CMS, but:

  • The icon specified in ModelAdmindoes not appear
  • After pressing the "Menu" button, the "Not Found" pop-up window appears.

If I uncomment the two lines in CompanyAdminand comment out the extension code in _config.php, everything will work fine.

Where am I doing wrong?

+4
2

CompanyAdmin _config.php:

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';
}

- $menu_title $url_segment CompanyAdmin, . :

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'; 
}
+4

, , LeftAndMainExtension, , , .

LeftAndMainExtension, .

-, ModelAdmin , .

-, , , . LeftAndMain, css .

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 '';
}

CSS, , LeftAndMain::MainMenu()

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

" ", , , , . , CMSMenu::add_menu_item(), LeftAndMainExtension , .

+1

Source: https://habr.com/ru/post/1649355/


All Articles