有没有办法覆盖任何/js/*.js文件?

时间:2017-03-16 21:21:29

标签: override prestashop prestashop-1.6

我知道有一些方法可以覆盖模块的js / css / tpl和其他文件,但我找不到覆盖的方法,例如/js/validate.js文件。 我已经尝试将自己的文件放到/themes/my-theme/js文件夹中,但我看不到结果(validate.js仍然从/js/文件夹加载。)

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

没有内置功能来执行此操作。

但您可以覆盖addMedia的方法FrontController

创建此文件/override/classes/controller/FrontController.php

<?php
class FrontController extends FrontControllerCore
{
    /**
     * Adds a media file(s) (CSS, JS) to page header
     *
     * @param string|array $media_uri Path to file, or an array of paths like: array(array(uri => media_type), ...)
     * @param string|null $css_media_type CSS media type
     * @param int|null $offset
     * @param bool $remove If True, removes media files
     * @param bool $check_path If true, checks if files exists
     * @return true|void
     */
    public function addMedia($media_uri, $css_media_type = null, $offset = null, $remove = false, $check_path = true)
    {
        if (!is_array($media_uri)) {
            if ($css_media_type) {
                $media_uri = array($media_uri => $css_media_type);
            } else {
                $media_uri = array($media_uri);
            }
        }

        $list_uri = array();
        foreach ($media_uri as $file => $media) {
            if (!Validate::isAbsoluteUrl($media)) {
                $different = 0;
                $different_css = 0;
                $type = 'css';
                if (!$css_media_type) {
                    $type = 'js';
                    $file = $media;
                }
                if (strpos($file, __PS_BASE_URI__.'modules/') === 0) {
                    $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/'.$type.'/modules/', $file, $different);
                    if (strrpos($override_path, $type.'/'.basename($file)) !== false) {
                        $override_path_css = str_replace($type.'/'.basename($file), basename($file), $override_path, $different_css);
                    }

                    if ($different && @filemtime($override_path)) {
                        $file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/'.$type.'/modules/', $file, $different);
                    } elseif ($different_css && @filemtime($override_path_css)) {
                        $file = $override_path_css;
                    }
                    if ($css_media_type) {
                        $list_uri[$file] = $media;
                    } else {
                        $list_uri[] = $file;
                    }
                // Here we add our test to override default js files
                } elseif (strpos($file, _PS_JS_DIR_) === 0) {
                    $override_path = str_replace(_PS_JS_DIR_, _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/default/', $file, $different);
                    if (strrpos($override_path, $type.'/'.basename($file)) !== false) {
                        $override_path_css = str_replace($type.'/'.basename($file), basename($file), $override_path, $different_css);
                    }

                    if ($different && @filemtime($override_path)) {
                        $file = str_replace(_PS_JS_DIR_, __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/default/', $file, $different);
                    } elseif ($different_css && @filemtime($override_path_css)) {
                        $file = $override_path_css;
                    }
                    if ($css_media_type) {
                        $list_uri[$file] = $media;
                    } else {
                        $list_uri[] = $file;
                    }
                } else {
                    $list_uri[$file] = $media;
                }
            } else {
                $list_uri[$file] = $media;
            }
        }

        if ($remove) {
            if ($css_media_type) {
                return parent::removeCSS($list_uri, $css_media_type);
            }
            return parent::removeJS($list_uri);
        }

        if ($css_media_type) {
            return parent::addCSS($list_uri, $css_media_type, $offset, $check_path);
        }

        return parent::addJS($list_uri, $check_path);
    }
}

使用此覆盖,您现在可以创建文件/themes/my_theme/js/default/validate.js以覆盖默认的/js/validate.js文件。

PS:与任何覆盖一样,您必须删除文件/cache/class_index.php