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

时间:2017-07-27 11:02:16

标签: php prestashop prestashop-1.7

我正在学习将模块写入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"

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

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

3 个答案:

答案 0 :(得分:2)

要加载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:您必须首先注册显示后台首页挂钩

答案 1 :(得分:1)

您犯了两个错误:

  1. 错误的方法调用
  2. CSS文件的无效路径

在PrestaShop 1.7中注册资产的正确代码(经过一些改进)是:

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

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

答案 2 :(得分:1)

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