function FormState::cleanValues

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::cleanValues()
  2. 10 core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::cleanValues()
  3. 11.x core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::cleanValues()

Overrides FormStateInterface::cleanValues

File

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

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.