-
Notifications
You must be signed in to change notification settings - Fork 7
/
oe_media.post_update.php
108 lines (93 loc) · 3.78 KB
/
oe_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
<?php
/**
* @file
* Post update functions for OpenEuropa Media module.
*/
declare(strict_types=1);
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
/**
* Update allowed file extensions.
*/
function oe_media_post_update_allowed_file_extensions(): void {
$field = FieldConfig::load('media.document.oe_media_file');
$field->setSetting('file_extensions', 'txt text md readme info doc dot docx dotx docm dotm xls xlt xla xlsx xltx xlsm xltm xlam xlsb ppt pot pps ppa pptx potx ppsx ppam pptm potm ppsm pdf ods odt odf');
$field->save();
}
/**
* Move the document files to the private file system.
*/
function oe_media_post_update_document_private_files(): void {
$field = FieldStorageConfig::load('media.oe_media_file');
if ($field instanceof FieldStorageConfigInterface && $field->getSetting('uri_scheme') !== 'private') {
$field->setSetting('uri_scheme', 'private');
$field->save();
}
}
/**
* Add new fields to document media bundle to allow remote files.
*/
function oe_media_post_update_00001(array &$sandbox) {
// Enable file_link module.
$module_installer = \Drupal::service('module_installer');
$module_installer->install(['options', 'file_link']);
\Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
\Drupal::service('kernel')->invalidateContainer();
// Create the new fields.
$entity_type_manager = \Drupal::entityTypeManager();
$config_manager = \Drupal::service('config.manager');
$storage = new FileStorage(\Drupal::service('extension.list.module')->getPath('oe_media') . '/config/post_updates/00001_remote_file');
$field_configs = [
'field.storage.media.oe_media_file_type',
'field.storage.media.oe_media_remote_file',
'field.field.media.document.oe_media_file_type',
'field.field.media.document.oe_media_remote_file',
];
foreach ($field_configs as $config) {
$config_record = $storage->read($config);
$entity_type = $config_manager->getEntityTypeIdByName($config);
$entity_storage = $entity_type_manager->getStorage($entity_type);
if (!$entity_storage->load($config_record['id'])) {
$entity = $entity_storage->createFromStorageRecord($config_record);
$entity->save();
}
}
// Set the file field optional.
$field_config = FieldConfig::load('media.document.oe_media_file');
$field_config->setRequired(FALSE);
$field_config->save();
$entity_type_manager->clearCachedDefinitions();
}
/**
* Set all the existing Document media entities to local files.
*/
function oe_media_post_update_00002(array &$sandbox) {
$media_storage = \Drupal::entityTypeManager()->getStorage('media');
if (!isset($sandbox['total'])) {
$query = $media_storage->getQuery();
$query->condition('bundle', 'document');
$document_ids = $query->accessCheck(FALSE)->execute();
if (empty($document_ids)) {
// We don't have to do anything if there are no document media entities.
$sandbox['#finished'] = 1;
return t('There are no Document media entities to update.');
}
$sandbox['current'] = 0;
$sandbox['documents_per_batch'] = 5;
$sandbox['document_ids'] = $document_ids;
$sandbox['total'] = count($sandbox['document_ids']);
}
$ids = array_slice($sandbox['document_ids'], $sandbox['current'], $sandbox['documents_per_batch']);
$documents_to_update = $media_storage->loadMultiple($ids);
foreach ($documents_to_update as $document) {
$document->set('oe_media_file_type', 'local');
$document->save();
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : ($sandbox['current'] / $sandbox['total']);
if ($sandbox['#finished'] === 1) {
return t('All the existing document media entities have been set to local files.');
}
}