试图在 prestashop 1.7 管理模块中加载 js 和 css 文件

trying to load js and css files in prestashop 1.7 admin module

我正在学习为 prestashop 1.7 编写模块,目前我正在尝试加载 css 和用户尝试配置模块时将使用的 js 文件。

这是我的模块的代码:

class TuxInModComments extends Module
{

    function __construct()
    {
        $this->name = 'tuxinmodcomments';
        $this->tab = 'quick_bulk_update';
        $this->version = '0.1';
        $this->author = 'Kfir Ozer';
        $this->displayName = 'Tux-In Comments and Ranks';
        $this->description = 'With this module, your costumers will be able to grade and comment your products';
        $this->bootstrap = true;

        parent::__construct();
    }

    public function install() {
        parent::install();
        $this->registerHook('actionAdminControllerSetMedia');
        return true;
    }

    public function processConfiguration()
    {
        if (Tools::isSubmit('mymod_pc_form')) {
            $enable_grades = Tools::getValue('enable_grades');
            $enable_comements = Tools::getValue('enable_comments');
            $csvFile = Tools::getValue('csv_file');
            die(var_export($csvFile));
            Configuration::updateValue('MYMOD_GRADES', $enable_grades);
            Configuration::updateValue('MYMOD_COMMENTS', $enable_comements);
            $this->context->smarty->assign('confirmation', 'ok');
        }
    }

    public function assignConfiguration()
    {
        $enable_grades = Configuration::get('MYMOD_GRADES');
        $enable_comments = Configuration::get('MYMOD_COMMENTS');
        $this->context->smarty->assign('enable_grades', $enable_grades);
        $this->context->smarty->assign('enable_comments', $enable_comments);
    }

    public function hookActionAdminControllerSetMedia($params){
        $this->registerStylesheet('module-tuxinmodcomments-css','modules/tuxinmodcomments/js/getcontent.css');
        $this->registerJavascript('module-tuxinmodcomments-js','modules/tuxinmodcomments/js/getcontent.js');
    }


    public function getContent() {
        $this->processConfiguration();
        $this->assignConfiguration();
        return $this->display(__FILE__,'getContent.tpl');
    }

}

所以我用名称 actionAdminControllerSetMedia 注册了管理集媒体挂钩,但它似乎没有设置样式表和 javascript 的功能,因为我对两者都得到了相同的错误: Uncaught Symfony\Component\Debug\Exception\UndefinedMethodException: Attempted to call an undefined method named "registerStylesheet" OR "registerJavascript" of class "AdminModulesController".

我真的很陌生..我读到我需要在前端控制器中设置它..但这是否意味着它会出现在常规页面而不是配置页面中?

不知道如何解决这个问题,有点困惑,因此非常感谢有关此问题的任何信息。

要加载 CSS 或 JS,您必须使用此挂钩,并使用以下代码段:

public function hookDisplayBackOfficeHeader()
{
    $this->context->controller->addCSS($this->_path.'pathtocss/module.css', 'all');
    $this->context->controller->addJS($this->_path.'pathtojs/module.js', 'all');
}

尽情享受吧:)

PS: 必须先注册显示后台header hook

由于后台需要注册资产,即AdminController,所以需要使用addJSaddCSS方法 因此,通过模块 class 添加 JS 和 CSS 文件的正确示例是:

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

这里是详细信息,how to register JavaScript in a back-office (in admin pages).

如果您需要在 PrestaShop 1.7 中为前台(即 FrontController)注册资产,那么您需要使用 registerJavascriptregisterStylesheet 方法:

public function hookHeader($params)
{
    $this->context->controller->registerJavascript(
        'module-tuxinmodcomments',
        'modules/' . $this->name . '/views/js/getcontent.js'
    );

    $this->context->controller->registerStylesheet(
        'module-tuxinmodcomments',
        'modules/' . $this->name . '/views/css/getcontent.css'
    );
}

添加CSS和JS文件到hookHeader:

public function hookHeader()
{
    $this->context->controller->addCSS($this->_path . 'views/css/styles.css');
    $this->context->controller->addJS($this->_path . 'views/js/script.js');
}

注册 hookHeader:

public function install()
{
    return parent::install()
        && $this->registerHook('header');
}