Jump to content

addCSS and addJS not working


Recommended Posts

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 by ralfillo (see edit history)
Link to comment
Share on other sites

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 by lexical_error (see edit history)
  • Like 1
Link to comment
Share on other sites

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

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 :P

 

<?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

  • 3 years later...
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

  • 3 years later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...