Drupal 7 overrides jquery js file in custom theme

Is it possible to overwrite / override jQuery Drupal 7.26 by default, used in a variable of custom template templates?

I mean one of the js files:

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

what comes on a special topic?

I tried this in sites/all/MYTPL/template.php, but it does not work:

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

I would like to know if anyone could manage it without using any modules, such as jQuery Update?

=== UPDATED ===

Actually I solved it with the accepted answer with some changes based on @cojomojo answer:

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';
}
+4
source share
3 answers

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 default jquery file.
}
+8

, . :

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

+4

Maybe use hook hook_js_alter .
Keep in mind that other modules will depend on jQuery, and if they use something that has changed between versions, you may run into problems.

+2
source

Source: https://habr.com/ru/post/1535283/


All Articles