PrestaShop 1.7.4覆盖管理模板

时间:2018-10-04 12:13:13

标签: php prestashop prestashop-1.7

我正在开发自己的模块。

如何覆盖位于以下位置的模板: \ admin \ themes \ default \ template \ controllers \ customers \ helpers \ view \ view.tpl吗?

我在模块文件夹中创建了文件: \ modules \ my_module_name \ override \ controllers \ admin \ templates \ customers \ helpers \ view \ view.tpl。

安装模块时,文件夹\ override \ controllers \ admin \ templates \为空。

1 个答案:

答案 0 :(得分:1)

根据Prestashop DevDocs,我们无法通过将管理模板文件放在模块的override文件夹中直接覆盖模板。

  

不可能,也永远不会从模块中覆盖主题。如果   您需要这个,而必须查看父/子主题   功能。

您可以做的一件事是,可以将template个文件放在override文件夹中,并将这些文件复制到模块install/reset上,然后将这些文件删除在模块uninstall上。为此,我们可以在Prestashop默认提供的functioncopy函数中调用removeinstall()的覆盖和uninstall()的覆盖。

您需要在模块中执行下面提到的步骤,以覆盖管理模板。

1)添加需要在您的模块__construct()方法中覆盖的模板文件列表

__ construct()方法

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)将要覆盖的view.tpl文件添加到以下路径上的模块覆盖文件夹中。确保已在此文件中进行了更改。

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

3)修改模块类文件中的install()uninstall()方法。

安装方法

public function install()
{
    $addAdminTplOverrides = $this->_addAdminTplOverrides();

    return parent::install() && $addAdminTplOverrides /** Other hook you need to register + Method you need to call on install **/;
}

uninstall()方法

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)分别在_addAdminTplOverrides()_removeAdminTplOverrides()中调用install()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;
}

5)现在install/reset您的模块;您会看到管理模板现在已被覆盖。

从此处的第1步到第5步完成代码

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;
}

希望这些对您有所帮助!