diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-11-23 16:00:08 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2008-11-23 16:00:08 +0000 |
commit | 1b4dd805ca9ebcf34815f480533a1069ba63b7e3 (patch) | |
tree | b38a60a5fd251f5583b449c5ce180cb42024d3a7 /includes | |
parent | 25f63a896578c8e342acfe7eebcd4e21cf982349 (diff) | |
download | brdo-1b4dd805ca9ebcf34815f480533a1069ba63b7e3.tar.gz brdo-1b4dd805ca9ebcf34815f480533a1069ba63b7e3.tar.bz2 |
#315801 by Rob Loach, Grugnoh2, mfer and dmitrig01: Add a hook_js_alter() to modify JavaScript being printed to the page.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/includes/common.inc b/includes/common.inc index 76eac625b..ad04138c7 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -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; @@ -2305,6 +2298,26 @@ function drupal_add_js($data = NULL, $options = NULL, $reset = FALSE) { } /** + * 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. * * References to JavaScript files are placed in a certain order: first, all @@ -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) { |