Overriding kernel template files from a module

I am an add-on application developer for Prestashop. I have been trying to solve this for a long time, how do you properly override the template file from the module? At the moment I am overriding the whole file by copying the template file to the override folder from my module on install:

From:

/modules/<module>/views/templates/admin/products/informations.tpl

      

To:

/override/controllers/admin/templates/products/informations.tpl

      

Overriding the entire file just to add an input field seems pretty crude.

This works fine, but I'm worried because if the user installs any other module that wants to use the same file and it corrupts, or a new version of Prestashop changes the file and no longer supports the new version. In all respects, this looks like a dirty solution to a problem.

Is there a way to override only part of the file, and possibly directly from the module folder? Is there a better way to do this? How do you do it?

+3


source to share


2 answers


To override templates in PrestaShop, you need to override the method hookDisplayOverrideTemplate

from your module controller.

To register your hook, add it to your setup function:

$this->registerHook('DisplayOverrideTemplate');

now add your own version of the function to work with your .tpl file.



public function hookDisplayOverrideTemplate($params)
{
    $controllerName = get_class($params['controller']);
    $tpl = dirname(__FILE__) . '/views/templates/override/example.tpl';
    if ($controllerName == 'ExampleController' && file_exists($tpl)) 
        return $tpl;
    return false;
}

      

In my example, you need to put the template you want to override inside your folder /views/template/override

.

When the page loads, the front controller classes/controller/FrontController.php

calls the function hookDisplayOverrideTemplate

and if the template returns it will be loaded, otherwise it will load the default template.

I tested this and know it works for PrestaShop v1.5 and v1.6

+1


source


If your module needs to change the code in the .tpl file, it is better to do it in oryginal.tpl and put an IF-ELSE statement there, which will add your input field only if the module is enabled. You cannot avoid destroying these files when someone updates the script.



You might be thinking of some kind of JS solution that will add client side input.

0


source







All Articles