Jump to content

What am I Doing Wrong? Adding Hook...


Recommended Posts

Hi guys,


I've been trying to work this out for HOURS now - and am having no luck :(


I have tried using the override folder, but couldn't get it to work - so for now, im just editing the original files -- once it works, i'll try and move my mods to the override files)


In themes\{my-theme}\header.tpl. I added this...




In classes\FrontController.php. I added the following line...


  'HOOK_HEADER' => Module::hookExec('header'),
  'HOOK_TOP' => Module::hookExec('top'),
  'HOOK_MYTEST' => Module::hookExec('mytest'), /// MY NEW LINE!!!
  'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn')


In modules\blockpermanentlinks\blockpermanentlinks.php.


I added $this->registerHook('mytest') into the install function...


function install()
  return (parent::install() AND $this->registerHook('top') AND $this->registerHook('mytest') AND $this->registerHook('header'));


I also added...


function hookmytest($params)
 echo 'hello hello hello hello';
 return 'hello hello hello hello';


Then, in my database, in the ps_hook table, i added a new row, with...


name: mytest
title: mytest
desc: just a test
position: 1
live_edit 0



I have turned on.. Back Office > Preferences > Performance > Force compile.

And i have disabled Cache.


I have tried so many guides on the net, and in this forum, but none seem to work.


Please help!


Thanks for reading. :)

Link to comment
Share on other sites

I'm wondering if the order you did the above in was maybe wrong. Try uninstalling and installing the module again - maybe the registerHook() call failed as the hook wasn't set up in the database correctly when the module was first installed?



  • Like 1
Link to comment
Share on other sites

Paul C -- thanks so much! I've honestly being trying to do this for close to 10hrs -- and i've been missing the last step! All my code works -- but i didn't realise that in the prestashop admin, you have to go to the modules, find the one you've edited, and click "Reset"!!! I can't believe it! LOL :)


It works great now thanks -- but it has caused me to have another question...


In header.tpl, the hook is {$HOOK_TOP}



I started editing the blockpermanentlinks module, because it contained $this->registerHook('top').


But now i see that $this->registerHook('top') is also in the blocksearch module.


Does that mean that, wherever $HOOK_TOP is in the template, it will load the hook in all the modules that contain the registerHook('top')???


Thanks again for the help!

Link to comment
Share on other sites

There are various elements to the process, here's a summary:


A Module can implement any or all of the standard Prestashop hooks they want. e.g. hookRightColumn(), hookLeftColumn(), hookTop() and hookHeader().


Unless you make a call to registerHook() though, the module will do nothing (that's why your output didn't display as the install function is only ever called at install time, so the registerHook() function had never been called for your new hook).


The purpose of implementing multiple display hook points is usually so that an end user can choose to move the module around in their store. They can only move it to a hook supported by your module. In general within the install function you only make a registerHook() call for the "default" location your module should display in. It's common to see in a module something like:


function hookLeftColumn($params)
 // some code to build something useful useful in a variable e.g. $content = 'Hello World';
 return $content;

function hookRightColumn($params)
 return $this->hookLeftColumn($params);


The hookRightColumn() function just does the same as the hookLeftColumn() function, but implementing it allows the user to move the module output from left to right. In the install function for the above there would be a call to registerHook('leftColumn'); and to display in the right column the end-user would use the Module->positions menu option in the Back Office.


In some cases a module will need to register more than one hook though; for example you may always want your module to use hookHeader() so you can insert any necessary css and js (using Tools:addCSS() and/or Tools::addJS() calls) into the page header.


Hope this helps,



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