You are here

public function FormState::cleanValues in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::cleanValues()
  2. 9 core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::cleanValues()

File

core/lib/Drupal/Core/Form/FormState.php, line 1197

Class

FormState
Stores information about the state of a form.

Namespace

Drupal\Core\Form

Code

public function cleanValues() {
  foreach ($this
    ->getCleanValueKeys() as $value) {
    $this
      ->unsetValue($value);
  }

  // Remove button values.
  // \Drupal::formBuilder()->doBuildForm() collects all button elements in a
  // form. We remove the button value separately for each button element.
  foreach ($this
    ->getButtons() as $button) {

    // Remove this button's value from the submitted form values by finding
    // the value corresponding to this button.
    // We iterate over the #parents of this button and move a reference to
    // each parent in self::getValues(). For example, if #parents is:
    // @code
    //   array('foo', 'bar', 'baz')
    // @endcode
    // then the corresponding self::getValues() part will look like this:
    // @code
    // array(
    //   'foo' => array(
    //     'bar' => array(
    //       'baz' => 'button_value',
    //     ),
    //   ),
    // )
    // @endcode
    // We start by (re)moving 'baz' to $last_parent, so we are able unset it
    // at the end of the iteration. Initially, $values will contain a
    // reference to self::getValues(), but in the iteration we move the
    // reference to self::getValue('foo'), and finally to
    // self::getValue(array('foo', 'bar')), which is the level where we
    // can unset 'baz' (that is stored in $last_parent).
    $parents = $button['#parents'];
    $last_parent = array_pop($parents);
    $key_exists = NULL;
    $values =& NestedArray::getValue($this
      ->getValues(), $parents, $key_exists);
    if ($key_exists && is_array($values)) {
      unset($values[$last_parent]);
    }
  }
  return $this;
}