function ViewsFormHelperTrait::formButtonWasClicked

Same name and namespace in other branches
  1. 11.x core/modules/views/src/ViewsFormHelperTrait.php \Drupal\views\ViewsFormHelperTrait::formButtonWasClicked()

The #process callback for a button.

Determines if a button is the form's triggering element.

The Form API has logic to determine the form's triggering element based on the data in POST. However, it only checks buttons based on a single #value per button. This function may be added to a button's #process callbacks to extend button click detection to support multiple #values per button. If the data in POST matches any value in the button's #values array, then the button is detected as having been clicked. This can be used when the value (label) of the same logical button may be different based on context (e.g., "Apply" vs. "Apply and continue").

See also

\Drupal\Core\Form\FormBuilder::handleInputElement()

\Drupal\Core\Form\FormBuilder::buttonWasClicked()

File

core/modules/views/src/ViewsFormHelperTrait.php, line 148

Class

ViewsFormHelperTrait
Provides reusable code to be shared by Views forms.

Namespace

Drupal\views

Code

public static function formButtonWasClicked(array $element, FormStateInterface $formState) : array {
  $userInput = $formState->getUserInput();
  $processInput = empty($element['#disabled']) && ($formState->isProgrammed() || $formState->isProcessingInput() && (!isset($element['#access']) || $element['#access']));
  if ($processInput && !$formState->getTriggeringElement() && !empty($element['#is_button']) && isset($userInput[$element['#name']]) && isset($element['#values']) && in_array($userInput[$element['#name']], array_map('strval', $element['#values']), TRUE)) {
    $formState->setTriggeringElement($element);
  }
  return $element;
}

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