3 namespace Drupal\entity_browser\Plugin\EntityBrowser\Display;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Ajax\OpenDialogCommand;
9 use Drupal\entity_browser\DisplayBase;
10 use Drupal\entity_browser\Events\Events;
11 use Drupal\entity_browser\Events\RegisterJSCallbacks;
12 use Drupal\Core\Ajax\AjaxResponse;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\entity_browser\Events\AlterEntityBrowserDisplayData;
17 * Presents entity browser in an Modal.
19 * @EntityBrowserDisplay(
21 * label = @Translation("Modal"),
22 * description = @Translation("Displays the entity browser in a modal window."),
26 class Modal extends IFrame {
31 public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
32 DisplayBase::displayEntityBrowser($element, $form_state, $complete_form, $persistent_data);
33 $js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $this->getUuid());
34 $js_event_object->registerCallback('Drupal.entityBrowser.selectionCompleted');
35 $js_event = $this->eventDispatcher->dispatch(Events::REGISTER_JS_CALLBACKS, $js_event_object);
36 $original_path = $this->currentPath->getPath();
39 'query_parameters' => [
41 'uuid' => $this->getUuid(),
42 'original_path' => $original_path,
46 'data-uuid' => $this->getUuid(),
49 $event_object = new AlterEntityBrowserDisplayData($this->configuration['entity_browser_id'], $this->getUuid(), $this->getPluginDefinition(), $form_state, $data);
50 $event = $this->eventDispatcher->dispatch(Events::ALTER_BROWSER_DISPLAY_DATA, $event_object);
51 $data = $event->getData();
53 '#theme_wrappers' => ['container'],
56 '#value' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])->toString(),
60 '#value' => $this->configuration['link_text'],
61 '#limit_validation_errors' => [],
63 '#name' => implode('_', $element['#eb_parents']),
65 'callback' => [$this, 'openModal'],
68 '#executes_submit_callback' => FALSE,
69 '#attributes' => $data['attributes'],
71 'library' => ['core/drupal.dialog.ajax', 'entity_browser/modal'],
76 'uuid' => $this->getUuid(),
77 'js_callbacks' => $js_event->getCallbacks(),
78 'original_path' => $original_path,
79 'auto_open' => $this->configuration['auto_open'],
90 * Generates the content and opens the modal.
94 * @param \Drupal\Core\Form\FormStateInterface $form_state
95 * The form state object.
97 * @return \Drupal\Core\Ajax\AjaxResponse
100 public function openModal(array &$form, FormStateInterface $form_state) {
101 $triggering_element = $form_state->getTriggeringElement();
102 $parents = $triggering_element['#parents'];
104 $parents = array_merge($parents, ['path']);
105 $input = $form_state->getUserInput();
106 $src = NestedArray::getValue($input, $parents);
108 $field_name = $triggering_element['#parents'][0];
109 $element_name = $this->configuration['entity_browser_id'];
110 $name = 'entity_browser_iframe_' . $element_name;
112 '#prefix' => '<div class="ajax-progress-throbber"></div>',
113 '#type' => 'html_tag',
117 'class' => 'entity-browser-modal-iframe',
119 'height' => $this->configuration['height'] - 90,
121 'style' => 'padding:0; position:relative; z-index:10002;',
126 $html = drupal_render($content);
128 $response = new AjaxResponse();
129 $response->addCommand(new OpenDialogCommand('#' . Html::getUniqueId($field_name . '-' . $element_name . '-dialog'), $this->configuration['link_text'], $html, [
133 'maxWidth' => $this->configuration['width'],
134 'maxHeight' => $this->configuration['height'],
145 public function __sleep() {
146 return ['configuration'];
152 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
153 $configuration = $this->getConfiguration();
155 $form = parent::buildConfigurationForm($form, $form_state);
159 '#title' => $this->t('Width of the modal'),
160 '#default_value' => $configuration['width'],
161 '#description' => $this->t('Empty value for responsive width.'),
165 '#title' => $this->t('Height of the modal'),
166 '#default_value' => $configuration['height'],
167 '#description' => $this->t('Empty value for responsive height.'),
169 $form['auto_open']['#description'] = $this->t('Will open Entity browser modal as soon as page is loaded, which might cause unwanted results. Should be used only in very specific cases such as Inline entity form integration. It is also advised not to use Entity browsers with this option enabled more than once per page.');
177 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}