You are here

public function LayoutParagraphsWidget::removeItemAjax in Layout Paragraphs 1.0.x

Ajax callback to remove an item - launches confirmation dialog.

Parameters

array $form: The form array.

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

Return value

\Drupal\Core\Ajax\AjaxResponse The Ajax Response.

File

src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php, line 1894

Class

LayoutParagraphsWidget
Entity Reference with Layout field widget.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldWidget

Code

public function removeItemAjax(array $form, FormStateInterface $form_state) {
  $element = $form_state
    ->getTriggeringElement();
  $parents = $element['#element_parents'];
  $field_state = static::getWidgetState($parents, $this->fieldName, $form_state);
  $widget_field = NestedArray::getValue($form, $field_state['array_parents']);
  $entity_form = $widget_field['remove_form'];
  $entity = $entity_form['#entity'];
  $type = $entity
    ->getParagraphType();
  $html_id = $this
    ->entityFormHtmlId($field_state);
  $dialog_options = [
    'dialogClass' => 'layout-paragraphs-dialog',
    'modal' => TRUE,
    'appendTo' => '#' . $this->wrapperId,
    'width' => 800,
    'drupalAutoButtons' => FALSE,
  ];
  $response = new AjaxResponse();
  $response
    ->addCommand(new AppendCommand('#' . $this->wrapperId, '<div id="' . $html_id . '"></div>'));
  $response
    ->addCommand(new OpenDialogCommand('#' . $html_id, $this
    ->t('Remove @type', [
    '@type' => $type
      ->label(),
  ]), $entity_form, $dialog_options));
  $response
    ->addCommand(new LayoutParagraphsStateResetCommand('#' . $this->wrapperId));
  return $response;
}