Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Display hooks

A display hook is a place in a page template where modules can insert their HTML. The names of such hooks start with display: displayHome is the home page, displayFooter is the footer. A module subscribes to a hook and writes a method that returns a string.

Subscribing to a hook

The subscription is made on module install with a registerHook() call:

// modules/acmehello/acmehello.php
public function install(): bool
{
    return parent::install()
        && $this->registerHook('displayHome');
}

The engine records in the database that the acmehello module answers the displayHome hook.

The handler

The method is named hook plus the hook name with a capital letter:

// modules/acmehello/acmehello.php
public function hookDisplayHome(array $params): string
{
    return '<p class="acme-hello">Welcome!</p>';
}

The string the method returns lands on the home page — where the theme calls this hook.

How to see the result

The module is already installed, so install() will not run again. Reset the module:

php bin/console prestashop:module reset acmehello

Open the storefront home page: the line "Welcome!" appears below the products.

Where a hook comes from

A hook is called by a theme template. In the file themes/classic/templates/index.tpl the home page prints a variable into which the engine has collected the answers of all modules to displayHome. In other templates a hook is called right in place:

{* themes/classic/templates/catalog/product.tpl *}
{hook h='displayFooterProduct' product=$product category=$category}

The product and category attributes end up in the $params array of the handler.

A hook on the product page

Reviews will be shown below the product description. Add a second subscription and a second method:

// modules/acmehello/acmehello.php
public function install(): bool
{
    return parent::install()
        && $this->registerHook('displayHome')
        && $this->registerHook('displayFooterProduct');
}
 
public function hookDisplayFooterProduct(array $params): string
{
    $productId = (int) Tools::getValue('id_product');
 
    return '<p>Reviews of product #' . $productId . ' will appear here.</p>';
}

Tools::getValue() reads a request parameter: on a product page it holds the product id. After resetting the module open any product — a line with its id appears at the bottom of the page.

Common display hooks

HookWhere it is shown
displayHeaderInside the head tag
displayTopThe site header
displayHomeThe home page
displayFooterThe footer
displayLeftColumn, displayRightColumnSide columns
displayProductAdditionalInfoThe product page, under the buy button
displayFooterProductThe product page, under the description
displayShoppingCartThe cart page
displayOrderConfirmationThe "Thank you for your order" page
displayCustomerAccountThe customer account, the list of sections

The full list and the order of modules on each hook are shown on the back office page "Design → Positions". There a module can also be attached to a hook or detached without code.

Your own hook

A module can declare its own extension point so that other modules add something to its block. In the module template you write {hook h='displayAcmeHelloAfterReviews'} — you invent the name yourself, starting it with display.

Common mistake

You added a new registerHook() call to install(), but the hook method does not fire. The reason: the module is already installed, and install() does not run any more. Reset the module with prestashop:module reset or attach the hook by hand on the "Design → Positions" page.

Thanks for reading!

Donate to the site — we work hard for you.