官术网_书友最值得收藏!

Triggering hooks

In the PrestaShop source code, you will find two types of hook triggers:

  • In .php file, it will be the Hook::exec('hookName') method
  • In .tpl file, it will be the {hook h='hookName'} Smarty function

In our case, in /classes/controllers/ProductController.php, you will find the following code:

Hook::exec('displayProductTabContent');

This function will execute all the functions named hookDisplayProductTabContent of the modules attached to this hook in the order defined by the positions we talked about earlier.

The return value of each function will be concatenated and returned by the Hook::exec function. The displayProductTabContent hook is generally used to display blocks at the bottom of the product page. If we look closer at the line containing the trigger (I simplified the following lines on purpose to focus on the essentials), we will see this:

$this->context->smarty->assign(array('HOOK_PRODUCT_TAB' => Hook::exec('displayProductTab', array('product' => $this->product)),'HOOK_PRODUCT_TAB_CONTENT' => Hook::exec ('displayProductTabContent',array('product' => $this->product)),));

So, in this case, each module will return HTML code and the result will be directly assigned to the Smarty templates.

Note

When a hook is called, some parameters can be passed on, for example:

Hook::exec('displayExample', array('val1' => 23, 'val2' => 'Hello'));

These are automatically passed as an array parameter to the function called by the hook. In this example, if a module that is attached to this hook has the following method:

public function hookDisplayExample($params)
{
  print_r($params);
}

Then, this function will display:

Array ( [val1] => 23 [val2] => hello )
主站蜘蛛池模板: 金沙县| 墨竹工卡县| 宜章县| 嘉鱼县| 齐齐哈尔市| 杭州市| 娄烦县| 绥棱县| 巴彦淖尔市| 周至县| 旬邑县| 宁城县| 耒阳市| 蒙阴县| 望谟县| 弋阳县| 临高县| 永定县| 门源| 黄大仙区| 鄯善县| 桑植县| 西贡区| 克什克腾旗| 子长县| 互助| 民勤县| 周口市| 拜城县| 城市| 长丰县| 肇州县| 四川省| 金秀| 丹东市| 永济市| 禄劝| 甘洛县| 郓城县| 化州市| 兴义市|