You are here

public function IFrame::displayEntityBrowser in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Display/IFrame.php \Drupal\entity_browser\Plugin\EntityBrowser\Display\IFrame::displayEntityBrowser()

Displays entity browser.

This is the "entry point" for every non-entity browser code to interact with it. It will take care about displaying entity browser in one way or another.

Parameters

array $element: A form element array containing basic properties for the entity browser element:

  • #eb_parents: The 'parents' space for the field in the form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

array $complete_form: The form structure where entity browser is being attached to.

array $persistent_data: (optional) Extra information to send to the Entity Browser Widget. This is needed as the widget may display after a new bootstrap, which would discard the current form state. Arbitrary values can be added and used by widgets, if needed. The expected array keys are "selected_entities" and "validators".

  • Drupal\Core\Entity\EntityInterface[] selected_entities An array of currently selected entities.
  • array validators An associative array mapping EntityBrowserWidgetValidation IDs to an array of options to pass to the plugin's validate method.

Return value

array A render array.

Overrides DisplayBase::displayEntityBrowser

1 method overrides IFrame::displayEntityBrowser()
Modal::displayEntityBrowser in src/Plugin/EntityBrowser/Display/Modal.php
Displays entity browser.

File

src/Plugin/EntityBrowser/Display/IFrame.php, line 140

Class

IFrame
Presents entity browser in an iFrame.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\Display

Code

public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
  parent::displayEntityBrowser($element, $form_state, $complete_form, $persistent_data);

  /** @var \Drupal\entity_browser\Events\RegisterJSCallbacks $event */
  $js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $this
    ->getUuid());
  $js_event_object
    ->registerCallback('Drupal.entityBrowser.selectionCompleted');
  $callback_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' => [
      'href' => '#browser',
      'class' => [
        'entity-browser-handle',
        'entity-browser-iframe',
      ],
      'data-uuid' => $this
        ->getUuid(),
      'data-original-path' => $original_path,
    ],
  ];
  $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',
    ],
    '#attributes' => [
      'class' => [
        'entity-browser-iframe-container',
      ],
    ],
    'link' => [
      '#type' => 'html_tag',
      '#tag' => 'a',
      '#value' => $this->configuration['link_text'],
      '#attributes' => $data['attributes'],
      '#attached' => [
        'library' => [
          'entity_browser/iframe',
        ],
        'drupalSettings' => [
          'entity_browser' => [
            $this
              ->getUuid() => [
              'auto_open' => $this->configuration['auto_open'],
            ],
            'iframe' => [
              $this
                ->getUuid() => [
                'src' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])
                  ->toString(),
                'width' => $this->configuration['width'],
                'height' => $this->configuration['height'],
                'js_callbacks' => $callback_event
                  ->getCallbacks(),
                'entity_browser_id' => $this->configuration['entity_browser_id'],
                'auto_open' => $this->configuration['auto_open'],
              ],
            ],
          ],
        ],
      ],
    ],
  ];
}