You are here

public static function EntityqueueDragtableWidget::addItemSubmit in Entityqueue 8

Submission handler for the "Add item" button.

File

src/Plugin/Field/FieldWidget/EntityqueueDragtableWidget.php, line 307

Class

EntityqueueDragtableWidget
Plugin implementation of the 'entityqueue_dragtable' widget.

Namespace

Drupal\entityqueue\Plugin\Field\FieldWidget

Code

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

  // Go two levels up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];
  $submitted_values = NestedArray::getValue($form_state
    ->getValues(), array_slice($button['#parents'], 0, -1));
  if (isset($submitted_values['new_item']['target_id'])) {
    $items = $form_state
      ->getFormObject()
      ->getEntity()
      ->get($field_name);
    $items
      ->appendItem($submitted_values['new_item']);

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