You are here

public function Modal::displayEntityBrowser in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/EntityBrowser/Display/Modal.php \Drupal\entity_browser\Plugin\EntityBrowser\Display\Modal::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 IFrame::displayEntityBrowser

File

src/Plugin/EntityBrowser/Display/Modal.php, line 31

Class

Modal
Presents entity browser in an Modal.

Namespace

Drupal\entity_browser\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->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' => [
          'core/drupal.dialog.ajax',
          'entity_browser/modal',
        ],
        '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'],
              ],
            ],
          ],
        ],
      ],
    ],
  ];
}