diff options
-rw-r--r-- | includes/common.inc | 129 | ||||
-rw-r--r-- | includes/form.inc | 7 | ||||
-rw-r--r-- | modules/color/color.module | 28 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 14 | ||||
-rw-r--r-- | modules/system/system.module | 13 |
5 files changed, 122 insertions, 69 deletions
diff --git a/includes/common.inc b/includes/common.inc index 05a96512d..cabcf7dfa 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3120,6 +3120,67 @@ function drupal_get_js($scope = 'header', $javascript = NULL) { } /** + * Adds all the attached libraries, JavaScript and CSS to the page. + * + * @param $libraries + * An array of depending libraries to be added. + * @param $js + * An array of JavaScript components to add. + * @param $css + * An array of cascading stylesheets to add. + * @param $weight + * The default weight of JavaScript and CSS being added. This is only applied + * to the stylesheets and JavaScript items that don't have an explicit weight + * assigned to them. + * @param $dependency_check + * When TRUE, will exit if a given library's dependencies are missing. When + * set to FALSE, will continue to add the libraries, even though one of the + * dependencies are missing. Defaults to FALSE. + * @return + * Will return FALSE if there were any missing library dependencies. TRUE will + * be returned if all library dependencies were met. + * + * @see drupal_add_library(), drupal_render() + */ +function drupal_process_attached(array $libraries = array(), array $js = array(), array $css = array(), $weight = JS_DEFAULT, $dependency_check = FALSE) { + // Add the libraries first. + $success = TRUE; + foreach ($libraries as $library) { + if (drupal_add_library($library[0], $library[1]) === FALSE) { + $success = FALSE; + // Exit if the dependency is missing. + if ($dependency_check) { + return $success; + } + } + } + + // Add both the JavaScript and the CSS. + foreach (array('js' => $js, 'css' => $css) as $type => $items) { + foreach ($items as $data => $options) { + // If the value is not an array, it's a filename and passed as first + // (and only) argument. + if (!is_array($options)) { + $data = $options; + $options = NULL; + } + // In some cases, the first parameter ($data) is an array. Arrays can't be + // passed as keys in PHP, so we have to get $data from the value array. + if (is_numeric($data)) { + $data = $options['data']; + unset($options['data']); + } + // Apply the default weight if the weight isn't explicitly given. + if (!isset($options['weight'])) { + $options['weight'] = $weight; + } + call_user_func('drupal_add_' . $type, $data, $options); + } + } + return $success; +} + +/** * Adds multiple JavaScript or CSS files at the same time. * * A library defines a set of JavaScript and/or CSS files, optionally using @@ -3145,43 +3206,16 @@ function drupal_add_library($module, $name) { $added = &drupal_static(__FUNCTION__, array()); // Only process the library if it exists and it was not added already. - if (!isset($added[$module][$name]) && $library = drupal_get_library($module, $name)) { - // Prevent repeated/recursive processing. - $added[$module][$name] = TRUE; - - // Ensure dependencies first. - foreach ($library['dependencies'] as $dependency) { - if (drupal_add_library($dependency[0], $dependency[1]) === FALSE) { - // If any dependent library could not be added, this library will break; - // stop here. - $added[$module][$name] = FALSE; - return FALSE; - } + if (!isset($added[$module][$name])) { + if ($library = drupal_get_library($module, $name)) { + // Add all components within the library. + $added[$module][$name] = drupal_process_attached($library['dependencies'], $library['js'], $library['css'], JS_LIBRARY, TRUE); } - - // Add defined JavaScript. - foreach ($library['js'] as $data => $options) { - // For JS settings we need to transform $options['data'] into $data. - if (isset($options['type'], $options['data']) && $options['type'] == 'setting') { - $data = $options['data']; - unset($options['data']); - } - // If not specified, assign a default weight of JS_LIBRARY. - elseif (!isset($options['weight'])) { - $options['weight'] = JS_LIBRARY; - } - drupal_add_js($data, $options); - } - - // Add defined stylesheets. - foreach ($library['css'] as $data => $options) { - drupal_add_css($data, $options); + else { + // Requested library does not exist. + $added[$module][$name] = FALSE; } } - // Requested library does not exist. - else { - $added[$module][$name] = FALSE; - } return $added[$module][$name]; } @@ -3996,27 +4030,12 @@ function drupal_render(&$elements) { } } - // Add additional CSS and JavaScript files associated with this element. - foreach (array('css', 'js') as $kind) { - if (!empty($elements['#attached_' . $kind]) && is_array($elements['#attached_' . $kind])) { - foreach ($elements['#attached_' . $kind] as $data => $options) { - // If the value is not an array, it's a filename and passed as first - // (and only) argument. - if (!is_array($options)) { - $data = $options; - $options = NULL; - } - // When drupal_add_js with 'type' => 'setting' is called, the first - // parameter ($data) is an array. Arrays can't be keys in PHP, so we - // have to get $data from the value array. - if (is_numeric($data)) { - $data = $options['data']; - unset($options['data']); - } - call_user_func('drupal_add_' . $kind, $data, $options); - } - } - } + // Add additional libraries, CSS and JavaScript associated with this element. + drupal_process_attached( + isset($elements['#attached_library']) ? $elements['#attached_library'] : array(), + isset($elements['#attached_js']) ? $elements['#attached_js'] : array(), + isset($elements['#attached_css']) ? $elements['#attached_css'] : array() + ); $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : ''; $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : ''; diff --git a/includes/form.inc b/includes/form.inc index e9030efe6..b57b7c816 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -1594,7 +1594,7 @@ function form_get_options($element, $key) { */ function theme_fieldset($element) { if (!empty($element['#collapsible'])) { - drupal_add_js('misc/collapse.js'); + $element['#attached_js']['misc/collapse.js'] = array(); if (!isset($element['#attributes']['class'])) { $element['#attributes']['class'] = array(); @@ -2215,7 +2215,7 @@ function form_process_fieldset(&$element, &$form_state) { $element['#group_members'] = &$form_state['groups'][$parents]; // Contains form element summary functionalities. - drupal_add_js('misc/form.js', array('weight' => JS_LIBRARY + 1)); + $element['#attached_js']['misc/form.js'] = array('weight' => JS_LIBRARY + 1); return $element; } @@ -2300,8 +2300,7 @@ function form_process_vertical_tabs($element, &$form_state) { */ function theme_vertical_tabs($element) { // Add required JavaScript and Stylesheet. - drupal_add_js('misc/vertical-tabs.js', array('weight' => JS_DEFAULT - 1)); - drupal_add_css('misc/vertical-tabs.css'); + drupal_add_library('system', 'vertical-tabs'); return '<div class="vertical-tabs-panes">' . $element['#children'] . '</div>'; } diff --git a/modules/color/color.module b/modules/color/color.module index d3ddeadc1..2cefe7d43 100644 --- a/modules/color/color.module +++ b/modules/color/color.module @@ -154,16 +154,6 @@ function color_scheme_form(&$form_state, $theme) { $base = drupal_get_path('module', 'color'); $info = color_get_info($theme); - // Add Farbtastic color picker. - drupal_add_library('system', 'farbtastic'); - - // Add custom CSS and JS. - drupal_add_css($base . '/color.css', array('preprocess' => FALSE)); - drupal_add_js($base . '/color.js'); - drupal_add_js(array('color' => array( - 'reference' => color_get_palette($theme, TRUE) - )), 'setting'); - // See if we're using a predefined scheme. $current = implode(',', variable_get('color_' . $theme . '_palette', array())); // Note: we use the original theme when the default scheme is chosen. @@ -176,6 +166,24 @@ function color_scheme_form(&$form_state, $theme) { '#title' => t('Color set'), '#options' => $info['schemes'], '#default_value' => $current, + // Add Farbtastic color picker. + '#attached_library' => array( + array('system', 'farbtastic'), + ), + // Add custom CSS. + '#attached_css' => array( + $base . '/color.css' => array('preprocess' => FALSE), + ), + // Add custom JavaScript. + '#attached_js' => array( + $base . '/color.js', + array( + 'data' => array('color' => array( + 'reference' => color_get_palette($theme, TRUE), + )), + 'type' => 'setting', + ), + ), ); // Add palette fields. diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 13aad21e0..ad565b1ab 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -857,6 +857,20 @@ class JavaScriptTestCase extends DrupalWebTestCase { $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, 'unknown') === FALSE, t('Unknown library was not added to the page.')); } + + /** + * Tests the addition of libraries through the #attached_library property. + */ + function testAttachedLibrary() { + $element = array( + '#attached_library' => array( + array('system', 'farbtastic'), + ) + ); + drupal_render($element); + $scripts = drupal_get_js(); + $this->assertTrue(strpos($scripts, 'misc/farbtastic/farbtastic.js'), t('The attached_library property adds the additional libraries.')); + } } /** diff --git a/modules/system/system.module b/modules/system/system.module index 274558e0d..a4c0a600d 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -931,6 +931,19 @@ function system_library() { ), ); + // Vertical Tabs. + $libraries['vertical-tabs'] = array( + 'title' => 'Vertical Tabs', + 'website' => 'http://drupal.org/node/323112', + 'version' => '1.0', + 'js' => array( + 'misc/vertical-tabs.js' => array(), + ), + 'css' => array( + 'misc/vertical-tabs.css' => array(), + ), + ); + // Farbtastic. $libraries['farbtastic'] = array( 'title' => 'Farbtastic', |