diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 7382a18..6c11d23 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -1129,7 +1129,7 @@ protected function buttonWasClicked($element, FormStateInterface &$form_state) { * {@inheritdoc} */ public function setValue($element, $value, FormStateInterface &$form_state) { - NestedArray::setValue($form_state['values'], $element['#parents'], $value, TRUE); + $form_state->setValue($element, $value); } /** diff --git a/core/lib/Drupal/Core/Form/FormState.php b/core/lib/Drupal/Core/Form/FormState.php index 8bba317..589010b 100644 --- a/core/lib/Drupal/Core/Form/FormState.php +++ b/core/lib/Drupal/Core/Form/FormState.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Form; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Url; /** @@ -462,6 +463,15 @@ public function getErrors() { } /** + * {@inheritdoc} + */ + public function setValue($element, $value) { + $values = &$this->get('values'); + NestedArray::setValue($values, $element['#parents'], $value, TRUE); + return $this; + } + + /** * Wraps drupal_set_message(). * * @return array|null diff --git a/core/lib/Drupal/Core/Form/FormStateInterface.php b/core/lib/Drupal/Core/Form/FormStateInterface.php index 1748fca..28f3892 100644 --- a/core/lib/Drupal/Core/Form/FormStateInterface.php +++ b/core/lib/Drupal/Core/Form/FormStateInterface.php @@ -243,4 +243,33 @@ public function getErrors(); */ public function getError($element); + /** + * Changes submitted form values during form validation. + * + * Use this function to change the submitted value of a form element in a form + * validation function, so that the changed value persists in $form_state + * through to the submission handlers. + * + * Note that form validation functions are specified in the '#validate' + * component of the form array (the value of $form['#validate'] is an array of + * validation function names). If the form does not originate in your module, + * you can implement hook_form_FORM_ID_alter() to add a validation function + * to $form['#validate']. + * + * @param array $element + * The form element that should have its value updated; in most cases you + * can just pass in the element from the $form array, although the only + * component that is actually used is '#parents'. If constructing yourself, + * set $element['#parents'] to be an array giving the path through the form + * array's keys to the element whose value you want to update. For instance, + * if you want to update the value of $form['elem1']['elem2'], which should + * be stored in $form_state['values']['elem1']['elem2'], you would set + * $element['#parents'] = array('elem1','elem2'). + * @param mixed $value + * The new value for the form element. + * + * @return $this + */ + public function setValue($element, $value); + } diff --git a/core/lib/Drupal/Core/Form/FormSubmitter.php b/core/lib/Drupal/Core/Form/FormSubmitter.php index b212549..b3797d2 100644 --- a/core/lib/Drupal/Core/Form/FormSubmitter.php +++ b/core/lib/Drupal/Core/Form/FormSubmitter.php @@ -61,7 +61,7 @@ public function doSubmitForm(&$form, FormStateInterface &$form_state) { // \Drupal\Core\Form\FormBuilderInterface::submitForm). if ($batch = &$this->batchGet() && !isset($batch['current_set'])) { // Store $form_state information in the batch definition. - $batch['form_state'] = clone $form_state; + $batch['form_state'] = $form_state; $batch['progressive'] = !$form_state['programmed']; $response = batch_process();