Completed
Push — 8.x-1.x ( 2f666a...42c2bb )
by Janez
02:29
created

Modal::path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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