forked from SU-SWS/stanford_media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanford_media.install
273 lines (235 loc) · 8.38 KB
/
stanford_media.install
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
<?php
/**
* @file
* stanford_media.install
*/
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\views\Entity\View;
use Drupal\media\Entity\MediaType;
/**
* Enable new submodule.
*/
function stanford_media_update_8001() {
\Drupal::service('module_installer')->install(['media_duplicate_validation']);
}
/**
* Update the media view.
*/
function stanford_media_update_8002() {
$view = View::load('media_entity_browser');
$list_display = &$view->getDisplay('use_list');
$list_display['display_options']['fields']['source_id'] = [
'id' => 'source_id',
'field' => 'source_id',
'group_type' => 'count_distinct',
];
$list_display['display_options']['fields']['source_id'] += $list_display['display_options']['fields']['count'];
unset($list_display['display_options']['fields']['count']);
// Move operations column to the end.
$operations = $list_display['display_options']['fields']['operations'];
unset($list_display['display_options']['fields']['operations']);
$list_display['display_options']['fields']['operations'] = $operations;
$view->save();
}
/**
* Change video embed field thumbnail to a lazy loading video.
*/
function stanford_media_update_8003() {
$display = EntityViewDisplay::load('media.video.default');
$display_options = [
'label' => 'hidden',
'type' => 'video_embed_field_lazyload',
'settings' => [
'image_style' => 'medium',
'autoplay' => TRUE,
'width' => 200,
'height' => 133,
'responsive' => FALSE,
],
];
$display->setComponent('field_media_video_embed_field', $display_options);
$display->save();
}
/**
* Add `alt` and `title` attributes to the embed filter formats.
*/
function stanford_media_update_8004() {
$entity_type_manager = \Drupal::entityTypeManager();
$formats = $entity_type_manager->getStorage('filter_format')
->loadMultiple();
/** @var \Drupal\filter\Entity\FilterFormat $filter_format */
foreach ($formats as $filter_format) {
$filters = $filter_format->get('filters');
// Only need to change it if the format filters html and allows embeded
// entities.
if (isset($filters['filter_html']) && isset($filters['entity_embed'])) {
preg_match("/<drupal-entity.*?>/", $filters['filter_html']['settings']['allowed_html'], $drupal_entity_tag);
// Only need to check for alt since the UI has validation for both. If the
// filter was modified in the UI, it will have both alt & title.
if ($drupal_entity_tag && strpos($drupal_entity_tag[0], ' alt') === FALSE) {
$filters['filter_html']['settings']['allowed_html'] = str_replace('<drupal-entity', '<drupal-entity alt title', $filters['filter_html']['settings']['allowed_html']);
$filter_format->set('filters', $filters);
$filter_format->save();
}
}
}
}
/**
* Move inline image alt text into a contrib supported attribute.
*
* This doesn't edit any revisions.
*/
function stanford_media_update_8005(&$sandbox) {
if (!isset($sandbox['count'])) {
$sandbox['entities'] = _stanford_media_update_8005_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_storage = $entity_type_manager->getStorage($entity_type);
$entity = $entity_storage->load($entity_id);
$field_values = $entity->get($field_name)->getValue();
foreach ($field_values as &$field_value) {
$field_value['value'] = _stanford_media_update_8005_fix_alt($field_value['value']);
}
$entity->set($field_name, $field_values);
$entity->save();
}
$sandbox['#finished'] = empty($sandbox['entities']) ? 1 : ($sandbox['count'] - count($sandbox['entities'])) / $sandbox['count'];
}
/**
* Get the list of entities needing updates.
*
* @return array
* List of values with entity type, field and id.
*/
function _stanford_media_update_8005_entity_list() {
$list = [];
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, '%alt_text"%', 'LIKE')
->execute();
}
catch (Exception $e) {
continue;
}
foreach ($entity_ids as $entity_id) {
$list[] = "$entity_type:$field_name:$entity_id";
}
}
asort($list);
return array_unique($list);
}
/**
* Get a list of all fields that are filtered.
*
* @return array
* Array of values with entity type and field name.
*/
function _stanford_media_update_8005_get_filter_fields() {
$field_manager = \Drupal::service('entity_field.manager');
$filtered_text_fields = ['text', 'text_long', 'text_with_summary'];
$filtered_fields = [];
foreach ($field_manager->getFieldMap() as $entity_type => $fields) {
foreach ($fields as $field_name => $field_info) {
if (!in_array($field_info['type'], $filtered_text_fields)) {
continue;
}
$filtered_fields[] = "$entity_type:$field_name";
}
}
asort($filtered_fields);
return array_unique($filtered_fields);
}
/**
* Move the alt_text value in the json data into an `alt` attribute.
*
* @param string $html
* Original Html with the embed entity tag.
*
* @return string
* Fixed html.
*/
function _stanford_media_update_8005_fix_alt($html) {
preg_match_all("/<drupal-entity.*?\/drupal-entity>/s", $html, $tokens);
foreach ($tokens[0] as $token) {
// Existing tokens already use the contrib attribute or dont use an alt at
// all.
if (strpos($token, ' alt="') !== FALSE || strpos($token, 'alt_text"') === FALSE) {
continue;
}
$new_token = $token;
preg_match("/data-entity-embed-display-settings=\".*?\"/", $new_token, $settings);
$display_settings = str_replace('data-entity-embed-display-settings="', '', $settings[0]);
$display_settings = substr($display_settings, 0, -1);
$display_settings = json_decode(htmlspecialchars_decode($display_settings), TRUE);
$alt_text = $display_settings['alt_text'];
unset($display_settings['alt_text']);
$new_token = str_replace($settings[0], 'data-entity-embed-display-settings="' . htmlspecialchars(json_encode($display_settings)) . '"', $new_token);
$new_token = str_replace('<drupal-entity ', "<drupal-entity alt=\"$alt_text\" ", $new_token);
$html = str_replace($token, $new_token, $html);
}
return $html;
}
/**
* Merge two setting configs into one.
*/
function stanford_media_update_8006() {
$config_factory = \Drupal::configFactory();
$old_image_styles = $config_factory->getEditable('stanford_media.embeddable_image_styles');
$old_captions = $config_factory->getEditable('stanford_media.allowed_caption_formats');
$new_settings = $config_factory->getEditable('stanford_media.settings');
$new_settings->set('embeddable_image_styles', $old_image_styles->get('allowed_styles'));
$new_settings->set('allowed_caption_formats', $old_captions->get('allowed_formats'));
$new_settings->save();
$old_image_styles->delete();
$old_captions->delete();
}
/**
* Set database schema to 8200 for 2.x version.
*/
function stanford_media_update_8200() {
// Intentionally left empty.
}
/**
* Change media library form widget for Images.
*/
function stanford_media_update_8201(&$sandbox) {
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::load('image');
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form */
$form = EntityFormDisplay::load('media.image.media_library');
if (!$media_type || !$form) {
return;
}
$field_name = $media_type->getSource()->getConfiguration()['source_field'];
$form->removeComponent($field_name);
$options = [
'type' => 'image_focal_point',
'settings' => [
'progress_indicator' => 'throbber',
'preview_image_style' => 'thumbnail',
'preview_link' => FALSE,
'offsets' => '50,50',
],
];
$form->setComponent($field_name, $options);
$form->save();
}
/**
* Install oembed_providers module.
*/
function stanford_media_update_8202() {
\Drupal::service('module_installer')->install(['oembed_providers']);
}
/**
* Enable transliterate_filenames module.
*/
function stanford_media_update_8220() {
\Drupal::service('module_installer')->install(['transliterate_filenames']);
}