Jump to content

[SOLVED] [Override] Add a new hook to an existing module ?


be_tnt

Recommended Posts

Hello,

 

I am trying to add a new hook to an existing module but it does not work. My prestashop version is 1.5.3.

 

So far I have done the following:

  1. Add the new hook in ps_hook:
    			INSERT INTO `ps_hook` (`name`, `title`, `description`)
    			VALUES ('displayToptop', 'Top top of the pages', 'Hooks before the top hook');
    


  2. Add a new alias in ps_hook_alias for this new hook:
    			INSERT INTO `ps_hook_alias` (`alias`, `name`)
    			VALUES ('toptop', 'displayToptop');
    


  3. Override the FrontController class:
    			class FrontController extends FrontControllerCore
    			{
    			  public function initContent()
    			  {
    			   parent::initContent();
    			   if ($this->context->getMobileDevice() == false)
    			   {
    				$this->context->smarty->assign('HOOK_TOPTOP', Hook::exec('displayToptop'));
    			   }
    			  }
    			}
    


  4. Override (at least try) the existing module. Here it's the blockuserinfo module.:
    			if (!defined('_PS_VERSION_'))
    			exit;	
    		class BlockUserInfo extends Module
    			{	public function install()
    			{
    			  return (parent::install() AND $this->registerHook('toptop'));
    			}	
    		/**
    			* Returns module content for header
    			*
    			* @param array $params Parameters
    			* @return string Content
    			*/
    			public function hookToptop($params)
    			{
    			  if (!$this->active)
    			   return;	  $this->smarty->assign(array(
    			   'cart' => $this->context->cart,
    			   'cart_qties' => $this->context->cart->nbProducts(),
    			   'logged' => $this->context->customer->isLogged(),
    			   'customerName' => ($this->context->customer->logged ? $this->context->customer->firstname.' '.$this->context->customer->lastname : false),
    			   'firstName' => ($this->context->customer->logged ? $this->context->customer->firstname : false),
    			   'lastName' => ($this->context->customer->logged ? $this->context->customer->lastname : false),
    			   'order_process' => Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order'
    			  ));
    			  return $this->display(__FILE__, 'blockuserinfotop.tpl');
    			}	
    		}
    


The new view has been created too. I have uninstall and re-install the module but when I try to hook this module to the new hook, it says: it's not possible to hook this module to this hook (or something equivalent as my backend is in french).

 

Did I do something wrong? Is it possible to add new hook to existing module? Should I completely redevelopped a new module based on the existing one?

 

Thx in advance!

Edited by be_tnt (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

you need to do this also:

 

public function hookToptop($params){

 return $this->hookTop($params);
}

 

so that it returns what the hook_top returns. If you want to have a different template to show your hook, you must do this:

 

public function hookToptop($params){

$this->hookTop($params);
return $this->display(__FILE__, 'my_blockuserinfo.tpl');

}

Link to comment
Share on other sites

In fact I do not want to display the content of the hookTop.

 

The only way to add a new hook in an existing module is to create a new one based on the existing one and modify the module class. There is no way right now to override a module class.

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