Commit 73a65a36 authored by webchick's avatar webchick

Issue #2313397 by tim.plunkett: Replace FormBuilderInterface::setValue() with...

Issue #2313397 by tim.plunkett: Replace FormBuilderInterface::setValue() with FormStateInterface::setValueForElement().
parent 3724e192
......@@ -615,12 +615,12 @@ function form_type_token_value($element, $input = FALSE) {
* Changes submitted form values during form validation.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal::formBuilder()->setValue().
* Use $form_state->setValueForElement().
*
* @see \Drupal\Core\Form\FormBuilderInterface::setValue().
* @see \Drupal\Core\Form\FormStateInterface::setValueForElement().
*/
function form_set_value($element, $value, FormStateInterface $form_state) {
\Drupal::formBuilder()->setValue($element, $value, $form_state);
$form_state->setValueForElement($element, $value);
}
/**
......
......@@ -1019,7 +1019,7 @@ protected function handleInputElement($form_id, &$element, FormStateInterface &$
// 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 (!NestedArray::keyExists($form_state->getValues(), $element['#parents'])) {
$this->setValue($element, $element['#value'], $form_state);
$form_state->setValueForElement($element, $element['#value']);
}
}
......@@ -1083,15 +1083,6 @@ protected function buttonWasClicked($element, FormStateInterface &$form_state) {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function setValue($element, $value, FormStateInterface &$form_state) {
$values = $form_state->getValues();
NestedArray::setValue($values, $element['#parents'], $value, TRUE);
$form_state->set('values', $values);
}
/**
* Triggers kernel.response and sends a form response.
*
......
......@@ -324,33 +324,4 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state);
*/
public function doBuildForm($form_id, &$element, FormStateInterface &$form_state);
/**
* 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 $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 $value
* The new value for the form element.
* @param $form_state
* The current state of the form where the value change should be recorded.
*/
public function setValue($element, $value, FormStateInterface &$form_state);
}
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Response;
......@@ -567,7 +568,7 @@ public function addBuildInfo($property, $value) {
* {@inheritdoc}
*/
public function getValues() {
return $this->values;
return $this->values ?: array();
}
/**
......@@ -580,6 +581,16 @@ public function addValue($property, $value) {
return $this;
}
/**
* {@inheritdoc}
*/
public function setValueForElement($element, $value) {
$values = $this->getValues();
NestedArray::setValue($values, $element['#parents'], $value, TRUE);
$this->set('values', $values);
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -175,6 +175,35 @@ public function getValues();
*/
public function addValue($property, $value);
/**
* 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 setValueForElement($element, $value);
/**
* Determines if any forms have any errors.
*
......
......@@ -396,7 +396,7 @@ public static function validateDefaultImageForm(array &$element, FormStateInterf
else {
$value = 0;
}
\Drupal::formBuilder()->setValue($element, $value, $form_state);
$form_state->setValueForElement($element, $value);
}
/**
......
......@@ -145,7 +145,7 @@ public static function validateAllowedValues($element, FormStateInterface $form_
}
}
\Drupal::formBuilder()->setValue($element, $values, $form_state);
$form_state->setValueForElement($element, $values);
}
}
......
......@@ -87,8 +87,7 @@ public static function validateFormElement(array &$element, FormStateInterface $
// Trim the submitted value.
$alias = trim($element['alias']['#value']);
if (!empty($alias)) {
$form_builder = \Drupal::formBuilder();
$form_builder->setValue($element['alias'], $alias, $form_state);
$form_state->setValueForElement($element['alias'], $alias);
// Validate that the submitted alias does not exist yet.
$is_exists = \Drupal::service('path.alias_storage')->aliasExists($alias, $element['langcode']['#value'], $element['source']['#value']);
......
......@@ -159,4 +159,28 @@ public function testFormErrorsDuringSubmission() {
$form_state->setErrorByName('test', 'message');
}
/**
* Tests that setting the value for an element adds to the values.
*
* @covers ::setValueForElement
*/
public function testSetValueForElement() {
$element = array(
'#parents' => array(
'foo',
'bar',
),
);
$value = $this->randomName();
$form_state = new FormState();
$form_state->setValueForElement($element, $value);
$expected = array(
'foo' => array(
'bar' => $value,
),
);
$this->assertSame($expected, $form_state->getValues());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment