【问题标题】:Drupal 7 override jquery js file in custom themeDrupal 7 在自定义主题中覆盖 jquery js 文件
【发布时间】:2014-04-06 21:32:37
【问题描述】:

是否可以重写/覆盖自定义模板脚本变量中使用的默认 Drupal 7.26 jquery?

我的意思是其中一个 js 文件:

<script type="text/javascript" src="http://drupal.dev/misc/jquery.js?v=1.4.4"></script>

通过自定义主题来的?

我在sites/all/MYTPL/template.php 中尝试过,但它不起作用:

$scripts['misc/jquery.js']['data'] = $base_theme . '/js/packages/jquery-2.1.0.min.js';

我想知道是否有人在不使用任何模块(例如jQuery Update)的情况下管理它?

=== 更新 ===

实际上,我通过接受的答案解决了这个问题,并根据@cojomojo 的答案进行了一些修改:

function THEMEname_js_alter(&$javascript) {
    // Swap out jQuery to use an updated version of the library.
    $javascript['misc/jquery.js']['data'] = drupal_get_path('theme', 'THEMEname') . '/js/packages/jquery-2.1.0.min.js';
}

【问题讨论】:

    标签: javascript php jquery drupal drupal-7


    【解决方案1】:

    在您主题的template.php 文件中使用此代码,在hook_js_alter 中。

    function [YOUR_THEME]_js_alter(&$js)
    {
        $jqKey = "my-new-jquery"; // a key for new jquery file entry
        $js[$jqKey] = $js['misc/jquery.js']; // copy the default jquery settings, so you don't have to re-type them.
        $js[$jqKey]['data'] = "https://code.jquery.com/jquery-2.1.0.min.js"; // the path for new jquery file.
        $js[$jqKey]['version'] = '2.1.0'; // your jquery version.
    
        unset($js['misc/jquery.js']); // delete drupal's default jquery file.
    }
    

    【讨论】:

      【解决方案2】:

      所以你有几个选择来做到这一点。他们在这里:

      你可以像这样使用 hook_js_alter:

      <?php
      function hook_js_alter(&$javascript) {
        // Swap out jQuery to use an updated version of the library.
       $javascript['misc/jquery.js']['data'] =         drupal_get_path('module', 'jquery_update') . '/jquery.js';
      }
      ?>
      

      或者为了避免像提到的 2pha 这样的损坏,您可以并排运行两个版本的 jquery。 jQuery 已经具有与不同版本的 jQuery 一起运行的功能(或者,实际上,与使用 $ 符号作为函数或变量的任何其他 JavaScript 库一起运行)。这是 noConflict() 函数。您可以在此处查看 API 页面:http://api.jquery.com/jQuery.noConflict/

      要在您的项目中使用此功能,只需告诉浏览器您的新 jQuery 库,并且您将通过自定义 noConflict 标识符引用它。这是一个如何在自定义主题中实现此功能的示例。

      MyTheme/page.tpl.php

      <head>
        <title><?php print $head_title; ?></title>
        <?php print $head; ?>
        <?php print $styles; ?>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">
          var $jq = jQuery.noConflict();
        </script>
        <?php print $scripts; ?>
      </head>
      

      另一种可能的方法是将 jQuery 与预处理页面交换,如下所示:

      <?php
      function yourModuleOrThemeName_preprocess_page(&$variables) {
        // exclude backend pages to avoid core js not working anymore
        // you could also just use a backend theme to avoid this
        if (arg(0) != 'admin' || !(arg(1) == 'add' && arg(2) == 'edit') || arg(0) != 'panels' || arg(0) != 'ctools') {
          $scripts = drupal_add_js();
          $new_jquery = array(drupal_get_path('theme', 'YOURTHEME') . '/js/jquery-1.7.1.min.js' => $scripts['core']['misc/jquery.js']);
          $scripts['core'] = array_merge($new_jquery, $scripts['core']);
          unset($scripts['core']['misc/jquery.js']);
          $variables['scripts'] = drupal_get_js('header', $scripts);
        }
      }
      ?>
      

      对我来说,最好的方法是并排运行两个版本的 jQuery(Drupal 7 默认使用的版本以及您的主题需要的任何版本)或将 jQuery 与预处理页面交换。 hook_js_alter 有可能破坏任何需要不同版本的 jQuery 的东西。完成您所要求的所有潜在方法都在这里:https://drupal.org/node/1058168

      【讨论】:

      • 我在hook_js_alter() 中使用您的第一个代码,但它不起作用,我应该在声明后立即调用该函数,对吗?如果是这样,为什么在那个函数里面有 drupal_get_path('module', 'jquery_update') ?我的jquery.js 文件位于/sites/all/themes/MYTPL/js/jquery-2.1.0.min.js 内。
      【解决方案3】:

      也许使用钩子hook_js_alter
      请记住,尽管其他模块将依赖于 jQuery,如果它们使用的东西在版本之间发生了变化,您可能会遇到问题。

      【讨论】:

      • hook_js_alter 仅适用于 jQuery 更新模块,不是吗?
      • cojomojo 有你要找的详细答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      相关资源
      最近更新 更多