1 | <?php |
---|
2 | // $Id: jquery_update.module,v 1.5.2.2.2.8 2010/04/23 16:34:39 sun Exp $ |
---|
3 | |
---|
4 | /** |
---|
5 | * @file |
---|
6 | * Updates Drupal to use the latest version of jQuery. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * The path to the jQuery files that need to be replaced. |
---|
11 | */ |
---|
12 | define('JQUERY_UPDATE_REPLACE_PATH', drupal_get_path('module', 'jquery_update') .'/replace'); |
---|
13 | |
---|
14 | /** |
---|
15 | * Array of jQuery files to replace if jQuery is loaded. |
---|
16 | */ |
---|
17 | function jquery_update_get_replacements() { |
---|
18 | return array( |
---|
19 | 'module' => array( |
---|
20 | 'misc/farbtastic/farbtastic.js' => 'farbtastic.js', |
---|
21 | 'misc/teaser.js' => 'teaser.js', |
---|
22 | 'misc/jquery.form.js' => 'jquery.form.js', |
---|
23 | 'misc/ahah.js' => 'ahah.js', |
---|
24 | // Certain versions of Views re-add tabledrag.js as $type 'module'. |
---|
25 | 'misc/tabledrag.js' => 'tabledrag.js', |
---|
26 | ), |
---|
27 | 'core' => array( |
---|
28 | 'misc/tabledrag.js' => 'tabledrag.js', |
---|
29 | ), |
---|
30 | ); |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Implementation of hook_theme_registry_alter(). |
---|
35 | * |
---|
36 | * Make jQuery Update's page preprocess function run *after* everything else's, |
---|
37 | * so that a theme can't call drupal_get_js() and mess everything up. |
---|
38 | */ |
---|
39 | function jquery_update_theme_registry_alter(&$theme_registry) { |
---|
40 | if (isset($theme_registry['page'])) { |
---|
41 | if (count($theme_registry['page']['preprocess functions']) > 0) { |
---|
42 | // If jquery_update's preprocess function is there already, remove it. |
---|
43 | if ($key = array_search('jquery_update_preprocess_page', $theme_registry['page']['preprocess functions'])) { |
---|
44 | unset($theme_registry['page']['preprocess functions'][$key]); |
---|
45 | } |
---|
46 | } |
---|
47 | // Now tack it on at the end so it runs after everything else. |
---|
48 | $theme_registry['page']['preprocess functions'][] = 'jquery_update_preprocess_page'; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Implementation of moduleName_preprocess_hook(). |
---|
54 | * |
---|
55 | * Replace Drupal core's jquery.js with the new one from jQuery Update module. |
---|
56 | */ |
---|
57 | function jquery_update_preprocess_page(&$variables) { |
---|
58 | // Only do this for pages that have JavaScript on them. |
---|
59 | if (!empty($variables['scripts'])) { |
---|
60 | |
---|
61 | // Perform the logic if either jQuery Update's jquery.js is newer than core's. |
---|
62 | if (variable_get('jquery_update_replace', TRUE)) { |
---|
63 | // Get an array of all the JavaScript files loaded by Drupal on this page. |
---|
64 | $scripts = drupal_add_js(); |
---|
65 | |
---|
66 | // Replace jquery.js first. |
---|
67 | $new_jquery = array(jquery_update_jquery_path() => $scripts['core']['misc/jquery.js']); |
---|
68 | $scripts['core'] = array_merge($new_jquery, $scripts['core']); |
---|
69 | unset($scripts['core']['misc/jquery.js']); |
---|
70 | |
---|
71 | // Loop through each of the required replacements. |
---|
72 | foreach (jquery_update_get_replacements() as $type => $replacements) { |
---|
73 | foreach ($replacements as $find => $replace) { |
---|
74 | // If the file to replace is loaded on this page... |
---|
75 | if (isset($scripts[$type][$find])) { |
---|
76 | // Create a new entry for the replacement file, and unset the original one. |
---|
77 | $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace; |
---|
78 | $scripts[$type][$replace] = $scripts[$type][$find]; |
---|
79 | unset($scripts[$type][$find]); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | $variables['scripts'] = drupal_get_js('header', $scripts); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Return the version of jQuery that is installed. |
---|
91 | * |
---|
92 | * This can be used by other modules' hook_requirements() to ensure that the |
---|
93 | * proper version of jQuery is installed. |
---|
94 | * |
---|
95 | * @see version_compare |
---|
96 | */ |
---|
97 | function jquery_update_get_version($jquery_path = NULL) { |
---|
98 | $version = 0; |
---|
99 | $pattern = '# * jQuery JavaScript Library v([0-9\.a-z]+)#'; |
---|
100 | |
---|
101 | // No file is passed in so default to the file included with this module. |
---|
102 | if (is_null($jquery_path)) { |
---|
103 | $jquery_path = jquery_update_jquery_path(); |
---|
104 | } |
---|
105 | |
---|
106 | // Return the version provided by jQuery Update. |
---|
107 | $jquery = file_get_contents($jquery_path); |
---|
108 | if (preg_match($pattern, $jquery, $matches)) { |
---|
109 | $version = $matches[1]; |
---|
110 | } |
---|
111 | |
---|
112 | return $version; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Implementation of hook_flush_caches(). |
---|
117 | */ |
---|
118 | function jquery_update_flush_caches() { |
---|
119 | // Find the versions of jQuery provided by core and this module. |
---|
120 | $jquery_update_version = jquery_update_get_version(); |
---|
121 | $jquery_core_version = jquery_update_get_version('misc/jquery.js'); |
---|
122 | |
---|
123 | // Set a variable according to whether core's version needs to be replaced. |
---|
124 | $replace = version_compare($jquery_core_version, $jquery_update_version, '<'); |
---|
125 | variable_set('jquery_update_replace', $replace); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Implementation of hook_menu(). |
---|
130 | */ |
---|
131 | function jquery_update_menu() { |
---|
132 | $items['admin/settings/jquery_update'] = array( |
---|
133 | 'title' => 'jQuery Update', |
---|
134 | 'description' => 'Configure settings for jQuery Update module.', |
---|
135 | 'page callback' => 'drupal_get_form', |
---|
136 | 'page arguments' => array('jquery_update_settings'), |
---|
137 | 'access arguments' => array('administer site configuration'), |
---|
138 | ); |
---|
139 | return $items; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Admin settings form. |
---|
144 | */ |
---|
145 | function jquery_update_settings() { |
---|
146 | // Clear the javascript cache when the setting is updated and check version of jquery file. |
---|
147 | $form['#submit'][] = 'drupal_clear_js_cache'; |
---|
148 | $form['#submit'][] = 'jquery_update_flush_caches'; |
---|
149 | |
---|
150 | $form['jquery_update_compression_type'] = array( |
---|
151 | '#type' => 'radios', |
---|
152 | '#title' => t('Choose jQuery compression level'), |
---|
153 | '#options' => array( |
---|
154 | 'min' => t('Production (Minified)'), |
---|
155 | 'none' => t('Development (Uncompressed Code)'), |
---|
156 | ), |
---|
157 | '#default_value' => variable_get('jquery_update_compression_type', 'min'), |
---|
158 | ); |
---|
159 | |
---|
160 | return system_settings_form($form); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Return the path to the jQuery file. |
---|
165 | */ |
---|
166 | function jquery_update_jquery_path() { |
---|
167 | $jquery_file = array('none' => 'jquery.js', 'min' => 'jquery.min.js'); |
---|
168 | return JQUERY_UPDATE_REPLACE_PATH .'/'. $jquery_file[variable_get('jquery_update_compression_type', 'min')]; |
---|
169 | } |
---|