You are here

public static function FieldCollectionEmbedWidget::addMoreSubmit in Field collection 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php \Drupal\field_collection\Plugin\Field\FieldWidget\FieldCollectionEmbedWidget::addMoreSubmit()

Submission handler for the "Add another item" button.

Overrides WidgetBase::addMoreSubmit

File

src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php, line 355

Class

FieldCollectionEmbedWidget
Plugin implementation of the 'field_collection_embed' widget.

Namespace

Drupal\field_collection\Plugin\Field\FieldWidget

Code

public static function addMoreSubmit(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];

  // Increment the items count.
  $field_state = static::getWidgetState($parents, $field_name, $form_state);
  $field_state['items_count']++;

  // Make a new field collection item. This helps to ensure that
  // trying to add a field collection item won't ressurect a deleted one from
  // the trash bin.
  $field_state['field_collection_item'][$field_state['items_count']] = FieldCollectionItem::create([
    'field_name' => $field_name,
  ]);
  static::setWidgetState($parents, $field_name, $form_state, $field_state);
  $form_state
    ->setRebuild();
}