Using hooks in prestashop 1.6.x and 1.7.x – continued

Prestashop 1.6 and 1.7 have the ability to register a hook in the loadable module code. Let’s consider an example – let’s create a hook that will be such a Smarty template of the source code theme:
1 |
{hook h='displayPrimerHook' mod='primermodule'} |
Where displayPrimerHook – hook name, primermodule – module name.
For this hook processing option, when installing the module, you need to register the hook in the php code:
1 2 3 4 5 6 7 8 |
public function install() { if (parent::install() && $this->registerHook('displayPrimerHook')) { MODULE CODE TO CONTINUE INSTALLATION } return false; } |
Hook registration happens exactly here:
1 |
$this->registerHook('displayPrimerHook'); |
And create a hook function handler:
1 2 3 4 |
public function hookdisplayPrimerHook() { HOOK HANDLING CODE } |
After installing the module and calling the code from the Smarty template:
1 |
{hook h='displayPrimerHook' mod='primermodule'} |
It will be processed according to the handler function code.
You can also call a registered hook in such a dynamic way from the php code:
1 |
Hook::exec('displayPrimerHook'); |
or from Smarty template:
1 |
{hook h='displayPrimerHook'} |
The code that is called by the hook must also be defined as a function.
For version prestashop 1.7, it was possible to register a hook in the theme configuration file theme.yml in hooks block. You can also write modules for this hook there, it looks like this:
To call a hook from a Smarty template, use this form:
1 |
{hook h='displayPrimerHook'} |
Registering a hook via a config file works when the theme is installed.