diff --git a/core/includes/ajax.inc b/core/includes/ajax.inc index 8817356..54d13a4 100644 --- a/core/includes/ajax.inc +++ b/core/includes/ajax.inc @@ -289,9 +289,9 @@ function ajax_render($commands = array()) { } // Now add a command to merge changes and additions to Drupal.settings. - $scripts = drupal_add_js(); - if (!empty($scripts['settings'])) { - $settings = $scripts['settings']; + $javascript_settings = drupal_js_settings(); + if (!empty($javascript_settings['settings'])) { + $settings = $javascript_settings['settings']; array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE)); } diff --git a/core/includes/common.inc b/core/includes/common.inc index 353a9b5..8cffa60 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4102,65 +4102,78 @@ function drupal_add_js($data = NULL, $options = NULL) { $options['weight'] += count($javascript) / 1000; if (isset($data)) { - // Add jquery.js and drupal.js, as well as the basePath setting, the - // first time a JavaScript file is added. - if (empty($javascript)) { - // url() generates the prefix using hook_url_outbound_alter(). Instead of - // running the hook_url_outbound_alter() again here, extract the prefix - // from url(). - url('', array('prefix' => &$prefix)); - $javascript = array( - 'settings' => array( - 'data' => array( - array('basePath' => base_path()), - array('pathPrefix' => empty($prefix) ? '' : $prefix), - ), - 'type' => 'setting', - 'scope' => 'header', - 'group' => JS_SETTING, - 'every_page' => TRUE, - 'weight' => 0, - 'browsers' => array(), - ), - 'core/misc/drupal.js' => array( - 'data' => 'core/misc/drupal.js', - 'type' => 'file', - 'scope' => 'header', - 'group' => JS_LIBRARY, - 'every_page' => TRUE, - 'weight' => -1, - 'preprocess' => TRUE, - 'cache' => TRUE, - 'defer' => FALSE, - 'browsers' => array(), - ), - ); - // Register all required libraries. - drupal_add_library('system', 'jquery', TRUE); - drupal_add_library('system', 'jquery.once', TRUE); - drupal_add_library('system', 'html5shiv', TRUE); + // Add jquery.js and drupal.js the first time a JavaScript file is added. + if ($options['type'] == 'setting') { + // All JavaScript settings are placed in the header of the page with + // the library weight so that inline scripts appear afterwards. + drupal_js_settings($data); } + else { + if (empty($javascript)) { + // url() generates the prefix using hook_url_outbound_alter(). Instead of + // running the hook_url_outbound_alter() again here, extract the prefix + // from url(). + url('', array('prefix' => &$prefix)); + $javascript = array( + 'core/misc/drupal.js' => array( + 'data' => 'core/misc/drupal.js', + 'type' => 'file', + 'scope' => 'header', + 'group' => JS_LIBRARY, + 'every_page' => TRUE, + 'weight' => -1, + 'preprocess' => TRUE, + 'cache' => TRUE, + 'defer' => FALSE, + 'browsers' => array(), + ), + ); + // Register all required libraries. + drupal_add_library('system', 'jquery', TRUE); + drupal_add_library('system', 'jquery.once', TRUE); + drupal_add_library('system', 'html5shiv', TRUE); + } - switch ($options['type']) { - case 'setting': - // All JavaScript settings are placed in the header of the page with - // the library weight so that inline scripts appear afterwards. - $javascript['settings']['data'][] = $data; - break; - - case 'inline': - $javascript[] = $options; - break; + switch ($options['type']) { + case 'inline': + $javascript[] = $options; + break; - default: // 'file' and 'external' - // Local and external files must keep their name as the associative key - // so the same JavaScript file is not added twice. - $javascript[$options['data']] = $options; + default: // 'file' and 'external' + // Local and external files must keep their name as the associative key + // so the same JavaScript file is not added twice. + $javascript[$options['data']] = $options; + } } } return $javascript; } +function drupal_js_settings($data = NULL) { + $js_settings = &drupal_static(__FUNCTION__, array()); + url('', array('prefix' => &$prefix)); + + if (isset($data)) { + if (empty($js_settings)) { + $js_settings['settings'] = array( + 'data' => array( + array('basePath' => base_path()), + array('pathPrefix' => empty($prefix) ? '' : $prefix), + ), + 'type' => 'setting', + 'scope' => 'header', + 'group' => JS_SETTING, + 'every_page' => TRUE, + 'weight' => 0, + 'browsers' => array(), + ); + } + $js_settings['settings']['data'][] = $data; + } + + return $js_settings; +} + /** * Constructs an array of the defaults that are used for JavaScript items. * @@ -4227,6 +4240,9 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS return ''; } + // If we have valid javascript files then lets also merge in the settings. + $javascript = array_merge(drupal_js_settings(), $javascript); + // Allow modules to alter the JavaScript. if (!$skip_alter) { drupal_alter('js', $javascript);