ralfillo Posted April 11, 2016 Share Posted April 11, 2016 (edited) Hi guys,I'm following a module creation book, however when I try to do addCss ( and JS ) in the CSS and the JS files don't appear in the <head>. This is the file mymodcomments.php <?php class MyModComments extends Module{ public function __construct(){ $this->name = 'mymodcomments'; $this->tab = 'front_office_features'; $this->version = '0.1'; $this->author = 'Just Me'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('My Module of product comments'); $this->description = $this->l('With this module, your customers will be able to grade and comments your products'); } public function processConfiguration(){ if (Tools::isSubmit('mymod_pc_form')){ $enable_grades = Tools::getValue('enable_grades'); $enable_comments = Tools::getValue('enable_comments'); Configuration::updateValue('MYMOD_GRADES', $enable_grades); Configuration::updateValue('MYMOD_COMMENTS', $enable_comments); $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 getContent(){ $this->processConfiguration(); $this->assignConfiguration(); return $this->display(__FILE__, 'getContent.tpl'); } public function install(){ parent::install(); $this->registerHook('displayProductTabContent'); return true; $this->registerHook('header'); } public function processProductTabContent(){ if (Tools::isSubmit('mymod_pc_submit_comment')){ $id_product = Tools::getValue('id_product'); $grade = Tools::getValue('grade'); $comment = Tools::getValue('comment'); $insert = array( 'id_product' => (int)$id_product, 'grade' => (int)$grade, 'comment' => pSQL($comment), 'date_add' => date('Y-m-d H:i:s'), ); Db::getInstance()->insert('mymod_comment', $insert); $this->context->smarty->assign('new_comment_posted', 'true'); } } public function assignProductTabContent(){ $enable_grades = Configuration::get('MYMOD_GRADES'); $enable_comments = Configuration::get('MYMOD_COMMENTS'); $id_product = Tools::getValue('id_product'); $comments = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'mymod_comment WHERE id_product = '.(int)$id_product); $this->context->controller->addCSS($this->_path . 'views/css/ mymodcomments.css', 'all'); $this->context->controller->addJS($this->_path . 'views/js/ mymodcomments.js'); $this->context->smarty->assign('enable_grades', $enable_grades); $this->context->smarty->assign('enable_comments', $enable_comments); $this->context->smarty->assign('comments', $comments); } public function hookDisplayProductTabContent($params){ $this->processProductTabContent(); $this->assignProductTabContent(); return $this->display(__FILE__, 'displayProductTabContent.tpl'); } } ?> I created both files (mymodcomments.css and mymodcomments.js) in the right path. I've been trying to fix this problem for hours but I can't find the solution, I'd really appreciate your help. Thanks! Edited April 11, 2016 by ralfillo (see edit history) Link to comment Share on other sites More sharing options...
lexical_error Posted April 11, 2016 Share Posted April 11, 2016 (edited) hello i did it before as this way first add $this->registerHook('header') in public function install of ur module then add $this->context->controller->addJS($this->_path . 'views/js/ mymodcomments.js'); in public function hookHeader(). (for appear ur css or js in head) see blocktopmenu module for example. Edited April 11, 2016 by lexical_error (see edit history) 1 Link to comment Share on other sites More sharing options...
ralfillo Posted April 11, 2016 Author Share Posted April 11, 2016 Hi lexical_error, I just did the changes that you recommended me but it's still not working Do you have any other ideas? Thanks Link to comment Share on other sites More sharing options...
lexical_error Posted April 11, 2016 Share Posted April 11, 2016 addcss and addjs should be in hookHeader function public function hookHeader() { $this->context->controller->addCSS($this->_path . 'views/css/ mymodcomments.css', 'all'); $this->context->controller->addJS($this->_path . 'views/js/ mymodcomments.js'); } Link to comment Share on other sites More sharing options...
ralfillo Posted April 12, 2016 Author Share Posted April 12, 2016 Finally I coped most of the code from the repository from the book: https://github.com/FabienSerny/mymodcomments/commit/df34711810227e95177e07836b002d63530f0832 I adjusted it a bit and it works, it's a pity that I don't understand why wasn't working before <?php class MyModComments extends Module{ public function __construct(){ $this->name = 'mymodcomments'; $this->tab = 'front_office_features'; $this->version = '0.1'; $this->author = 'Just Me'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('My Module of product comments'); $this->description = $this->l('With this module, your customers will be able to grade and comments your products'); } public function install(){ parent::install(); $this->registerHook('displayProductTabContent'); return true; } public function processProductTabContent(){ if (Tools::isSubmit('mymod_pc_submit_comment')){ $id_product = Tools::getValue('id_product'); $grade = Tools::getValue('grade'); $comment = Tools::getValue('comment'); $insert = array( 'id_product' => (int)$id_product, 'grade' => (int)$grade, 'comment' => pSQL($comment), 'date_add' => date('Y-m-d H:i:s'), ); Db::getInstance()->insert('mymod_comment', $insert); $this->context->smarty->assign('new_comment_posted', 'true'); } } public function assignProductTabContent() { $enable_grades = Configuration::get('MYMOD_GRADES'); $enable_comments = Configuration::get('MYMOD_COMMENTS'); $id_product = Tools::getValue('id_product'); $comments = Db::getInstance()->executeS(' SELECT * FROM `'._DB_PREFIX_.'mymod_comment` WHERE `id_product` = '.(int)$id_product); $this->context->controller->addCSS($this->_path.'views/css/mymodcomments.css', 'all'); $this->context->controller->addJS($this->_path.'views/js/mymodcomments.js'); $this->context->smarty->assign('enable_grades', $enable_grades); $this->context->smarty->assign('enable_comments', $enable_comments); $this->context->smarty->assign('comments', $comments); } public function hookDisplayProductTabContent($params){ $this->processProductTabContent(); $this->assignProductTabContent(); return $this->display(__FILE__, 'displayProductTabContent.tpl'); } public function processConfiguration(){ if (Tools::isSubmit('mymod_pc_form')){ $enable_grades = Tools::getValue('enable_grades'); $enable_comments = Tools::getValue('enable_comments'); Configuration::updateValue('MYMOD_GRADES', $enable_grades); Configuration::updateValue('MYMOD_COMMENTS', $enable_comments); $this->context->smarty->assign('confirmation', 'ok'); } } } ?> Link to comment Share on other sites More sharing options...
ialhamdazeez Posted October 5, 2019 Share Posted October 5, 2019 On 4/11/2016 at 2:41 PM, lexical_error said: hello i did it before as this way first add $this->registerHook('header') in public function install of ur module then add $this->context->controller->addJS($this->_path . 'views/js/ mymodcomments.js'); in public function hookHeader(). (for appear ur css or js in head) see blocktopmenu module for example. Thank you very much. I have the same book and get the same error. Now is the error resolved with your hint Link to comment Share on other sites More sharing options...
2niTheGood Posted June 29, 2023 Share Posted June 29, 2023 Hello, I also encountered the same problem. CSS and JS files were not imported into the page. Following the previous advice I added a hookHeader function : public function hookHeader() { $this->context->controller->addCSS($this->_path . 'views/css/mymodcomments.css', 'all'); $this->context->controller->addJS($this->_path . 'views/js/mymodcomments.js'); } And I added this function in my install() function. I reset my module and great! It works! public function install() { parent::install(); $this->registerHook('displayProductTabContent'); $this->registerHook('header'); return true; } Thanks for your advice! Denis Link to comment Share on other sites More sharing options...
endriu107 Posted June 29, 2023 Share Posted June 29, 2023 Check this: https://devdocs.prestashop-project.org/8/themes/getting-started/asset-management/ Link to comment Share on other sites More sharing options...
ps8modules Posted June 29, 2023 Share Posted June 29, 2023 hookHeader is not supported in new versions of Prestashop. Use hookDisplayHeader. Next, run your css and js only on certain pages. Link to comment Share on other sites More sharing options...
ps8modules Posted June 29, 2023 Share Posted June 29, 2023 public function hookDisplayHeader() { if (in_array( $this->context->controller->controller_name, array('index','category', 'product')) { addcss and js} } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now