You are here

public static function YamlFormElementStates::removeRowSubmit in YAML Form 8

Form submission handler for removing a state or 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/YamlFormElementStates.php, line 430

Class

YamlFormElementStates
Provides a form element to edit an element's #states.

Namespace

Drupal\yamlform\Element

Code

public static function removeRowSubmit(array &$form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -4));
  $row_index = $button['#row_index'];
  $values = $element['states']['#value'];
  if (isset($values[$row_index]['state'])) {

    // Remove state.
    do {
      unset($values[$row_index]);
      $row_index++;
    } while (isset($values[$row_index]) && !isset($values[$row_index]['state']));
  }
  else {

    // Remove condition.
    unset($values[$row_index]);
  }

  // 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(self::getStorageKey($element, 'number_of_rows'), count($values));

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