Commit 1b4dd805 authored by webchick's avatar webchick

#315801 by Rob Loach, Grugnoh2, mfer and dmitrig01: Add a hook_js_alter() to...

#315801 by Rob Loach, Grugnoh2, mfer and dmitrig01: Add a hook_js_alter() to modify JavaScript being printed to the page.
parent 25f63a89
......@@ -2232,15 +2232,8 @@ function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) {
else {
$options = array();
}
$options += array(
'type' => 'file',
'weight' => JS_DEFAULT,
'scope' => 'header',
'cache' => TRUE,
'defer' => FALSE,
'preprocess' => TRUE,
'data' => $data,
);
$options += drupal_js_defaults($data);
// Preprocess can only be set if caching is enabled.
$options['preprocess'] = $options['cache'] ? $options['preprocess'] : FALSE;
......@@ -2304,6 +2297,26 @@ function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) {
return $javascript;
}
/**
* Constructs an array of the defaults that are used for JavaScript items.
*
* @param $data
* (optional) The default data parameter for the JavaScript item array.
* @see drupal_get_js()
* @see drupal_add_js()
*/
function drupal_js_defaults($data = NULL) {
return array(
'type' => 'file',
'weight' => JS_DEFAULT,
'scope' => 'header',
'cache' => TRUE,
'defer' => FALSE,
'preprocess' => TRUE,
'data' => $data,
);
}
/**
* Returns a themed presentation of all JavaScript code for the current page.
*
......@@ -2312,6 +2325,13 @@ function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) {
* are added to the page. Then, all settings are output, followed by 'inline'
* JavaScript code. If running update.php, all preprocessing is disabled.
*
* Note that hook_js_alter(&$javascript) is called during this function call
* to allow alterations of the JavaScript during its presentation. Calls to
* drupal_add_js() from hook_js_alter() will not be added to the output
* presentation. The correct way to add JavaScript during hook_js_alter()
* is to add another element to the $javascript array, deriving from
* drupal_js_defaults(). See locale_js_alter() for an example of this.
*
* @param $scope
* (optional) The scope for which the JavaScript rules should be returned.
* Defaults to 'header'.
......@@ -2321,11 +2341,10 @@ function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) {
* @return
* All JavaScript code segments and includes for the scope as HTML tags.
* @see drupal_add_js()
* @see locale_js_alter()
* @see drupal_js_defaults()
*/
function drupal_get_js($scope = 'header', $javascript = NULL) {
if ((!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') && function_exists('locale_update_js_files')) {
locale_update_js_files();
}
if (!isset($javascript)) {
$javascript = drupal_add_js();
}
......@@ -2333,6 +2352,9 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
return '';
}
// Allow modules to alter the JavaScript.
drupal_alter('js', $javascript);
// Filter out elements of the given scope.
$items = array();
foreach ($javascript as $item) {
......
......@@ -505,21 +505,18 @@ function locale_system_update($components) {
}
/**
* Update JavaScript translation file, if required, and add it to the page.
* Implementation of hook_js_alter().
*
* This function checks all JavaScript files currently added via drupal_add_js()
* and invokes parsing if they have not yet been parsed for Drupal.t()
* and Drupal.formatPlural() calls. Also refreshes the JavaScript translation
* file if necessary, and adds it to the page.
*/
function locale_update_js_files() {
function locale_js_alter(&$javascript) {
global $language;
$dir = file_create_path(variable_get('locale_js_directory', 'languages'));
$parsed = variable_get('javascript_parsed', array());
// Get an array of all the JavaScript added so far.
$javascript = drupal_add_js();
$files = $new_files = FALSE;
foreach ($javascript as $item) {
......@@ -563,7 +560,9 @@ function locale_update_js_files() {
// Add the translation JavaScript file to the page.
if ($files && !empty($language->javascript)) {
drupal_add_js($dir . '/' . $language->language . '_' . $language->javascript . '.js');
// Add the translation JavaScript file to the page.
$file = $dir . '/' . $language->language . '_' . $language->javascript . '.js';
$javascript[$file] = drupal_js_defaults($file);
}
}
......
......@@ -210,10 +210,7 @@ function simpletest_test_form() {
function theme_simpletest_test_table($table) {
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
// Since SimpleTest is a special use case for the table select, stick the
// SimpleTest JavaScript above the table select.
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => JS_DEFAULT - 1));
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js');
// Create header for test selection table.
$header = array(
......@@ -301,6 +298,18 @@ function theme_simpletest_test_table($table) {
}
}
/**
* Implementation of hook_js_alter().
*/
function simpletest_js_alter(&$javascript) {
// Since SimpleTest is a special use case for the table select, stick the
// SimpleTest JavaScript above the table select.
$simpletest = drupal_get_path('module', 'simpletest') . '/simpletest.js';
if (array_key_exists($simpletest, $javascript) && array_key_exists('misc/tableselect.js', $javascript)) {
$javascript[$simpletest]['weight'] = $javascript['misc/tableselect.js']['weight'] - 1;
}
}
function theme_simpletest_result_summary($form, $text = NULL) {
return '<div class="simpletest-'. ($form['#ok'] ? 'pass' : 'fail') .'">' . _simpletest_format_summary_line($form) . '</div>';
}
......
......@@ -376,8 +376,8 @@ class JavaScriptTestCase extends DrupalWebTestCase {
* Implementation of setUp().
*/
function setUp() {
// Enable locale in test environment.
parent::setUp('locale');
// Enable Locale and SimpleTest in the test environment.
parent::setUp('locale', 'simpletest');
// Disable preprocessing
$this->preprocess_js = variable_get('preprocess_js', 0);
......@@ -488,6 +488,23 @@ class JavaScriptTestCase extends DrupalWebTestCase {
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'misc/collapse.js') < strpos($javascript, 'misc/jquery.js'), t('Rendering a JavaScript file above jQuery.'));
}
/**
* Test altering a JavaScript's weight via hook_js_alter().
*
* @see simpletest_js_alter()
*/
function testAlter() {
// Add both tableselect.js and simpletest.js, with a larger weight on SimpleTest.
drupal_add_js('misc/tableselect.js');
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => JS_THEME));
// Render the JavaScript, testing if simpletest.js was altered to be before
// tableselect.js. See simpletest_js_alter() to see where this alteration
// takes place.
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'simpletest.js') < strpos($javascript, 'misc/tableselect.js'), t('Altering JavaScript weight through the alter hook.'));
}
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment