Search code examples
phptwigprestashopprestashop-1.7

Display a custom hook in the Back Office in Prestashop 1.7


I am trying to display some content in the default product table of Prestashop. I already managed to override the twig template {% extends 'PrestaShopBundle:Admin/Product/CatalogPage/Lists:list.html.twig' %}.

I verified it by typing a random string in the overriden template and it successfully appeared on the product list.

After that, I created a custom hook in my module with:

$this->registerHook('displayModProfit');

I verified that the hook was registered in the ps_hook database. It was.

I added a method in my module in order to display a test message with the hook. The method is called hookDisplayModProfit.

Then I used {{ renderhook('displayModProfit') }} in the overriden list.html.twig template.

However, it didn't work. Nothing was shown in the spot I wanted the test message to appear. I checked the database and realized that my module is not attached to the hook. I can't find the hook in the Back Office "Positions" tab either.

I read up a bit on this and people suggested adding a custom_hooks entry in the theme.yml file. The problem is, I am not making a theme specific hook. I am making a Back Office hook. How can I attach my module to it?


Solution

  • It seems for me that you just forgot to reset your module after custom hook addition. It is necessary to register your hook in the system. Or maybe you didn't add return statement in your method hookDisplayModProfit.

    If you follow the next instructions correctly everything should work like a charm

    1. Override your twig template file. Put a file with the same name in the correct directory inside your module. In your case, it is your_module/views/PrestaShop/Admin/Product/CatalogPage/Lists/list.html.twig and extend the original file {% extends 'PrestaShopBundle:Admin/Product/CatalogPage/Lists:list.html.twig' %}. Do not forget that we can only extend a block which exists in the original template. For example:
    {% block product_catalog_form_table_row %}
        {{ renderhook('displayModProfit') }}
    {% endblock %}
    
    1. In your module main php file add $this->registerHook('displayModProfit') to the install method.
    public function install()
    {
        return parent::install() && $this->registerHook('displayModProfit');
    }
    
    1. In your main php file add a method hookDisplayModProfit with a return statement
    public function hookDisplayModProfit()
    {
        return 'Hello world!';
    }
    
    1. Reset your module to register the new hook.