You are here

public static function WebformElementStates::addConditionSubmit in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformElementStates.php \Drupal\webform\Element\WebformElementStates::addConditionSubmit()

Form submission handler for adding another condition.

Parameters

array $form: An associative array containing the structure of the form.

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

File

src/Element/WebformElementStates.php, line 617

Class

WebformElementStates
Provides a webform element to edit an element's #states.

Namespace

Drupal\webform\Element

Code

public static function addConditionSubmit(array &$form, FormStateInterface $form_state) {

  // Get the webform states element by going up one level.
  $button = $form_state
    ->getTriggeringElement();
  $element =& NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -4));

  // The $row_index is not sequential so we need to rebuild the value instead
  // of just using an array_slice().
  $row_index = (int) $button['#row_index'];
  $values = [];
  foreach ($element['states']['#value'] as $index => $value) {
    $values[] = $value;
    if ($index === $row_index) {
      $values[] = [
        'selector' => '',
        'trigger' => '',
        'value' => '',
      ];
    }
  }

  // Reset values.
  $values = array_values($values);

  // Set values.
  $form_state
    ->setValueForElement($element['states'], $values);
  NestedArray::setValue($form_state
    ->getUserInput(), $element['states']['#parents'], $values);

  // Update the number of rows.
  $form_state
    ->set(static::getStorageKey($element, 'number_of_rows'), count($values));

  // Rebuild the form.
  $form_state
    ->setRebuild();
}