Jump to content
ralfillo

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)

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


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

}

?>

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More