Fix for Fix for Foreach!

  1. /**
  2.  * Adds multiple JavaScript or CSS files at the same time.
  3.  *
  4.  * A library defines a set of JavaScript and/or CSS files, optionally using
  5.  * settings, and optionally requiring another library. For example, a library
  6.  * can be a jQuery plugin, a JavaScript framework, or a CSS framework. This
  7.  * function allows modules to load a library defined/shipped by itself or a
  8.  * depending module; without having to add all files of the library separately.
  9.  * Each library is only loaded once.
  10.  *
  11.  * @param $module
  12.  *   The name of the module that registered the library.
  13.  * @param $name
  14.  *   The name of the library to add.
  15.  * @return
  16.  *   TRUE when the library was successfully added or FALSE if the library or one
  17.  *   of its dependencies could not be added.
  18.  *
  19.  * @see drupal_get_library()
  20.  * @see hook_library()
  21.  * @see hook_library_alter()
  22.  */
  23. function drupal_add_library($module, $name) {
  24.   $added = &drupal_static(__FUNCTION__, array());
  25.  
  26.   // Only process the library if it exists and it hasn't been added yet.
  27.   if (!isset($added[$module][$name]) && $library = drupal_get_library($module, $name)) {
  28.     // State that we are processing the library, so that it's not processed again.
  29.     $added[$module][$name] = TRUE;
  30.  
  31.     // Prepare the library for addition.
  32.     $library += array('dependencies' => array(), 'js' => array(), 'css' => array());
  33.  
  34.     // Ensure the dependencies are available first.
  35.     foreach ($library['dependencies'] as $dependency) {
  36.       // Directly invoke drupal_add_library() to test its return value. The first
  37.       // parameter is the library's hosting module, while the second argument is
  38.       // the library's name.
  39.       if (drupal_add_library($dependency[0], $dependency[1]) === FALSE) {
  40.         // If any dependent library could not be added, this library will
  41.         // break, so stop here.
  42.         return $added[$module][$name] = FALSE;
  43.       }
  44.     }
  45.  
  46.     // Add related JavaScript.
  47.     foreach ($library['js'] as $data => $options) {
  48.       // For JS settings we need to transform $options['data'] into $data.
  49.       if (isset($options['type'], $options['data']) && $options['type'] == 'setting') {
  50.         $data = $options['data'];
  51.         unset($options['data']);
  52.       }
  53.       // All JavaScript contained within the libraries are given a default
  54.       // weight of JS_LIBRARY.
  55.       elseif (!isset($options['weight'])) {
  56.         $options['weight'] = JS_LIBRARY;
  57.       }
  58.       drupal_add_js($data, $options);
  59.     }
  60.  
  61.     // Finally, add the stylesheets.
  62.     foreach ($library['css'] as $data => $options) {
  63.       drupal_add_css($data, $options);
  64.     }
  65.   }
  66.  
  67.   return $added[$module][$name];
  68. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.