Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1211 diff -u -p -r1.1211 common.inc --- includes/common.inc 27 Aug 2010 11:54:32 -0000 1.1211 +++ includes/common.inc 28 Aug 2010 01:51:57 -0000 @@ -5649,8 +5649,8 @@ function drupal_array_set_nested_value(& /** * Retrieves a value from a nested array with variable depth. * - * This helper function should be used when the depth of the array element you - * are changing may vary (that is, the number of parent keys is variable). It is + * This helper function should be used when the depth of the array element being + * retrieved may vary (that is, the number of parent keys is variable). It is * primarily used for form structures and renderable arrays. * * Without this helper function the only way to get a nested array value with @@ -5663,14 +5663,11 @@ function drupal_array_set_nested_value(& * * Instead, use this helper function: * @code - * list($value, $value_exists) = drupal_array_get_nested_value($form, $parents); - * if ($value_exists) { - * // ... do something with $value ... - * } + * $value = drupal_array_get_nested_value($form, $parents); * @endcode * * However if the number of array parent keys is static, the value should always - * be get directly rather than calling this function. For instance: + * be retrieved directly rather than calling this function. For instance: * @code * $value = $form['signature_settings']['signature']; * @endcode @@ -5679,34 +5676,63 @@ function drupal_array_set_nested_value(& * The array from which to get the value. * @param $parents * An array of parent keys of the value, starting with the outermost key. + * @param &$key_exists + * (Optional) If passed, this function sets this variable to TRUE if each + * parent key specified by $parents exists in the corresponding section of + * $array, and FALSE otherwise. If this function's return value is anything + * other than NULL, then $key_exists is necessarily TRUE, but if this + * function's return value is NULL, then the caller can check $key_exists if + * it needs to have different logic for when a key is missing from the array + * versus when the key exists, but the value is NULL. * * @return - * An indexed array containing: - * - The requested nested value, if it exists, or NULL if it does not. - * - TRUE if all the parent keys exist, FALSE otherwise. + * The requested nested value, possibly NULL, if each parent key specified by + * $parents exists in $array. Otherwise, NULL. The $key_exists argument may be + * used to distinguish the two possibilities when NULL is returned. * * @see drupal_array_set_nested_value() - * @see drupal_array_value_exists() */ -function drupal_array_get_nested_value($array, $parents) { +function drupal_array_get_nested_value($array, $parents, &$key_exists = NULL) { + $result = $array; foreach ($parents as $parent) { - if (isset($array[$parent])) { - $array = $array[$parent]; + // array_key_exists() is slower than isset() and triggers notices if the + // second argument is not an array, so only call it when absolutely + // necessary. + if (isset($result[$parent]) || (isset($result) && is_array($result) && array_key_exists($parent, $result))) { + $result = $result[$parent]; } else { - return array(NULL, FALSE); + $key_exists = FALSE; + return NULL; } } - return array($array, TRUE); + $key_exists = TRUE; + return $result; } /** - * Determines whether a value in a nested array with variable depth exists. + * Determines whether a nested array with variable depth contains all of the requested keys. * * This helper function should be used when the depth of the array element to be * checked may vary (that is, the number of parent keys is variable). See - * drupal_array_set_nested_value() for details. This helper is primarily used - * for form structures and renderable arrays. + * drupal_array_set_nested_value() for details. It is primarily used for form + * structures and renderable arrays. + * + * If the number of array parent keys is static, this helper function is + * unnecessary and the following code can be used instead: + * @code + * $key_exists = array_key_exists('signature', $form['signature_settings']); + * @endcode + * + * Note that the order of parameters of this function is opposite that of PHP's + * array_key_exists() function, but this is to have consistency with + * drupal_array_get_nested_value() and drupal_array_set_nested_value(). + * + * This function is a very thin wrapper around information that is provided by + * drupal_array_get_nested_value(). If the calling function is likely to be + * called thousands of times in a page request, or if it needs both the value + * and whether the key exists, then it can call drupal_array_get_nested_value() + * directly. * * @param $array * The array with the value to check for. @@ -5716,22 +5742,14 @@ function drupal_array_get_nested_value($ * @return * TRUE if all the parent keys exist, FALSE otherwise. * - * @see drupal_array_set_nested_value() * @see drupal_array_get_nested_value() */ -function drupal_array_nested_value_exists($array, $parents) { - foreach ($parents as $parent) { - if (isset($array[$parent])) { - $array = $array[$parent]; - } - else { - return FALSE; - } - } - return TRUE; +function drupal_array_nested_key_exists($array, $parents) { + $key_exists = NULL; + drupal_array_get_nested_value($array, $parents, $key_exists); + return $key_exists; } - /** * Provide theme registration for themes across .inc files. */ Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.485 diff -u -p -r1.485 form.inc --- includes/form.inc 27 Aug 2010 11:54:32 -0000 1.485 +++ includes/form.inc 28 Aug 2010 01:51:57 -0000 @@ -959,8 +959,11 @@ function drupal_validate_form($form_id, if (isset($form_state['triggering_element']['#limit_validation_errors']) && $form_state['triggering_element']['#limit_validation_errors'] !== FALSE) { $values = array(); foreach ($form_state['triggering_element']['#limit_validation_errors'] as $section) { - list($value, $value_exists) = drupal_array_get_nested_value($form_state['values'], $section); - if ($value_exists) { + // If the section exists within $form_state['values'], even if the value + // is NULL, copy it to $values. + $section_exists = NULL; + $value = drupal_array_get_nested_value($form_state['values'], $section, $section_exists); + if ($section_exists) { drupal_array_set_nested_value($values, $section, $value); } } @@ -1723,18 +1726,8 @@ function _form_builder_handle_input_elem if ($process_input) { // Get the input for the current element. NULL values in the input need to // be explicitly distinguished from missing input. (see below) - $input = $form_state['input']; - $input_exists = TRUE; - foreach ($element['#parents'] as $parent) { - if (is_array($input) && array_key_exists($parent, $input)) { - $input = $input[$parent]; - } - else { - $input = NULL; - $input_exists = FALSE; - break; - } - } + $input_exists = NULL; + $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists); // For browser-submitted forms, the submitted values do not contain values // for certain elements (empty multiple select, unchecked checkbox). // During initial form processing, we add explicit NULL values for such @@ -1806,7 +1799,7 @@ function _form_builder_handle_input_elem // Set the element's value in $form_state['values'], but only, if its key // does not exist yet (a #value_callback may have already populated it). - if (!drupal_array_nested_value_exists($form_state['values'], $element['#parents'])) { + if (!drupal_array_nested_key_exists($form_state['values'], $element['#parents'])) { form_set_value($element, $element['#value'], $form_state); } } Index: modules/file/file.module =================================================================== RCS file: /cvs/drupal/drupal/modules/file/file.module,v retrieving revision 1.37 diff -u -p -r1.37 file.module --- modules/file/file.module 27 Aug 2010 11:54:32 -0000 1.37 +++ modules/file/file.module 28 Aug 2010 01:51:58 -0000 @@ -572,7 +572,7 @@ function file_managed_file_submit($form, // and set $element to the managed_file element that contains that button. $parents = $form_state['triggering_element']['#array_parents']; $button_key = array_pop($parents); - list($element) = drupal_array_get_nested_value($form, $parents); + $element = drupal_array_get_nested_value($form, $parents); // No action is needed here for the upload button, because all file uploads on // the form are processed by file_managed_file_value() regardless of which