You are here

public static function WizardPluginBase::getSelected in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php \Drupal\views\Plugin\views\wizard\WizardPluginBase::getSelected()

Gets the current value of a #select element, from within a form constructor function.

This function is intended for use in highly dynamic forms (in particular the add view wizard) which are rebuilt in different ways depending on which triggering element (AJAX or otherwise) was most recently fired. For example, sometimes it is necessary to decide how to build one dynamic form element based on the value of a different dynamic form element that may not have even been present on the form the last time it was submitted. This function takes care of resolving those conflicts and gives you the proper current value of the requested #select element.

By necessity, this function sometimes uses non-validated user input from FormState::$input in making its determination. Although it performs some minor validation of its own, it is not complete. The intention is that the return value of this function should only be used to help decide how to build the current form the next time it is reloaded, not to be saved as if it had gone through the normal, final form validation process. Do NOT use the results of this function for any other purpose besides deciding how to build the next version of the form.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

$parents: An array of parent keys that point to the part of the submitted form values that are expected to contain the element's value (in the case where this form element was actually submitted). In a simple case (assuming #tree is TRUE throughout the form), if the select element is located in $form['wrapper']['select'], so that the submitted form values would normally be found in $form_state->getValue(array('wrapper', 'select')), you would pass array('wrapper', 'select') for this parameter.

$default_value: The default value to return if the #select element does not currently have a proper value set based on the submitted input.

$element: An array representing the current version of the #select element within the form.

Return value

The current value of the #select element. A common use for this is to feed it back into $element['#default_value'] so that the form will be rendered with the correct value selected.

6 calls to WizardPluginBase::getSelected()
Node::buildFilters in core/modules/node/src/Plugin/views/wizard/Node.php
Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
ViewAddForm::form in core/modules/views_ui/src/ViewAddForm.php
Gets the actual form array to be built.
WizardPluginBase::buildFilters in core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php
Builds the form structure for selecting the view's filters.
WizardPluginBase::buildForm in core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php
Form callback to build other elements in the "show" form.
WizardPluginBase::buildFormStyle in core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php
Adds the style options to the wizard form.

... See full list

File

core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php, line 491
Contains \Drupal\views\Plugin\views\wizard\WizardPluginBase.

Class

WizardPluginBase
Base class for Views wizard plugins.

Namespace

Drupal\views\Plugin\views\wizard

Code

public static function getSelected(FormStateInterface $form_state, $parents, $default_value, $element) {

  // For now, don't trust this to work on anything but a #select element.
  if (!isset($element['#type']) || $element['#type'] != 'select' || !isset($element['#options'])) {
    return $default_value;
  }

  // If there is a user-submitted value for this element that matches one of
  // the currently available options attached to it, use that. However, only
  // perform this check during the form rebuild. During the initial build
  // after #ajax is triggered, we need to rebuild the form as it was
  // initially. We need to check FormState::getUserInput() rather than
  // $form_state->getValues() here because the triggering element often has
  // the #limit_validation_errors property set to prevent unwanted errors
  // elsewhere on the form. This means that the $form_state->getValues() array
  // won't be complete. We could make it complete by adding each required part
  // of the form to the #limit_validation_errors property individually as the
  // form is being built, but this is difficult to do for a highly dynamic and
  // extensible form. This method is much simpler.
  $user_input = $form_state
    ->isRebuilding() ? $form_state
    ->getUserInput() : [];
  if (!empty($user_input)) {
    $key_exists = NULL;
    $submitted = NestedArray::getValue($user_input, $parents, $key_exists);

    // Check that the user-submitted value is one of the allowed options before
    // returning it. This is not a substitute for actual form validation;
    // rather it is necessary because, for example, the same select element
    // might have #options A, B, and C under one set of conditions but #options
    // D, E, F under a different set of conditions. So the form submission
    // might have occurred with option A selected, but when the form is rebuilt
    // option A is no longer one of the choices. In that case, we don't want to
    // use the value that was submitted anymore but rather fall back to the
    // default value.
    if ($key_exists && in_array($submitted, array_keys($element['#options']))) {
      return $submitted;
    }
  }

  // Fall back on returning the default value if nothing was returned above.
  return $default_value;
}