You are here

public static function YamlFormMultiple::addItemsSubmit in YAML Form 8

Form submission handler for adding more items.

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/YamlFormMultiple.php, line 306

Class

YamlFormMultiple
Provides a form element to assist in creation of multiple elements.

Namespace

Drupal\yamlform\Element

Code

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

  // Get the form list element by going up two levels.
  $button = $form_state
    ->getTriggeringElement();
  $element =& NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));

  // Add more items to the number of items.
  $number_of_items_storage_key = self::getStorageKey($element, 'number_of_items');
  $number_of_items = $form_state
    ->get($number_of_items_storage_key);
  $more_items = (int) $element['add']['more_items']['#value'];
  $form_state
    ->set($number_of_items_storage_key, $number_of_items + $more_items);

  // Reset values.
  $element['items']['#value'] = array_values($element['items']['#value']);
  $form_state
    ->setValueForElement($element['items'], $element['items']['#value']);
  NestedArray::setValue($form_state
    ->getUserInput(), $element['items']['#parents'], $element['items']['#value']);

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