-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoe_paragraphs.install
executable file
·256 lines (219 loc) · 9.2 KB
/
oe_paragraphs.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
<?php
/**
* @file
* Install, update and uninstall functions for the OE Paragraphs module.
*/
declare(strict_types=1);
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Copy the values to the new variant field and delete the old variant fields.
*/
function oe_paragraphs_update_8001(array &$sandbox): void {
if (!isset($sandbox['total'])) {
// Initialise batch variables and settings.
$sandbox['variant_fields'] = [
'oe_content_row' => 'field_oe_content_row_variant',
'oe_list_item_block' => 'field_oe_list_item_block_variant',
'oe_list_item' => 'field_oe_list_item_variant',
];
$sandbox['paragraph_ids'] = [];
foreach ($sandbox['variant_fields'] as $bundle => $field) {
// Make sure we have the old fields in the database.
$all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('paragraph', $bundle);
if (!isset($all_bundle_fields[$field])) {
continue;
}
// Take all entities that has a value in one of the old fields.
$query = \Drupal::entityQuery('paragraph');
$query->exists($field);
$result = $query->execute();
if (!empty($result)) {
$sandbox['paragraph_ids'] += $result;
}
}
$sandbox['total'] = count($sandbox['paragraph_ids']);
$sandbox['current'] = 0;
$sandbox['paragraphs_per_batch'] = 25;
// Make sure we have the new base field created before we copy values.
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Variant'))
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('oe_paragraphs_variant', 'paragraph', 'oe_paragraphs', $storage_definition);
// Start the sync for new configurations.
$storage = new FileStorage(\Drupal::service('extension.list.module')->getPath('oe_paragraphs') . '/config/updates/8001');
// Change field descriptions.
$fields_description = [
'paragraph.oe_content_row.field_oe_title' => 'The title to show for the inpage navigation.',
'paragraph.oe_list_item.field_oe_text_long' => 'List item description, displayed below the title.',
'paragraph.oe_list_item_block.field_oe_link' => 'Internal or external link to use for a call to action button.',
'paragraph.oe_list_item_block.field_oe_title' => 'Block title. Optional.',
'paragraph.oe_list_item.field_oe_image' => 'List item image.',
'paragraph.oe_list_item.field_oe_date' => 'Date information.',
'paragraph.oe_list_item.field_oe_title' => 'The title to show for the inpage navigation.',
'paragraph.oe_list_item.field_oe_link' => 'Internal or external link to use for a call to action button.',
];
foreach ($fields_description as $id => $description) {
$field = FieldConfig::load($id);
$field->setDescription($description);
$field->save();
}
// Create the new field from config.
$new_field_config = [
'field.storage.paragraph.field_oe_list_item_block_layout',
'field.field.paragraph.oe_list_item_block.field_oe_list_item_block_layout',
];
// Clear the field widget cache.
\Drupal::service('plugin.manager.field.widget')->clearCachedDefinitions();
$config_manager = \Drupal::service('config.manager');
$entity_manager = \Drupal::entityTypeManager();
foreach ($new_field_config as $config_name) {
$config_record = $storage->read($config_name);
$entity_type = $config_manager->getEntityTypeIdByName($config_name);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
$entity_storage = $entity_manager->getStorage($entity_type);
$entity = $entity_storage->createFromStorageRecord($config_record);
$entity->save();
}
// New form modes.
$form_modes = $storage->listAll('core.entity_form_mode.');
foreach ($form_modes as $mode) {
$values = $storage->read($mode);
$form_mode = EntityFormMode::create($values);
$form_mode->save();
}
// Update form displays.
$form_displays = $storage->listAll('core.entity_form_display.');
foreach ($form_displays as $display) {
$values = $storage->read($display);
$form_display = EntityFormDisplay::load($values['id']);
if ($form_display) {
foreach ($values as $key => $value) {
$form_display->set($key, $value);
}
}
else {
$form_display = EntityFormDisplay::create($values);
}
$form_display->save();
}
// Update view displays.
$view_displays = $storage->listAll('core.entity_view_display.');
foreach ($view_displays as $display) {
$values = $storage->read($display);
$view_display = EntityViewDisplay::load($values['id']);
foreach ($values as $key => $value) {
$view_display->set($key, $value);
}
$view_display->save();
}
}
// Get the current slice of paragraph ids.
$pids = array_slice($sandbox['paragraph_ids'], $sandbox['current'], $sandbox['paragraphs_per_batch']);
$entity_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
foreach ($pids as $pid) {
$paragraph = $entity_storage->load($pid);
$bundle = $paragraph->bundle();
$value = $paragraph->get($sandbox['variant_fields'][$bundle])->value;
// Check for values that needs conversion.
if (strpos($value, 'list_item_') !== FALSE) {
$value = substr($value, strlen('list_item_'));
}
// Normalize inpage value to the new display name.
if ($value === 'inpage') {
$value = 'inpage_navigation';
}
// Take out list block item values as they are not variants but layouts.
if ($sandbox['variant_fields'][$bundle] === 'field_oe_list_item_block_variant') {
// Set the layout filed with the value.
$paragraph->set('field_oe_list_item_block_layout', $value);
}
else {
// Set the new field with the old value.
$paragraph->set('oe_paragraphs_variant', $value);
}
// Set the old field to NULL.
$paragraph->set($sandbox['variant_fields'][$bundle], NULL);
$paragraph->save();
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : ($sandbox['current'] / $sandbox['total']);
if ($sandbox['#finished'] === 1) {
// Cleanup the fields after copy.
foreach ($sandbox['variant_fields'] as $bundle => $field) {
FieldStorageConfig::loadByName('paragraph', $field)->delete();
}
}
}
/**
* Update the content translation settings of the existing paragraphs types.
*/
function oe_paragraphs_update_8002(&$sandbox) {
// We need to update all paragraph types to hide the non-translatable fields
// on translation forms because otherwise the paragraphs don't work with
// content moderation.
// @see https://www.drupal.org/docs/8/modules/paragraphs/multilingual-and-content-moderation
$ids = [
'paragraph.oe_accordion',
'paragraph.oe_accordion_item',
'paragraph.oe_content_row',
'paragraph.oe_links_block',
'paragraph.oe_list_item',
'paragraph.oe_list_item_block',
'paragraph.oe_quote',
'paragraph.oe_rich_text',
];
/** @var \Drupal\language\ContentLanguageSettingsInterface[] $content_settings */
$content_settings = \Drupal::entityTypeManager()->getStorage('language_content_settings')->loadMultiple($ids);
foreach ($content_settings as $settings) {
$bundle_settings = $settings->getThirdPartySetting('content_translation', 'bundle_settings');
if (!$bundle_settings) {
continue;
}
if ($bundle_settings['untranslatable_fields_hide'] !== '1') {
$bundle_settings['untranslatable_fields_hide'] = '1';
$settings->setThirdPartySetting('content_translation', 'bundle_settings', $bundle_settings);
$settings->save();
}
}
}
/**
* Update list of paragraphs that can be referenced inside content rows.
*/
function oe_paragraphs_update_8003(&$sandbox) {
$storage = new FileStorage(\Drupal::service('extension.list.module')->getPath('oe_paragraphs') . '/config/updates/8003');
$values = $storage->read('field.field.paragraph.oe_content_row.field_oe_paragraphs');
$field = FieldConfig::load($values['id']);
if (!$field) {
return t('Could not load default field for content row paragraph.');
}
foreach ($values as $key => $value) {
$field->set($key, $value);
}
$field->save();
}
/**
* Updates the form display for the accordion paragraph.
*/
function oe_paragraphs_update_8004(&$sandbox) {
$storage = new FileStorage(\Drupal::service('extension.list.module')->getPath('oe_paragraphs') . '/config/updates/8004');
$values = $storage->read('core.entity_form_display.paragraph.oe_accordion.default');
$form_display = EntityFormDisplay::load($values['id']);
if (!$form_display) {
return t('Could not load default form display for accordion paragraph.');
}
foreach ($values as $key => $value) {
$form_display->set($key, $value);
}
$form_display->save();
}