diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/bootstrap.inc | 73 | ||||
-rw-r--r-- | includes/common.inc | 11 |
2 files changed, 80 insertions, 4 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 256121c3e..84fdd9833 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -1877,6 +1877,79 @@ function drupal_hash_base64($data) { } /** + * Merges multiple arrays, recursively, and returns the merged array. + * + * This function is similar to PHP's array_merge_recursive() function, but it + * handles non-array values differently. When merging values that are not both + * arrays, the latter value replaces the former rather than merging with it. + * + * Example: + * @code + * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); + * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); + * + * // This results in array('fragment' => array('x', 'y'), 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd'))). + * $incorrect = array_merge_recursive($link_options_1, $link_options_2); + * + * // This results in array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))). + * $correct = drupal_array_merge_deep($link_options_1, $link_options_2); + * @endcode + * + * @param ... + * Arrays to merge. + * + * @return + * The merged array. + * + * @see drupal_array_merge_deep_array() + */ +function drupal_array_merge_deep() { + return drupal_array_merge_deep_array(func_get_args()); +} + +/** + * Merges multiple arrays, recursively, and returns the merged array. + * + * This function is equivalent to drupal_array_merge_deep(), except the + * input arrays are passed as a single array parameter rather than a variable + * parameter list. + * + * The following are equivalent: + * - drupal_array_merge_deep($a, $b); + * - drupal_array_merge_deep_array(array($a, $b)); + * + * The following are also equivalent: + * - call_user_func_array('drupal_array_merge_deep', $arrays_to_merge); + * - drupal_array_merge_deep_array($arrays_to_merge); + * + * @see drupal_array_merge_deep() + */ +function drupal_array_merge_deep_array($arrays) { + $result = array(); + + foreach ($arrays as $array) { + foreach ($array as $key => $value) { + // Renumber integer keys as array_merge_recursive() does. Note that PHP + // automatically converts array keys that are integer strings (e.g., '1') + // to integers. + if (is_integer($key)) { + $result[] = $value; + } + // Recurse when both values are arrays. + elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { + $result[$key] = drupal_array_merge_deep_array(array($result[$key], $value)); + } + // Otherwise, use the latter value, overriding any previous value. + else { + $result[$key] = $value; + } + } + } + + return $result; +} + +/** * Generates a default anonymous $user object. * * @return Object - the user object. diff --git a/includes/common.inc b/includes/common.inc index 7a9dff388..4ad8e08d8 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3751,6 +3751,7 @@ function drupal_region_class($region) { * array('type' => 'inline', 'scope' => 'footer', 'weight' => 5) * ); * drupal_add_js('http://example.com/example.js', 'external'); + * drupal_add_js(array('myModule', array('key' => 'value')), 'setting'); * @endcode * * Calling drupal_static_reset('drupal_add_js') will clear all JavaScript added @@ -3783,9 +3784,11 @@ function drupal_region_class($region) { * hosted on the local server. These files will not be aggregated if * JavaScript aggregation is enabled. * - 'setting': An associative array with configuration options. The array is - * directly placed in Drupal.settings. All modules should wrap their actual - * configuration settings in another variable to prevent conflicts in the - * Drupal.settings namespace. + * merged directly into Drupal.settings. All modules should wrap their + * actual configuration settings in another variable to prevent conflicts in + * the Drupal.settings namespace. Items added with a string key will replace + * existing settings with that key; items with numeric array keys will be + * added to the existing settings array. * @param $options * (optional) A string defining the type of JavaScript that is being added in * the $data parameter ('file'/'setting'/'inline'/'external'), or an @@ -4070,7 +4073,7 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS case 'setting': $js_element = $element; $js_element['#value_prefix'] = $embed_prefix; - $js_element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(call_user_func_array('array_merge_recursive', $item['data'])) . ");"; + $js_element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(drupal_array_merge_deep_array($item['data'])) . ");"; $js_element['#value_suffix'] = $embed_suffix; $output .= theme('html_tag', array('element' => $js_element)); break; |