You are here

public function ViewWidget::ajaxCallback in Entity Reference View Widget 8

Ajax callback for the the add items button.

File

src/Plugin/Field/FieldWidget/ViewWidget.php, line 254
Contains \Drupal\entity_reference_view_widget\Plugin\Field\FieldWidget\ViewWidget.

Class

ViewWidget
Plugin implementation of the 'entity_reference_view_widget' widget.

Namespace

Drupal\entity_reference_view_widget\Plugin\Field\FieldWidget

Code

public function ajaxCallback(array $form, FormStateInterface &$form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (isset($triggering_element['#ervw_settings'])) {
    $response = new AjaxResponse();
    $view = $this
      ->getSetting('view');
    if (!empty($view)) {
      list($view_id, $display_id) = explode('|', $view);
      if ($view = Views::getView($view_id)) {
        $view
          ->setAjaxEnabled(TRUE);
        $preview = $view
          ->preview($display_id);
        $response
          ->addCommand(new SettingsCommand($view->element['#attached']['js'][0]['data'], TRUE));
        $form_settings = array(
          'view' => $view,
          'preview' => $preview,
          'settings' => $triggering_element['#ervw_settings'] + $this
            ->getSettings(),
        );
        $modal_form = \Drupal::formBuilder()
          ->getForm('Drupal\\entity_reference_view_widget\\Form\\ModalForm', $form_settings);
        $response
          ->addCommand(new SettingsCommand($modal_form['actions']['add_items']['#attached']['js'][0]['data'], TRUE));
        $modal_content = '';

        // Display the exposed widgets on top of the modal.
        if (!empty($view->exposed_widgets)) {
          $modal_content .= drupal_render($view->exposed_widgets);
        }
        $modal_content .= drupal_render($modal_form);
        $response
          ->addCommand(new OpenModalDialogCommand($view->storage
          ->label(), $modal_content, array(
          'width' => 700,
        )));
      }
    }
    return $response;
  }
}