Search code examples
phpprestashopprestashop-1.7

PrestaShop 1.7.4 override admin template


I'm developing my own module.

How to override template located in: \admin\themes\default\template\controllers\customers\helpers\view\view.tpl ?

I created file in my module folder: \modules\my_module_name\override\controllers\admin\templates\customers\helpers\view\view.tpl .

When I install my module, folder \override\controllers\admin\templates\ is empty.


Solution

  • According to Prestashop DevDocs we can not override templates directly by putting admin template files in override folder in our module.

    Overriding a theme from a module is NOT possible, and never will. If you need this, you have to look instead at the parent/child theme feature.

    There one thing that you can do is you can put template files in override folder and copy these files on module install/reset and remove these files on module uninstall. To achieve this we can call our function to copy override and remove override in install() and uninstall() function that is provided by default in Prestashop.

    You need to do below mentioned step in you module to override admin templates.

    1) Add list of template file(s) that needs to be overridden in you modules __construct() method

    __construct() Method

    public function __construct()
    {
        // ..... your other code here ....
    
        // list of template(s) file that needs to be override
        $this->admin_tpl_overrides = array(
            implode(DIRECTORY_SEPARATOR, array('override', 'controllers', 'admin', 'templates', 'customers', 'helpers', 'view', 'view.tpl'))
        );
    }
    

    2) Add view.tpl file that you want to override in modules override folder on below path. Make sure have done your changes in this file.

    modules\{YOUR_MODULE_NAME}\override\controllers\admin\templates\customers\helpers\view

    3) Modify install() and uninstall() methods in your modules class file.

    Install Method

    public function install()
    {
        $addAdminTplOverrides = $this->_addAdminTplOverrides();
    
        return parent::install() && $addAdminTplOverrides /** Other hook you need to register + Method you need to call on install **/;
    }
    

    uninstall() Method

    public function uninstall()
    {
        $removeAdminTplOverrides = $this->_removeAdminTplOverrides();
    
        return parent::uninstall() && $removeAdminTplOverrides /** Other hook you need to un-register + Method you need to call on uninstall **/;
    }
    

    4) Called _addAdminTplOverrides() and _removeAdminTplOverrides() method in install() and uninstall() respectively; add these functions just after uninstall method.

    private function _addAdminTplOverrides() 
    {
        $module_override_path = $this->getLocalPath().DIRECTORY_SEPARATOR;
        $result = true;
        foreach ($this->admin_tpl_overrides as $admin_tpl_path) {
            $override_file_path = $module_override_path.$admin_tpl_path;
            $dest_override_file_path = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$admin_tpl_path;
    
            if(file_exists($override_file_path)) {
                if (!copy($override_file_path, $dest_override_file_path)) {
                    $result &= false;
                }
            } else {
                $result &= false;
            }
        }        
        return $result;
    }
    
    private function _removeAdminTplOverrides() 
    {
        $module_override_path = $this->getLocalPath().DIRECTORY_SEPARATOR;
        $result = true;
        foreach ($this->admin_tpl_overrides as $admin_tpl_path) {
            $dest_override_file_path = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$admin_tpl_path;            
            if(file_exists($dest_override_file_path)) {
                if (!unlink($dest_override_file_path)) {
                    $result &= false;
                }
            }
        }        
        return $result;
    }
    

    5) Now install/reset your module; you can see admin template is overridden now.

    Complete code from step 1 to 5 here

    public function __construct()
    {
        // ..... your other code here ....
    
        // list of template(s) file that needs to be override
        $this->admin_tpl_overrides = array(
            implode(DIRECTORY_SEPARATOR, array('override', 'controllers', 'admin', 'templates', 'customers', 'helpers', 'view', 'view.tpl'))
        );
    }
    
    public function install()
    {
        $addAdminTplOverrides = $this->_addAdminTplOverrides();
    
        return parent::install() && $addAdminTplOverrides /** Other hook you need to register + Method you need to call on install **/;
    }
    
    public function uninstall()
    {
        $removeAdminTplOverrides = $this->_removeAdminTplOverrides();
    
        return parent::uninstall() && $removeAdminTplOverrides /** Other hook you need to un-register + Method you need to call on uninstall **/;
    }
    
    private function _addAdminTplOverrides() 
    {
        $module_override_path = $this->getLocalPath().DIRECTORY_SEPARATOR;
        $result = true;
        foreach ($this->admin_tpl_overrides as $admin_tpl_path) {
            $override_file_path = $module_override_path.$admin_tpl_path;
            $dest_override_file_path = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$admin_tpl_path;
    
            if(file_exists($override_file_path)) {
                if (!copy($override_file_path, $dest_override_file_path)) {
                    $result &= false;
                }
            } else {
                $result &= false;
            }
        }        
        return $result;
    }
    
    private function _removeAdminTplOverrides() 
    {
        $module_override_path = $this->getLocalPath().DIRECTORY_SEPARATOR;
        $result = true;
        foreach ($this->admin_tpl_overrides as $admin_tpl_path) {
            $dest_override_file_path = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$admin_tpl_path;            
            if(file_exists($dest_override_file_path)) {
                if (!unlink($dest_override_file_path)) {
                    $result &= false;
                }
            }
        }        
        return $result;
    }
    

    Hope these will be helpful to you!