1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
<?php
namespace Drupal\media_library;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\views\ViewExecutableFactory;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Service which builds the media library.
*
* @internal
* This service is an internal part of the modal media library dialog and
* does not provide any extension points.
*/
class MediaLibraryUiBuilder {
use StringTranslationTrait;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The currently active request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The views executable factory.
*
* @var \Drupal\views\ViewExecutableFactory
*/
protected $viewsExecutableFactory;
/**
* The media library opener resolver.
*
* @var \Drupal\media_library\OpenerResolverInterface
*/
protected $openerResolver;
/**
* Constructs a MediaLibraryUiBuilder instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\views\ViewExecutableFactory $views_executable_factory
* The views executable factory.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The currently active request object.
* @param \Drupal\media_library\OpenerResolverInterface $opener_resolver
* The opener resolver.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack, ViewExecutableFactory $views_executable_factory, FormBuilderInterface $form_builder, OpenerResolverInterface $opener_resolver) {
$this->entityTypeManager = $entity_type_manager;
$this->request = $request_stack->getCurrentRequest();
$this->viewsExecutableFactory = $views_executable_factory;
$this->formBuilder = $form_builder;
$this->openerResolver = $opener_resolver;
}
/**
* Get media library dialog options.
*
* @return array
* The media library dialog options.
*/
public static function dialogOptions() {
return [
'classes' => [
'ui-dialog' => 'media-library-widget-modal',
],
'title' => t('Add or select media'),
'height' => '75%',
'width' => '75%',
];
}
/**
* Build the media library UI.
*
* @param \Drupal\media_library\MediaLibraryState $state
* (optional) The current state of the media library, derived from the
* current request.
*
* @return array
* The render array for the media library.
*/
public function buildUi(?MediaLibraryState $state = NULL) {
if (!$state) {
$state = MediaLibraryState::fromRequest($this->request);
}
// When navigating to a media type through the vertical tabs, we only want
// to load the changed library content. This is not only more efficient, but
// also provides a more accessible user experience for screen readers.
if ($state->get('media_library_content') === '1') {
return $this->buildLibraryContent($state);
}
else {
return [
'#theme' => 'media_library_wrapper',
'#attributes' => [
'id' => 'media-library-wrapper',
],
'menu' => $this->buildMediaTypeMenu($state),
'content' => $this->buildLibraryContent($state),
// Attach the JavaScript for the media library UI. The number of
// available slots needs to be added to make sure users can't select
// more items than allowed.
'#attached' => [
'library' => ['media_library/ui'],
'drupalSettings' => [
'media_library' => [
'selection_remaining' => $state->getAvailableSlots(),
],
],
],
];
}
}
/**
* Build the media library content area.
*
* @param \Drupal\media_library\MediaLibraryState $state
* The current state of the media library, derived from the current request.
*
* @return array
* The render array for the media library.
*/
protected function buildLibraryContent(MediaLibraryState $state) {
return [
'#type' => 'container',
'#theme_wrappers' => [
'container__media_library_content',
],
'#attributes' => [
'id' => 'media-library-content',
],
'form' => $this->buildMediaTypeAddForm($state),
'view' => $this->buildMediaLibraryView($state),
];
}
/**
* Check access to the media library.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Run access checks for this account.
* @param \Drupal\media_library\MediaLibraryState $state
* (optional) The current state of the media library, derived from the
* current request.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function checkAccess(AccountInterface $account, ?MediaLibraryState $state = NULL) {
if (!$state) {
try {
$state = MediaLibraryState::fromRequest($this->request);
}
catch (BadRequestHttpException $e) {
return AccessResult::forbidden($e->getMessage());
}
catch (\InvalidArgumentException $e) {
return AccessResult::forbidden($e->getMessage());
}
}
// Deny access if the view or display are removed.
$view = $this->entityTypeManager->getStorage('view')->load('media_library');
if (!$view) {
return AccessResult::forbidden('The media library view does not exist.')
->setCacheMaxAge(0);
}
if (!$view->getDisplay('widget')) {
return AccessResult::forbidden('The media library widget display does not exist.')
->addCacheableDependency($view);
}
// The user must at least be able to view media in order to access the media
// library.
$can_view_media = AccessResult::allowedIfHasPermission($account, 'view media')
->addCacheableDependency($view);
// Delegate any further access checking to the opener service nominated by
// the media library state.
return $this->openerResolver->get($state)->checkAccess($state, $account)
->andIf($can_view_media);
}
/**
* Get the media type menu for the media library.
*
* @param \Drupal\media_library\MediaLibraryState $state
* The current state of the media library, derived from the current request.
*
* @return array
* The render array for the media type menu.
*/
protected function buildMediaTypeMenu(MediaLibraryState $state) {
// Add the menu for each type if we have more than 1 media type enabled for
// the field.
$allowed_type_ids = $state->getAllowedTypeIds();
if (count($allowed_type_ids) <= 1) {
return [];
}
// @todo Add a class to the li element.
// https://www.drupal.org/project/drupal/issues/3029227
$menu = [
'#theme' => 'links__media_library_menu',
'#links' => [],
'#attributes' => [
'class' => ['js-media-library-menu'],
],
];
$allowed_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple($allowed_type_ids);
$selected_type_id = $state->getSelectedTypeId();
foreach ($allowed_types as $allowed_type_id => $allowed_type) {
$link_state = MediaLibraryState::create($state->getOpenerId(), $state->getAllowedTypeIds(), $allowed_type_id, $state->getAvailableSlots(), $state->getOpenerParameters());
// Add the 'media_library_content' parameter so the response will contain
// only the updated content for the tab.
// @see self::buildUi()
$link_state->set('media_library_content', 1);
$title = $allowed_type->label();
$display_title = $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span>', ['@title' => $title]);
if ($allowed_type_id === $selected_type_id) {
$display_title = $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span><span class="active-tab visually-hidden"> (selected)</span>', ['@title' => $title]);
}
$menu['#links']['media-library-menu-' . $allowed_type_id] = [
'title' => $display_title,
'url' => Url::fromRoute('media_library.ui', [], [
'query' => $link_state->all(),
]),
'attributes' => [
'role' => 'button',
'data-title' => $title,
],
];
}
// Set the active menu item.
$menu['#links']['media-library-menu-' . $selected_type_id]['attributes']['class'][] = 'active';
return $menu;
}
/**
* Get the add form for the selected media type.
*
* @param \Drupal\media_library\MediaLibraryState $state
* The current state of the media library, derived from the current request.
*
* @return array
* The render array for the media type add form.
*/
protected function buildMediaTypeAddForm(MediaLibraryState $state) {
$selected_type_id = $state->getSelectedTypeId();
$access_handler = $this->entityTypeManager->getAccessControlHandler('media');
$context = [
'media_library_state' => $state,
];
if (!$access_handler->createAccess($selected_type_id, NULL, $context)) {
return [];
}
$selected_type = $this->entityTypeManager->getStorage('media_type')->load($selected_type_id);
$plugin_definition = $selected_type->getSource()->getPluginDefinition();
if (empty($plugin_definition['forms']['media_library_add'])) {
return [];
}
// After the form to add new media is submitted, we need to rebuild the
// media library with a new instance of the media add form. The form API
// allows us to do that by forcing empty user input.
// @see \Drupal\Core\Form\FormBuilder::doBuildForm()
$form_state = new FormState();
if ($state->get('_media_library_form_rebuild')) {
$form_state->setUserInput([]);
$state->remove('_media_library_form_rebuild');
}
$form_state->set('media_library_state', $state);
return $this->formBuilder->buildForm($plugin_definition['forms']['media_library_add'], $form_state);
}
/**
* Get the media library view.
*
* @param \Drupal\media_library\MediaLibraryState $state
* The current state of the media library, derived from the current request.
*
* @return array
* The render array for the media library view.
*/
protected function buildMediaLibraryView(MediaLibraryState $state) {
// @todo Make the view configurable in
// https://www.drupal.org/project/drupal/issues/2971209
$view = $this->entityTypeManager->getStorage('view')->load('media_library');
$view_executable = $this->viewsExecutableFactory->get($view);
$display_id = $state->get('views_display_id', 'widget');
// Make sure the state parameters are set in the request so the view can
// pass the parameters along in the pager, filters etc.
$view_request = $view_executable->getRequest();
$view_request->query->add($state->all());
$view_executable->setRequest($view_request);
$args = [$state->getSelectedTypeId()];
$view_executable->setDisplay($display_id);
$view_executable->preExecute($args);
$view_executable->execute($display_id);
return $view_executable->buildRenderable($display_id, $args, FALSE);
}
}
|