You are here

public function Bootstrap4Modal::displayEntityBrowser in Bootstrap 4 Modal 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/EntityBrowser/Display/Bootstrap4Modal.php \Drupal\bootstrap4_modal\Plugin\EntityBrowser\Display\Bootstrap4Modal::displayEntityBrowser()

File

src/Plugin/EntityBrowser/Display/Bootstrap4Modal.php, line 40

Class

Bootstrap4Modal
Presents entity browser in an Modal.

Namespace

Drupal\bootstrap4_modal\Plugin\EntityBrowser\Display

Code

public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
  DisplayBase::displayEntityBrowser($element, $form_state, $complete_form, $persistent_data);
  $js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $this
    ->getUuid());
  $js_event_object
    ->registerCallback('Drupal.entityBrowser.selectionCompleted');
  $js_event = $this->eventDispatcher
    ->dispatch(Events::REGISTER_JS_CALLBACKS, $js_event_object);
  $original_path = $this->currentPath
    ->getPath();
  $data = [
    'query_parameters' => [
      'query' => [
        'uuid' => $this
          ->getUuid(),
        'original_path' => $original_path,
      ],
    ],
    'attributes' => [
      'data-uuid' => $this
        ->getUuid(),
    ],
  ];
  $event_object = new AlterEntityBrowserDisplayData($this->configuration['entity_browser_id'], $this
    ->getUuid(), $this
    ->getPluginDefinition(), $form_state, $data);
  $event = $this->eventDispatcher
    ->dispatch(Events::ALTER_BROWSER_DISPLAY_DATA, $event_object);
  $data = $event
    ->getData();
  return [
    '#theme_wrappers' => [
      'container',
    ],
    'path' => [
      '#type' => 'hidden',
      '#value' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])
        ->toString(),
    ],
    'open_modal' => [
      '#type' => 'submit',
      '#value' => $this
        ->t($this->configuration['link_text']),
      '#limit_validation_errors' => [],
      '#submit' => [],
      '#name' => implode('_', $element['#eb_parents']),
      '#ajax' => [
        'callback' => [
          $this,
          'openModal',
        ],
        'event' => 'click',
      ],
      '#executes_submit_callback' => FALSE,
      '#attributes' => $data['attributes'],
      '#attached' => [
        'library' => [
          'bootstrap4_modal/bs4_modal.entity_browser',
        ],
        'drupalSettings' => [
          'entity_browser' => [
            $this
              ->getUuid() => [
              'auto_open' => $this->configuration['auto_open'],
            ],
            'modal' => [
              $this
                ->getUuid() => [
                'uuid' => $this
                  ->getUuid(),
                'js_callbacks' => $js_event
                  ->getCallbacks(),
                'original_path' => $original_path,
                'auto_open' => $this->configuration['auto_open'],
              ],
            ],
          ],
        ],
      ],
    ],
  ];
}