Jump to content

Add a hook to an existing module


Recommended Posts

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

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

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

 

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

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

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

 

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.

  • Like 1
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...