jellyMan Posted October 7, 2016 Share Posted October 7, 2016 How to add a hook to an existing module with prestashop 1.6. There is a blockcontact module that comes with prestashop. I'd like to register a new Hook for it called "displayHeaderMiddle", so I can load the module into a certain area of my site. I don't fancy editing the /modules/blockcontact/blockcontact.php file because I think that's bad right? I've done a bit of investigating and think I need to either create a /override/blockcontact/blockcontact.php file and add an install() method and override the default one. - If it's this one do I need to re-install the module in the admin area? Or can I do something in my theme, like create a /themes/my-theme/modules/blockcontact/blockcontact.php file and to something there. I'm stumped here guys. Thanks in advance for any help. Link to comment Share on other sites More sharing options...
Daniel Patilea Posted October 7, 2016 Share Posted October 7, 2016 On 10/7/2016 at 9:31 AM, jellyMan said: How to add a hook to an existing module with prestashop 1.6. There is a blockcontact module that comes with prestashop. I'd like to register a new Hook for it called "displayHeaderMiddle", so I can load the module into a certain area of my site. I don't fancy editing the /modules/blockcontact/blockcontact.php file because I think that's bad right? I've done a bit of investigating and think I need to either create a /override/blockcontact/blockcontact.php file and add an install() method and override the default one. - If it's this one do I need to re-install the module in the admin area? Or can I do something in my theme, like create a /themes/my-theme/modules/blockcontact/blockcontact.php file and to something there. I'm stumped here guys. Thanks in advance for any help. There's no need of creating another hook, just make an override for the module and assign it a new hook where you can place on your site. Link to comment Share on other sites More sharing options...
jellyMan Posted October 7, 2016 Author Share Posted October 7, 2016 Sorry I don't understand what you mean? 1 Link to comment Share on other sites More sharing options...
Daniel Patilea Posted October 7, 2016 Share Posted October 7, 2016 On 10/7/2016 at 9:38 AM, jellyMan said: Sorry I don't understand what you mean? Here's a tutorial on how you can create on override 1 Link to comment Share on other sites More sharing options...
jellyMan Posted October 7, 2016 Author Share Posted October 7, 2016 I've created an override for this now - but how would I go about registering a hook? The methods ive created on my override don't seem to be getting fired. Link to comment Share on other sites More sharing options...
Daniel Patilea Posted October 7, 2016 Share Posted October 7, 2016 On 10/7/2016 at 1:53 PM, jellyMan said: I've created an override for this now - but how would I go about registering a hook? The methods ive created on my override don't seem to be getting fired. Did you deleted the class_index.php file from /cache folder? Link to comment Share on other sites More sharing options...
jellyMan Posted October 7, 2016 Author Share Posted October 7, 2016 (edited) I did yeh - my override code looks like this... <?php if (!defined('_CAN_LOAD_FILES_')) exit; class BlockcontactOverride extends Blockcontact { public function registerDisplayHeaderMiddleHook() { return $this->registerHook('displayHeaderMiddle'); } public function hookDisplayHeaderMiddle() { return 'Contact block header middle hook'; } } Edited October 7, 2016 by jellyMan (see edit history) 1 Link to comment Share on other sites More sharing options...
Daniel Patilea Posted October 7, 2016 Share Posted October 7, 2016 On 10/7/2016 at 2:06 PM, jellyMan said: I did yeh - my override code looks like this... <?php if (!defined('_CAN_LOAD_FILES_')) exit; class BlockcontactOverride extends Blockcontact { public function registerDisplayHeaderMiddleHook() { return $this->registerHook('displayHeaderMiddle'); } public function hookDisplayHeaderMiddle() { return 'Contact block header middle hook'; } } It looks ok to me. Now you also have to hook the module to those positions. Go to Modules > Positions > Transplant new module Link to comment Share on other sites More sharing options...
jellyMan Posted October 7, 2016 Author Share Posted October 7, 2016 My 'displayHeaderMiddle' isn't showing up in the transplant to select menu. It's also not in the database. It's as if... public function registerDisplayHeaderMiddleHook() { return $this->registerHook('displayHeaderMiddle'); } isn't getting fired. Link to comment Share on other sites More sharing options...
jellyMan Posted October 7, 2016 Author Share Posted October 7, 2016 if i do this... public function install() { return parent::install() && $this->registerHook('displayHeaderMiddle'); } And then go to Modules in the admin and uninstall/install the module then the hook get's registered as expected. But I want to avoid doing this. Link to comment Share on other sites More sharing options...
rocky Posted October 8, 2016 Share Posted October 8, 2016 An easier solution is to use the following in a TPL file: {hook h="displayNav" mod="blockcontact"} Link to comment Share on other sites More sharing options...
jellyMan Posted October 10, 2016 Author Share Posted October 10, 2016 On 10/8/2016 at 2:08 AM, rocky said: An easier solution is to use the following in a TPL file: {hook h="displayNav" mod="blockcontact"} So I guess this loads the hookDisplayNav method on the blockcontact module right? I'm very new to prestashop so sorry for the n00b questions. 1 Link to comment Share on other sites More sharing options...
rocky Posted October 10, 2016 Share Posted October 10, 2016 That's right. 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