You are here

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

Ajax callback to return the entire ERL element.

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 1787

Class

LayoutParagraphsWidget
Entity Reference with Layout field widget.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldWidget

Code

public function saveItemAjax(array $form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  $uuid = $triggering_element['#uuid'];
  $delta = $triggering_element['#delta'];
  $parents = $triggering_element['#element_parents'];
  $field_state = static::getWidgetState($parents, $this->fieldName, $form_state);
  $widget_field = NestedArray::getValue($form, $field_state['array_parents']);
  $html_id = $this
    ->entityFormHtmlId($field_state);
  $response = new AjaxResponse();
  if ($form_state
    ->hasAnyErrors()) {
    $entity_form = $widget_field['entity_form'];
    $selector = '#' . $this->wrapperId . ' .layout-paragraphs-form';
    $entity_form['status'] = [
      '#weight' => -100,
      '#type' => 'status_messages',
    ];
    $response
      ->addCommand(new ReplaceCommand($selector, $entity_form));
  }
  else {
    $element = static::findElementByUuid($widget_field, $uuid);
    $path = array_merge($widget_field['#parents'], [
      'add_more',
      'actions',
    ]);
    $target_id = $form_state
      ->getValue(array_merge($path, [
      'dom_id',
    ]));
    $insert_method = $form_state
      ->getValue(array_merge($path, [
      'insert_method',
    ]));
    $settings = [
      'target_id' => $target_id,
      'insert_method' => $insert_method,
      'element_id' => $this->wrapperId . '--item-' . $delta,
    ];
    $response
      ->addCommand(new LayoutParagraphsInsertCommand($settings, $element));
    $response
      ->addCommand(new CloseDialogCommand('#' . $html_id));
    $response
      ->addCommand(new SettingsCommand([
      'layoutParagraphsWidgets' => [
        $this->wrapperId => [
          'itemsCount' => $this
            ->activeItemsCount($field_state['items']),
        ],
      ],
    ], TRUE));
  }
  $disabled_bin = $widget_field['disabled'];
  $response
    ->addCommand(new ReplaceCommand('#' . $this->wrapperId . ' .layout-paragraphs-disabled-items', $disabled_bin));
  return $response;
}