This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
/
imgix.install
244 lines (205 loc) · 6.81 KB
/
imgix.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
<?php
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\image\Entity\ImageStyle;
use Drupal\imgix\ImgixImageStyleFactoryInterface;
use Drupal\user\Entity\Role;
/**
* Implements @see hook_update_last_removed().
*/
function imgix_update_last_removed(): int
{
return 8001;
}
/**
* Replace 'administer imgix' permission with 'administer image styles'
*/
function imgix_update_8002(): void
{
foreach (Role::loadMultiple() as $role) {
if (!$role->hasPermission('administer imgix')) {
continue;
}
$role->revokePermission('administer imgix');
$role->grantPermission('administer image styles');
$role->save();
}
}
/**
* Remove the 'mapping_url' option
*/
function imgix_update_8003(): void
{
\Drupal::configFactory()
->getEditable('imgix.settings')
->clear('mapping_url')
->save();
}
/**
* Convert Imgix presets to image styles
*/
function imgix_update_8004(): string
{
/** @var ImgixImageStyleFactoryInterface $factory */
$factory = \Drupal::service('imgix.image_style_factory');
$config = \Drupal::configFactory()->getEditable('imgix.presets');
$count = 0;
// Install the new image module dependency if not yet installed
\Drupal::getContainer()
->get('module_installer')
->install(['image']);
foreach ($config->get('presets') ?? [] as $preset) {
if (ImageStyle::load($preset['key'])) {
continue;
}
parse_str($preset['query'], $parameters);
$imageStyle = $factory->getImageStyleByParameters($parameters);
$imageStyle->set('id', $preset['key']);
$imageStyle->set('name', $preset['key']);
$imageStyle->save();
$count++;
}
$config->delete();
return sprintf('Created %d image styles from Imgix presets.', $count);
}
/**
* Migrate fields from imgix to image field types
*/
function imgix_update_8005(): void
{
$database = \Drupal::database();
$fieldStorageStorage = \Drupal::entityTypeManager()
->getStorage('field_storage_config');
$fieldStorageConfigNames = $fieldStorageStorage->getQuery()
->condition('type', 'imgix')
->execute();
/** @var FieldStorageConfig $fieldStorage */
foreach ($fieldStorageStorage->loadMultiple($fieldStorageConfigNames) as $fieldStorage) {
$entityTypeId = $fieldStorage->getTargetEntityTypeId();
$fieldName = $fieldStorage->getName();
$newFieldType = 'image';
$table = $entityTypeId . '__' . $fieldName;
$currentRows = null;
$newFieldsList = [];
$fieldStorage = FieldStorageConfig::loadByName($entityTypeId, $fieldName);
if ($fieldStorage === null) {
return;
}
// Get all current data from DB.
if ($database->schema()->tableExists($table)) {
// The table data to restore after the update is completed.
$currentRows = $database->select($table, 'n')
->fields('n')
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
}
// Map description to alt field
foreach ($currentRows as &$row) {
$row[$fieldName . '_alt'] = $row[$fieldName . '_description'];
unset($row[$fieldName . '_description']);
}
// Use existing field config for new field.
foreach ($fieldStorage->getBundles() as $bundle => $label) {
$field = FieldConfig::loadByName($entityTypeId, $bundle, $fieldName);
$newField = $field->toArray();
$newField['field_type'] = $newFieldType;
$newFieldsList[] = $newField;
}
// Deleting field storage which will also delete bundles(fields).
$newFieldStorage = $fieldStorage->toArray();
$newFieldStorage['type'] = $newFieldType;
$fieldStorage->delete();
// Purge field data now to allow new field and field_storage with same name
// to be created.
field_purge_batch(40);
// Create new field storage.
$newFieldStorage = FieldStorageConfig::create($newFieldStorage);
$newFieldStorage->save();
// Create new fields.
foreach ($newFieldsList as $nfield) {
$nfieldConfig = FieldConfig::create($nfield);
$nfieldConfig->save();
}
// Restore existing data in new table.
if ($currentRows !== null) {
foreach ($currentRows as $row) {
$query = sprintf(
'INSERT IGNORE INTO {%s} (%s) VALUES (:values[])',
$table,
implode(', ', array_keys($row))
);
$database->query($query, [':values[]' => array_values($row)]);
}
}
}
}
/**
* Migrate field widgets from imgix to image
*/
function imgix_update_8006(): void
{
foreach (EntityFormDisplay::loadMultiple() as $formDisplay) {
$changed = false;
foreach ($formDisplay->getComponents() as $fieldName => $component) {
if (!isset($component['type']) || $component['type'] !== 'imgix') {
continue;
}
$component['type'] = 'image_image';
if (isset($component['preview_preset'])) {
$component['preview_image_style'] = $component['preview_preset'];
unset($component['preview_preset']);
}
$formDisplay->setComponent($fieldName, $component);
$changed = true;
}
if ($changed) {
$formDisplay->save();
}
}
}
/**
* Migrate field formatters from imgix to image
*/
function imgix_update_8007(): void
{
foreach (EntityViewDisplay::loadMultiple() as $viewDisplay) {
$changed = false;
foreach ($viewDisplay->getComponents() as $fieldName => $component) {
if (!isset($component['type']) || $component['type'] !== 'imgix_formatter') {
continue;
}
$component['type'] = 'image';
if (isset($component['image_preset'])) {
$component['image_style'] = $component['image_preset'];
unset($component['image_preset']);
}
$viewDisplay->setComponent($fieldName, $component);
$changed = true;
}
if ($changed) {
$viewDisplay->save();
}
}
}
/**
* Activate the imgix image toolkit
*/
function imgix_update_8008(): void
{
\Drupal::configFactory()
->getEditable('system.image')
->set('toolkit', 'imgix')
->save();
}
/**
* Remove the 's3_has_prefix' option
*/
function imgix_update_8009(): void
{
\Drupal::configFactory()
->getEditable('imgix.settings')
->clear('s3_has_prefix')
->save();
}