-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoe_link_lists.post_update.php
235 lines (206 loc) · 7.43 KB
/
oe_link_lists.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
<?php
/**
* @file
* Post update functions for OE Link Lists.
*/
declare(strict_types=1);
use Drupal\oe_link_lists\Entity\LinkList;
use Drupal\oe_link_lists\Entity\LinkListType;
/**
* Update all the bundles to use the link source plugin selection.
*/
function oe_link_lists_post_update_00001() {
$link_list_types = LinkListType::loadMultiple();
foreach ($link_list_types as $id => $link_list_type) {
if ($id === 'manual') {
// The manual one is handled in its own submodule.
continue;
}
$link_list_type->set('configurable_link_source_plugins', TRUE);
$link_list_type->save();
}
}
/**
* Update all link lists to set the default no_results_behaviour plugin.
*/
function oe_link_lists_post_update_00002(&$sandbox) {
if (!isset($sandbox['total'])) {
// Get all the link lists.
$ids = \Drupal::entityTypeManager()
->getStorage('link_list')
->getQuery()
->accessCheck(FALSE)
->execute();
if (!$ids) {
return t('No link lists need to be updated.');
}
$sandbox['ids'] = $ids;
$sandbox['total'] = count($sandbox['ids']);
$sandbox['current'] = 0;
$sandbox['items_per_batch'] = 10;
}
$ids = array_slice($sandbox['ids'], $sandbox['current'], $sandbox['items_per_batch']);
/** @var \Drupal\oe_link_lists\Entity\LinkListInterface[] $link_lists */
$link_lists = LinkList::loadMultiple($ids);
foreach ($link_lists as $link_list) {
$configuration = $link_list->getConfiguration();
if (isset($configuration['no_results_behaviour'])) {
// This should not happen but in case something went wrong.
$sandbox['current']++;
continue;
}
$configuration['no_results_behaviour'] = [
'plugin' => 'hide_list',
'plugin_configuration' => [],
];
$link_list->setConfiguration($configuration);
$link_list->save();
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : ($sandbox['current'] / $sandbox['total']);
if ($sandbox['#finished'] === 1) {
return t('A total of @updated link lists have been updated.', ['@updated' => $sandbox['current']]);
}
}
/**
* Update all link lists to set the default more_link plugin.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
function oe_link_lists_post_update_00003(&$sandbox) {
if (!isset($sandbox['total'])) {
// Get all the link lists.
$ids = \Drupal::entityTypeManager()
->getStorage('link_list')
->getQuery()
->accessCheck(FALSE)
->execute();
if (!$ids) {
return t('No link lists need to be updated.');
}
$sandbox['ids'] = $ids;
$sandbox['total'] = count($sandbox['ids']);
$sandbox['current'] = 0;
$sandbox['updated'] = 0;
$sandbox['items_per_batch'] = 10;
foreach (\Drupal::languageManager()->getLanguages() as $language) {
$sandbox['languages'][] = $language->getId();
}
}
$ids = array_slice($sandbox['ids'], $sandbox['current'], $sandbox['items_per_batch']);
/** @var \Drupal\oe_link_lists\Entity\LinkListInterface[] $link_lists */
$link_lists = LinkList::loadMultiple($ids);
foreach ($link_lists as $link_list) {
$needs_save = FALSE;
foreach ($sandbox['languages'] as $language) {
$translation = $link_list->hasTranslation($language) ? $link_list->getTranslation($language) : NULL;
if (!$translation) {
continue;
}
// Determine if we are updating the original config or a translation
// because we need to know later how to handle the config array.
$original = $translation->language()->getId() === $link_list->getUntranslated()->language()->getId();
// We cannot use the `getConfiguration()` API method because that will
// attempt merging of array keys so if one of the translations loses some
// translatable keys, the API won't be able to merge them anymore. So we
// need to work using the raw configuration array stored in the entity
// and ensure that we are updating accordingly below.
$update = _oe_link_lists_update_configuration_with_default_more_link($translation->get('configuration')->get(0)->getValue(), $original);
if ($update['needs save']) {
$needs_save = TRUE;
$translation->set('configuration', $update['configuration']);
}
}
if ($needs_save) {
$sandbox['updated']++;
$link_list->save();
}
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : ($sandbox['current'] / $sandbox['total']);
if ($sandbox['#finished'] === 1) {
return t('A total of @updated link lists out of @total have been updated.', [
'@updated' => $sandbox['updated'],
'@total' => $sandbox['total'],
]);
}
}
/**
* Updates a link list configuration with the "more_link" plugin configuration.
*
* This method receives the raw config values of either the original language
* or of the translation (which has less keys). So we need to ensure we are
* updating the translation keys accordingly and not set values which don't
* belong in there.
*
* @param array $configuration
* The configuration array.
* @param bool $original_language
* Whether it's the config of the original language.
*
* @return array
* An array with the config and whether a save is required.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
function _oe_link_lists_update_configuration_with_default_more_link(array $configuration, bool $original_language): array {
$update = [
'configuration' => $configuration,
'needs save' => FALSE,
];
// If it's already set on it for any reason, we don't do anything.
if (isset($configuration['more_link'])) {
return $update;
}
if (empty($configuration)) {
// It means we are probably on a translation and there is no configuration
// translated.
return $update;
}
$more = $configuration['more'] ?? [];
if ((!$more || !isset($more['button'])) && $original_language) {
// If we are on the original language and we don't have a "more" button
// configured, we just default to the empty "more_link" array. This does
// not happen for the translations.
$configuration['more_link'] = [];
$update['needs save'] = TRUE;
$update['configuration'] = $configuration;
return $update;
}
if (isset($more['button']) && $more['button'] === 'no') {
// It means there is no "more" link configured. It also means we are on
// the original because the "button" key never ends up in the translation
// array. And we just default to the empty "more_link" array.
$configuration['more_link'] = [];
unset($configuration['more']);
$update['needs save'] = TRUE;
$update['configuration'] = $configuration;
return $update;
}
if ($original_language) {
$configuration['more_link'] = [
'plugin' => 'custom_link',
'plugin_configuration' => [
'target' => $more['target'],
'title_override' => $more['title_override'],
],
];
}
else {
$configuration['more_link'] = [
'plugin_configuration' => [],
];
if (isset($more['target'])) {
$configuration['more_link']['plugin_configuration']['target'] = $more['target'];
}
if (isset($more['title_override'])) {
$configuration['more_link']['plugin_configuration']['title_override'] = $more['title_override'];
}
}
unset($configuration['more']);
$update['configuration'] = $configuration;
$update['needs save'] = TRUE;
return $update;
}