3e855a55890c94522fbfa812f56f6cb79602bf69
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / EntityBrowser / Display / Modal.php
1 <?php
2
3 namespace Drupal\entity_browser\Plugin\EntityBrowser\Display;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Ajax\OpenDialogCommand;
8 use Drupal\Core\Url;
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;
15
16 /**
17  * Presents entity browser in an Modal.
18  *
19  * @EntityBrowserDisplay(
20  *   id = "modal",
21  *   label = @Translation("Modal"),
22  *   description = @Translation("Displays the entity browser in a modal window."),
23  *   uses_route = TRUE
24  * )
25  */
26 class Modal extends IFrame {
27
28   /**
29    * {@inheritdoc}
30    */
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();
37
38     $data = [
39       'query_parameters' => [
40         'query' => [
41           'uuid' => $this->getUuid(),
42           'original_path' => $original_path,
43         ],
44       ],
45       'attributes' => [
46         'data-uuid' => $this->getUuid(),
47       ],
48     ];
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();
52     return [
53       '#theme_wrappers' => ['container'],
54       'path' => [
55         '#type' => 'hidden',
56         '#value' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])->toString(),
57       ],
58       'open_modal' => [
59         '#type' => 'submit',
60         '#value' => $this->configuration['link_text'],
61         '#limit_validation_errors' => [],
62         '#submit' => [],
63         '#name' => implode('_', $element['#eb_parents']),
64         '#ajax' => [
65           'callback' => [$this, 'openModal'],
66           'event' => 'click',
67         ],
68         '#executes_submit_callback' => FALSE,
69         '#attributes' => $data['attributes'],
70         '#attached' => [
71           'library' => ['core/drupal.dialog.ajax', 'entity_browser/modal'],
72           'drupalSettings' => [
73             'entity_browser' => [
74               'modal' => [
75                 $this->getUuid() => [
76                   'uuid' => $this->getUuid(),
77                   'js_callbacks' => $js_event->getCallbacks(),
78                   'original_path' => $original_path,
79                   'auto_open' => $this->configuration['auto_open'],
80                 ],
81               ],
82             ],
83           ],
84         ],
85       ],
86     ];
87   }
88
89   /**
90    * Generates the content and opens the modal.
91    *
92    * @param array $form
93    *   The form array.
94    * @param \Drupal\Core\Form\FormStateInterface $form_state
95    *   The form state object.
96    *
97    * @return \Drupal\Core\Ajax\AjaxResponse
98    *   An ajax response.
99    */
100   public function openModal(array &$form, FormStateInterface $form_state) {
101     $triggering_element = $form_state->getTriggeringElement();
102     $parents = $triggering_element['#parents'];
103     array_pop($parents);
104     $parents = array_merge($parents, ['path']);
105     $input = $form_state->getUserInput();
106     $src = NestedArray::getValue($input, $parents);
107
108     $field_name = $triggering_element['#parents'][0];
109     $element_name = $this->configuration['entity_browser_id'];
110     $name = 'entity_browser_iframe_' . $element_name;
111     $content = [
112       '#prefix' => '<div class="ajax-progress-throbber"></div>',
113       '#type' => 'html_tag',
114       '#tag' => 'iframe',
115       '#attributes' => [
116         'src' => $src,
117         'class' => 'entity-browser-modal-iframe',
118         'width' => '100%',
119         'height' => $this->configuration['height'] - 90,
120         'frameborder' => 0,
121         'style' => 'padding:0; position:relative; z-index:10002;',
122         'name' => $name,
123         'id' => $name,
124       ],
125     ];
126     $html = drupal_render($content);
127
128     $response = new AjaxResponse();
129     $response->addCommand(new OpenDialogCommand('#' . Html::getUniqueId($field_name . '-' . $element_name . '-dialog'), $this->configuration['link_text'], $html, [
130       'width' => 'auto',
131       'height' => 'auto',
132       'modal' => TRUE,
133       'maxWidth' => $this->configuration['width'],
134       'maxHeight' => $this->configuration['height'],
135       'fluid' => 1,
136       'autoResize' => 0,
137       'resizable' => 0,
138     ]));
139     return $response;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function __sleep() {
146     return ['configuration'];
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
153     $configuration = $this->getConfiguration();
154
155     $form = parent::buildConfigurationForm($form, $form_state);
156
157     $form['width'] = [
158       '#type' => 'number',
159       '#title' => $this->t('Width of the modal'),
160       '#default_value' => $configuration['width'],
161       '#description' => $this->t('Empty value for responsive width.'),
162     ];
163     $form['height'] = [
164       '#type' => 'number',
165       '#title' => $this->t('Height of the modal'),
166       '#default_value' => $configuration['height'],
167       '#description' => $this->t('Empty value for responsive height.'),
168     ];
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.');
170
171     return $form;
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
178
179 }