dongling5411
2017-07-27 11:02
浏览 51
已采纳

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

I'm learning to write modules to prestashop 1.7, currently I'm trying to load css and js files that will be used when the user tried to configure the module.

this is the code of my module:

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

}

so I registered the admin set Media hook with the name actionAdminControllerSetMedia but it seems that it doesn't have the functions to set stylesheet and javascript cause I get the same error for both: Uncaught Symfony\Component\Debug\Exception\UndefinedMethodException: Attempted to call an undefined method named "registerStylesheet" OR "registerJavascript" of class "AdminModulesController".

I'm really new to this.. I read that I need to set it in the front controller.. but doesn't that mean that it will appear in the regular page and not in the configuration page?

don't know how to resolve this and a bit confused, so any information regarding the issue would be greatly appreciated.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshan1959 2017-07-27 11:11
    已采纳

    To load CSS or JS you have tu use this hook, with this snippet:

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

    Enjoy :)

    PS: You have to register the display backoffice header hook first

    打赏 评论
  • drouie2014 2018-11-05 04:38

    You did two mistakes:

    1. Incorrect methods call
    2. The invalid path to the CSS file

    The correct code (with some improvements) to register assets in PrestaShop 1.7 is:

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

    Here is the detailed information, how to register JavaScript in a back-office (in admin pages).

    打赏 评论

相关推荐 更多相似问题