Modal::buildConfigurationForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
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
        'frameborder' => 0,
120
        'style' => 'padding:0; position:relative; z-index:10002;',
121
        'name' => $name,
122
        'id' => $name,
123
      ],
124
    ];
125
    if (!empty($this->configuration['height']) && is_numeric($this->configuration['height']) && $this->configuration['height'] > 90) {
126
      $content['#attributes']['height'] = $this->configuration['height'] - 90;
127
    }
128
    $html = drupal_render($content);
129
130
    $response = new AjaxResponse();
131
    $response->addCommand(new OpenDialogCommand('#' . Html::getUniqueId($field_name . '-' . $element_name . '-dialog'), $this->configuration['link_text'], $html, [
132
      'width' => 'auto',
133
      'height' => 'auto',
134
      'modal' => TRUE,
135
      'maxWidth' => $this->configuration['width'],
136
      'maxHeight' => $this->configuration['height'],
137
      'fluid' => 1,
138
      'autoResize' => 0,
139
      'resizable' => 0,
140
    ]));
141
    return $response;
142
  }
143
144
  /**
145
   * {@inheritdoc}
146
   */
147
  public function __sleep() {
148
    return ['configuration'];
149
  }
150
151
  /**
152
   * {@inheritdoc}
153
   */
154
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
155
    $configuration = $this->getConfiguration();
156
157
    $form = parent::buildConfigurationForm($form, $form_state);
158
159
    $form['width'] = [
160
      '#type' => 'number',
161
      '#title' => $this->t('Width of the modal'),
162
      '#default_value' => $configuration['width'],
163
      '#description' => $this->t('Empty value for responsive width.'),
164
    ];
165
    $form['height'] = [
166
      '#type' => 'number',
167
      '#title' => $this->t('Height of the modal'),
168
      '#default_value' => $configuration['height'],
169
      '#description' => $this->t('Empty value for responsive height.'),
170
    ];
171
    $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.');
172
173
    return $form;
174
  }
175
176
  /**
177
   * {@inheritdoc}
178
   */
179
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
180
181
}
182