forked from SU-SWS/stanford_media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanford_media.post_update.php
416 lines (364 loc) · 14.1 KB
/
stanford_media.post_update.php
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* @file
* stanford_media.post_update.php
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Template\Attribute;
use Drupal\editor\Entity\Editor;
use Drupal\embed\Entity\EmbedButton;
use Drupal\entity_browser\Entity\EntityBrowser;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\media\Entity\MediaType;
use Drupal\views\Entity\View;
use Drupal\image\Entity\ImageStyle;
/**
* Convert media browser field widgets to media library widgets.
*/
function stanford_media_post_update_8200() {
\Drupal::service('module_installer')->install(['media_library']);
/** @var \Drupal\config_readonly\Config\ConfigReadonlyStorage $config_storage */
$config_storage = \Drupal::service('config.storage.staging');
if (!FieldStorageConfig::load('media.field_media_oembed_video')) {
$field_storage = [
'field_name' => 'field_media_oembed_video',
'entity_type' => 'media',
'type' => 'string',
];
// If config sync directory has a configuration for the field, make sure to
// use the UUID from that. If we don't, the field storage table will be
// deleted during config sync and all data will be lost.
if ($config_storage->exists('field.storage.media.field_media_oembed_video')) {
$staged_config = $config_storage->read('field.storage.media.field_media_oembed_video');
$field_storage['uuid'] = $staged_config['uuid'];
}
FieldStorageConfig::create($field_storage)->save();
}
if (!FieldConfig::load('media.video.field_media_oembed_video')) {
$field_config = [
'field_name' => 'field_media_oembed_video',
'label' => 'Video URL',
'entity_type' => 'media',
'bundle' => 'video',
'required' => TRUE,
];
if ($config_storage->exists('field.field.media.video.field_media_oembed_video')) {
$staged_config = $config_storage->read('field.field.media.video.field_media_oembed_video');
$field_config['uuid'] = $staged_config['uuid'];
}
FieldConfig::create($field_config)->save();
}
$media_library_widget = ['type' => 'media_library_widget'];
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
foreach (EntityFormDisplay::loadMultiple() as $form_display) {
$form_modified = FALSE;
// Change all media reference field widgets to use the media library widget.
foreach ($form_display->getComponents() as $component_name => $settings) {
if (isset($settings['type']) && $settings['type'] == 'entity_browser_entity_reference') {
$media_library_widget['weight'] = $settings['weight'];
$form_display->setComponent($component_name, $media_library_widget);
$form_modified = TRUE;
}
}
// Hide the old field, and add the new field to the form display.
if ($form_display->getTargetEntityTypeId() == 'media' && $form_display->getTargetBundle() == 'video') {
$form_display->removeComponent('field_media_video_embed_field');
$form_display->setComponent('field_media_oembed_video');
$form_modified = TRUE;
}
if ($form_modified) {
$form_display->save();
}
}
foreach (EntityViewDisplay::loadMultiple() as $view_display) {
if ($view_display->getTargetEntityTypeId() == 'media' && $view_display->getTargetBundle() == 'video') {
$view_display->removeComponent('field_media_video_embed_field');
$view_display->setComponent('field_media_oembed_video', [
'type' => 'oembed',
'label' => 'hidden',
]);
$view_display->save();
}
}
drupal_flush_all_caches();
$source_config = [
'source_field' => 'field_media_oembed_video',
'providers' => ['YouTube', 'Vimeo'],
'thumbnails_directory' => 'public://oembed_thumbnails',
];
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::load('video');
$media_type->set('source_configuration', $source_config);
$media_type->set('source', 'oembed:video');
$media_type->calculateDependencies();
$media_type->save();
}
/**
* Update text formats by switching the media buttons.
*/
function stanford_media_post_update_8201() {
/** @var \Drupal\editor\EditorInterface $editor */
foreach (Editor::loadMultiple() as $editor) {
$settings = $editor->getSettings();
foreach ($settings['toolbar']['rows'] as &$row_items) {
foreach ($row_items as &$row_group) {
if ($browser_position = array_search('media_browser', $row_group['items'])) {
$row_group['items'][$browser_position] = 'DrupalMediaLibrary';
$editor->setSettings($settings);
$editor->save();
}
}
}
}
$display_modes = ['full'];
$media_settings = \Drupal::configFactory()
->getEditable('stanford_media.settings');
foreach ($media_settings->get('embeddable_image_styles') as $image_style) {
$display_modes[] = _stanford_media_post_update_8202_image_display_mode($image_style);
}
$display_modes = array_unique($display_modes);
$media_settings->delete();
/** @var \Drupal\filter\FilterFormatInterface $filter */
foreach (FilterFormat::loadMultiple() as $filter) {
$filters = $filter->get('filters');
if (isset($filters['entity_embed'])) {
$filters['media_embed'] = [
'id' => 'media_embed',
'provider',
'media',
'status' => TRUE,
'weight' => $filters['entity_embed']['weight'],
'settings' => [
'default_view_mode' => 'full',
'allowed_view_modes' => array_combine($display_modes, $display_modes),
],
];
// Make sure if the filter strips html tags, we need to keep the
// drupal-media tag and its attributes.
if (isset($filters['filter_html'])) {
$filters['filter_html']['settings']['allowed_html'] .= ' <drupal-media data-entity-type data-entity-uuid data-align data-caption data-* alt>';
}
$filter->set('filters', $filters);
$filter->save();
}
}
}
/**
* Convert drupal-entity tags in wysiwyg's to drupal-media tags.
*/
function stanford_media_post_update_8202(&$sandbox) {
if (!isset($sandbox['count'])) {
$sandbox['entities'] = _stanford_media_post_update_8202_entity_list();
$sandbox['count'] = count($sandbox['entities']);
}
$entity_type_manager = \Drupal::entityTypeManager();
$entity_ids = array_splice($sandbox['entities'], 0, 25);
foreach ($entity_ids as $item) {
[$entity_type, $field_name, $entity_id] = explode(':', $item);
$entity = $entity_type_manager->getStorage($entity_type)->load($entity_id);
$field_values = $entity->get($field_name)->getValue();
foreach ($field_values as &$field_value) {
$field_value['value'] = _stanford_media_post_update_8202_change_tag($field_value['value']);
}
$entity->set($field_name, $field_values);
$entity->save();
}
$sandbox['#finished'] = empty($sandbox['entities']) ? 1 : ($sandbox['count'] - count($sandbox['entities'])) / $sandbox['count'];
}
/**
* Parse the drupal-entity tag, and change it into drupal-media tag.
*
* @param string $html
* Original html markup
*
* @return string
* New html markup with corrected tokens.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _stanford_media_post_update_8202_change_tag($html) {
preg_match_all("/<drupal-entity.*?data-entity-type=\"media\".*?\/drupal-entity>/s", $html, $tokens);
foreach ($tokens[0] as $token) {
$token_dom = Html::load($token);
$token_element = $token_dom->getElementsByTagName('drupal-entity')->item(0);
$attributes = [
'data-entity-uuid',
'alt',
'data-align',
];
$new_token_attributes = new Attribute();
$new_token_attributes->setAttribute('data-entity-type', 'media');
foreach ($attributes as $attribute) {
if ($value = $token_element->getAttribute($attribute)) {
$new_token_attributes->setAttribute($attribute, $value);
}
}
$display_settings = json_decode($token_element->getAttribute('data-entity-embed-display-settings'), TRUE);
if (isset($display_settings['image_style'])) {
$new_token_attributes->setAttribute('data-view-mode', _stanford_media_post_update_8202_image_display_mode($display_settings['image_style']));
}
if (isset($display_settings['description'])) {
$new_token_attributes->setAttribute('data-display-description', $display_settings['description']);
}
if ($caption_data = htmlspecialchars_decode($token_element->getAttribute('data-caption'))) {
$json_caption = json_decode($caption_data, TRUE);
if (is_array($json_caption)) {
$caption_text = strip_tags(check_markup($json_caption['value'], $json_caption['format']), '<a>');
$new_token_attributes->setAttribute('data-caption', $caption_text ?: $json_caption['value']);
}
else {
$new_token_attributes->setAttribute('data-caption', $caption_data);
}
}
$new_token = "<drupal-media$new_token_attributes></drupal-media>";
if (!empty($display_settings['linkit']['href'])) {
$link_attributes = new Attribute($display_settings['linkit']);
$new_token = "<a$link_attributes>$new_token</a>";
}
$html = str_replace($token, $new_token, $html);
}
return $html;
}
/**
* Create a display mode for each image style.
*
* @param string $image_style
* Image style id.
*
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
* The existing or new entity view display object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _stanford_media_post_update_8202_image_display_mode($image_style) {
$view_modes = &drupal_static(__FUNCTION__, []);
if (empty($view_modes)) {
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
foreach (EntityViewDisplay::loadMultiple() as $display) {
if ($display->getTargetEntityTypeId() != 'media' || $display->getTargetBundle() != 'image') {
continue;
}
$field_display = $display->getComponent('field_media_image');
if ($field_display && isset($field_display['settings']['image_style'])) {
$view_modes[$field_display['settings']['image_style']] = $display->getMode();
}
}
}
if (!isset($view_modes[$image_style])) {
$image_style_entity = ImageStyle::load($image_style);
EntityViewMode::create([
'id' => 'media.stanford_image_' . $image_style,
'targetEntityType' => 'media',
'label' => $image_style_entity ? $image_style_entity->label() : $image_style,
])->save();
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $new_display */
$new_display = EntityViewDisplay::create([
'targetEntityType' => 'media',
'bundle' => 'image',
'mode' => 'stanford_image_' . $image_style,
'status' => TRUE,
]);
$settings = [
'label' => 'hidden',
'type' => 'image',
'settings' => ['image_style' => $image_style, 'image_link' => ''],
];
$new_display->setComponent('field_media_image', $settings);
$new_display->removeComponent('created');
$new_display->removeComponent('thumbnail');
$new_display->removeComponent('uid');
$new_display->save();
$view_modes[$image_style] = $new_display->getMode();
}
return $view_modes[$image_style];
}
/**
* Get the list of entities with drupal-entity tokens.
*
* @return array
* List of entities needing fixed.
*/
function _stanford_media_post_update_8202_entity_list() {
$list = [];
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = \Drupal::moduleHandler();
$module_handler->loadInclude('stanford_media', 'install');
foreach (_stanford_media_update_8005_get_filter_fields() as $field) {
[$entity_type, $field_name] = explode(':', $field);
try {
$entity_ids = \Drupal::entityQuery($entity_type)
->condition($field_name, '<drupal-entity', 'CONTAINS')
->execute();
}
catch (Exception $e) {
\Drupal::logger('stanford_media')
->warning(t('Unable to query for %type entities with the field %field'), [
'%type' => $entity_type,
'%field' => $field_name,
]);
continue;
}
foreach ($entity_ids as $entity_id) {
$list[] = "$entity_type:$field_name:$entity_id";
}
}
asort($list);
return array_unique($list);
}
/**
* Migrate video data to core supported fields.
*/
function stanford_media_post_update_8203(&$sandbox) {
if (!isset($sandbox['count'])) {
$sandbox['entities'] = \Drupal::entityTypeManager()
->getStorage('media')
->loadByProperties(['bundle' => 'video']);
$sandbox['count'] = count($sandbox['entities']);
}
$video_medias = array_splice($sandbox['entities'], 0, 25);
/** @var \Drupal\media\MediaInterface $media */
foreach ($video_medias as $media) {
if (empty($media->get('field_media_oembed_video')->getString())) {
$video_url = $media->get('field_media_video_embed_field')->getString();
$media->set('field_media_oembed_video', $video_url);
$media->save();
}
}
$sandbox['#finished'] = empty($sandbox['entities']) ? 1 : ($sandbox['count'] - count($sandbox['entities'])) / $sandbox['count'];
}
/**
* Delete unused configs now.
*/
function stanford_media_post_update_8204() {
$browsers = EntityBrowser::loadMultiple([
'file_browser',
'image_browser',
'media_browser',
'video_browser',
]);
foreach ($browsers as $browser) {
$browser->delete();
}
if ($button = EmbedButton::load('media_browser')) {
$button->delete();
}
if ($view = View::load('media_entity_browser')) {
$view->delete();
}
\Drupal::messenger()
->addMessage(t('Review these modules to see if they still require being enabled: entity_browser, embed, & entity_embed'));
// Delete the old video fields & then reset the media type dependencies.
FieldConfig::load('media.video.field_media_video_embed_field')->delete();
if ($storage = FieldStorageConfig::load('media.field_media_video_embed_field')) {
$storage->delete();
}
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::load('video');
$media_type->calculateDependencies();
$media_type->save();
}