diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-11-13 14:04:08 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-11-13 14:04:08 +0000 |
commit | b51569528f42fbecbcefc4ffb2d9b982d43e0186 (patch) | |
tree | 8618fd6c7422b1eabd75b761c82f2108cacd4a93 /includes | |
parent | 6344f0379e7404b6b07f0adc3b412ccbc0a49d89 (diff) | |
download | brdo-b51569528f42fbecbcefc4ffb2d9b982d43e0186.tar.gz brdo-b51569528f42fbecbcefc4ffb2d9b982d43e0186.tar.bz2 |
- Patch #745590 by justinrandell, effulgentsia, yched, quicksketch, eojthebrave: #managed_file() element does not work when #extended not TRUE, or when ancestor element doesn't have #tree=TRUE.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 19 | ||||
-rw-r--r-- | includes/form.inc | 27 |
2 files changed, 35 insertions, 11 deletions
diff --git a/includes/common.inc b/includes/common.inc index ef6d1b971..b542def81 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -5987,14 +5987,22 @@ function element_set_attributes(array &$element, array $map) { * An array of parent keys, starting with the outermost key. * @param $value * The value to set. + * @param $force + * (Optional) If TRUE, the value is forced into the structure even if it + * requires the deletion of an already existing non-array parent value. If + * FALSE, PHP throws an error if trying to add into a value that is not an + * array. Defaults to FALSE. * * @see drupal_array_get_nested_value() */ -function drupal_array_set_nested_value(array &$array, array $parents, $value) { +function drupal_array_set_nested_value(array &$array, array $parents, $value, $force = FALSE) { $ref = &$array; foreach ($parents as $parent) { - // Note that PHP is fine with referencing a not existing array key - in this - // case it just creates an entry with NULL as value. + // PHP auto-creates container arrays and NULL entries without error if $ref + // is NULL, but throws an error if $ref is set, but not an array. + if ($force && isset($ref) && !is_array($ref)) { + $ref = array(); + } $ref = &$ref[$parent]; } $ref = $value; @@ -6058,10 +6066,7 @@ function drupal_array_set_nested_value(array &$array, array $parents, $value) { function drupal_array_get_nested_value(array &$array, array $parents, &$key_exists = NULL) { $ref = &$array; foreach ($parents as $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($ref[$parent]) || (is_array($ref) && array_key_exists($parent, $ref))) { + if (is_array($ref) && array_key_exists($parent, $ref)) { $ref = &$ref[$parent]; } else { diff --git a/includes/form.inc b/includes/form.inc index e3a217bda..15c18cbda 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -1031,10 +1031,29 @@ function drupal_validate_form($form_id, &$form, &$form_state) { drupal_array_set_nested_value($values, $section, $value); } } - // For convenience we always make the value of the pressed button available. + // A button's #value does not require validation, so for convenience we + // allow the value of the clicked button to be retained in its normal + // $form_state['values'] locations, even if these locations are not included + // in #limit_validation_errors. if (isset($form_state['triggering_element']['#button_type'])) { - $values[$form_state['triggering_element']['#name']] = $form_state['triggering_element']['#value']; - drupal_array_set_nested_value($values, $form_state['triggering_element']['#parents'], $form_state['triggering_element']['#value']); + $button_value = $form_state['triggering_element']['#value']; + + // Like all input controls, the button value may be in the location + // dictated by #parents. If it is, copy it to $values, but do not override + // what may already be in $values. + $parents = $form_state['triggering_element']['#parents']; + if (!drupal_array_nested_key_exists($values, $parents) && drupal_array_get_nested_value($form_state['values'], $parents) === $button_value) { + drupal_array_set_nested_value($values, $parents, $button_value); + } + + // Additionally, form_builder() places the button value in + // $form_state['values'][BUTTON_NAME]. If it's still there, after + // validation handlers have run, copy it to $values, but do not override + // what may already be in $values. + $name = $form_state['triggering_element']['#name']; + if (!isset($values[$name]) && isset($form_state['values'][$name]) && $form_state['values'][$name] === $button_value) { + $values[$name] = $button_value; + } } $form_state['values'] = $values; } @@ -2301,7 +2320,7 @@ function form_type_token_value($element, $input = FALSE) { * Form state array where the value change should be recorded. */ function form_set_value($element, $value, &$form_state) { - drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value); + drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value, TRUE); } /** |