Can't add js and css files to back office

前端 未结 2 838
甜味超标
甜味超标 2021-01-26 02:24

I want to add JS and CSS files to back office in my module. But I get error: Attempted to call an undefined method named \"registerStylesheet\" of class \"AdminModulesController

2条回答
  •  有刺的猬
    2021-01-26 02:42

    The explanation of which methods to use:

    These are FrontController methods in PrestaShop 1.7: registerJavascript and registerStylesheet.

    These are legacy (deprecated) FrontController methods in PrestaShop 1.7: addJS and addCSS.

    These are AdminController methods in PrestaShop 1.7, 1.6, 1.5: addJS and addCSS.

    So, the correct example to add a JS and a CSS files for a back-office (i.e. for AdminController) via a module class is:

    public function hookActionAdminControllerSetMedia($params)
    { 
        // Adds your's CSS file from a module's directory
        $this->context->controller->addCSS($this->_path . 'views/css/example.css'); 
    
        // Adds your's JavaScript file from a module's directory
        $this->context->controller->addJS($this->_path . 'views/js/example.js');
    }
    

    For an additional information see my yet another answer how to register JavaScript in a back-office (in admin pages). I have updated it after this question.

提交回复
热议问题