-
Notifications
You must be signed in to change notification settings - Fork 10
/
format_strawberryfield.install
364 lines (348 loc) · 14.7 KB
/
format_strawberryfield.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Database\Database;
/**
* Install Entity and add 'mime type' field to 'metadata display' entities.
*/
/**
* Implements hook_install().
*/
function format_strawberryfield_install() {
}
function format_strawberryfield_update_8001() {
$schema = Database::getConnection()->schema();
// Always start by testing if the entity is already deployed.
if (!$schema->tableExists('metadatadisplay_entity')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->installEntityType(\Drupal::entityTypeManager()->getDefinition('metadatadisplay_entity'));
}
// Add mimetype field definition to \Drupal\format_strawberryfield\Entity\MetadataDisplayEntity
//@see https://www.drupal.org/node/2554097
$storage_definition = BaseFieldDefinition::create('list_string')
->setLabel(t('Primary mime type this Twig Template entity will generate as output.'))
->setDescription(t('When downloading the output, this will define the extension, validation and format. Every Mime type supports also being rendered as HTML'))
->setSettings(array(
'default_value' => 'text/html',
'max_length' => 64,
'cardinality' => 1,
'allowed_values' => [
'text/html' => 'HTML',
'application/json' => 'JSON',
'application/ld+json' => 'JSON-LD',
'application/xml' => 'XML',
'text/text' => 'TEXT',
'text/turtle' => 'RDF/TURTLE',
'text/csv' => 'CSV',
],
))
->setRequired(TRUE)
->setDisplayOptions('view', [
'region' => 'hidden',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE)
->addConstraint('NotBlank');
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('mimetype', 'metadatadisplay_entity', 'format_strawberryfield', $storage_definition);
}
/**
* Implements hook_update_N().
*
* Installs metadataexpose_entity config entity for exposing twig output via routes.
*
* @see 'https://www.drupal.org/node/3034742'
*/
function format_strawberryfield_update_8701() {
\Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([
'id' => 'metadataexpose_entity',
'label' => new TranslatableMarkup('Exposed Metadata using Twig Configuration'),
'config_prefix' => 'metadataexpose_entity',
'admin_permission' => 'administer site configuration',
'entity_keys' => ['id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
'active' => 'active',
],
'config_export' => [
'id',
'label',
'uuid',
'target_entity_types',
'source_entityfield_name',
'processor_entity_id',
'cache',
'active'
],
]));
}
/**
* Implements hook_update_N().
*
* Updates Formatters, Exposed metadata and Views to load metadatadisplay entities via UUID.
*
*/
function format_strawberryfield_update_8702() {
$config_factory = \Drupal::configFactory();
// Find all views configs.
foreach ($config_factory->listAll('views.view.') as $view_config_name) {
$needs_saving = FALSE;
$view = $config_factory->getEditable($view_config_name);
// Go through each display on each view.
$displays = $view->get('display');
foreach ($displays as $display_name => &$display) {
if (isset($display['display_options']['fields'])) {
foreach ($display['display_options']['fields'] as &$field) {
$metadatadisplayentity_id = $field['settings']['metadatadisplayentity_id'] ?? NULL;
$metadatadisplayentity_id = is_scalar($metadatadisplayentity_id) ? $metadatadisplayentity_id : NULL;
if ($metadatadisplayentity_id) {
$metadata_display_entitiy = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_id);
if ($metadata_display_entitiy) {
$uuid = $metadata_display_entitiy->uuid();
$field['settings']['metadatadisplayentity_uuid'] = $uuid;
unset($field['settings']['metadatadisplayentity_id']);
$needs_saving = TRUE;
}
}
}
}
}
if ($needs_saving) {
$view->set('display', $displays);
$view->save(TRUE);
}
}
// Entity view display configs for nodes.
foreach ($config_factory->listAll('core.entity_view_display.node.') as $entity_view_display_config) {
$entity_view = $config_factory->getEditable($entity_view_display_config);
// We need to load the ones that contain a metadata display id as number
// $data['content']['fieldname'][type] = strawberry_metadata_formatter
$data = $entity_view->getRawData();
$needs_saving = FALSE;
if (isset($data['third_party_settings']['ds']['fields'])) {
foreach ($data['third_party_settings']['ds']['fields'] as &$field) {
$metadatadisplayentity_id = $field['settings']['formatter']['metadatadisplayentity_id'] ?? NULL;
$metadatadisplayentity_id = is_scalar($metadatadisplayentity_id) ? $metadatadisplayentity_id : NULL;
if ($metadatadisplayentity_id) {
$metadata_display_entitiy = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_id);
if ($metadata_display_entitiy) {
$uuid = $metadata_display_entitiy->uuid();
$field['settings']['formatter']['metadatadisplayentity_uuid'] = $uuid;
unset($field['settings']['formatter']['metadatadisplayentity_id']);
$needs_saving = TRUE;
}
}
else {
$metadatadisplayentity_source = $field['settings']['formatter']['metadatadisplayentity_source'] ?? NULL;
$metadatadisplayentity_source = is_scalar($metadatadisplayentity_source) ? $metadatadisplayentity_source : NULL;
if ($metadatadisplayentity_source) {
$metadata_display_entitiy = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_source);
if ($metadata_display_entitiy) {
$uuid = $metadata_display_entitiy->uuid();
$field['settings']['formatter']['metadatadisplayentity_uuid'] = $uuid;
unset($field['settings']['formatter']['metadatadisplayentity_source']);
$needs_saving = TRUE;
}
}
}
}
}
if (isset($data['content'])) {
foreach ($data['content'] as &$realfield) {
$metadatadisplayentity_id = $realfield['settings']['metadatadisplayentity_id'] ?? NULL;
$metadatadisplayentity_id = is_scalar($metadatadisplayentity_id) ? $metadatadisplayentity_id : NULL;
if ($metadatadisplayentity_id) {
$metadata_display_entitiy = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_id);
if ($metadata_display_entitiy) {
$uuid = $metadata_display_entitiy->uuid();
$realfield['settings']['metadatadisplayentity_uuid'] = $uuid;
unset($realfield['settings']['metadatadisplayentity_id']);
$needs_saving = TRUE;
}
}
else {
$metadatadisplayentity_source = $field['settings']['formatter']['metadatadisplayentity_source'] ?? NULL;
$metadatadisplayentity_source = is_scalar($metadatadisplayentity_source) ? $metadatadisplayentity_source : NULL;
if ($metadatadisplayentity_source) {
$metadata_display_entitiy = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_source);
if ($metadata_display_entitiy) {
$uuid = $metadata_display_entitiy->uuid();
$field['settings']['formatter']['metadatadisplayentity_uuid'] = $uuid;
unset($field['settings']['formatter']['metadatadisplayentity_source']);
$needs_saving = TRUE;
}
}
}
}
}
if ($needs_saving) {
$entity_view->setData($data);
$entity_view->save(TRUE);
}
}
// Now exposed metadata endpoints.
$metadataexpose_entities = \Drupal::entityTypeManager()->getStorage('metadataexpose_entity')->loadMultiple();
foreach ($metadataexpose_entities as $metadataexpose_entity) {
$metadatadisplayentity_id = $metadataexpose_entity->processor_entity_id ?? NULL;
if ($metadatadisplayentity_id) {
$metadatadisplayentity = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->load($metadatadisplayentity_id);
if ($metadatadisplayentity) {
$uuid = $metadatadisplayentity->uuid();
$metadataexpose_entity->setMetadatadisplayentityUuid($uuid);
$metadataexpose_entity->set('metadatadisplayentity_uuid', $uuid);
$metadataexpose_entity->set('processor_entity_id', NULL);
// To be deprecated afterwards.
$metadataexpose_entity->processor_entity_id = NULL;
$metadataexpose_entity->save(TRUE);
}
}
}
$message = "All Metadata Display Entities in Configurations updated to use UUIDs";
return $message;
}
/**
* Implements hook_update_N().
*
* Updates metadataexpose_entity config adding hide_on_embargo config option.
*
*/
function format_strawberryfield_update_8703() {
$message = "Nothing to update. metadataexpose_entity entity is not installed.";
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('metadataexpose_entity');
if ($entity_type) {
$entity_config_export = $entity_type->get('config_export');
$entity_config_export[] = 'hide_on_embargo';
$entity_type->set('config_export', $entity_config_export);
\Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
$message = "Metadata Exposed Display Entity's (metadataexpose_entity) Schema updated with hide_on_embargo setting";
}
return $message;
}
/**
* Implements hook_update_N().
*
* Attemps to fix Twig syntax deprecations from Twig 2 to Twig 3.
*
*/
function format_strawberryfield_update_8704() {
$separator = "\r\n";
$ifinsidefor_regex = '/{%([\s|\-|~]*for.+?(?=\s{1}if))(.*?)%}/m';
$any_for_regex = '/{%([\s|\-|~]*for.+)%}/m';
// Not used but will keep here bc so hard to build/write!
// $not_ifinsidefor_regex = '/^(?!.*if).*{%[\s|~|\-]*for.*/m';
$endfor_regex = '/({%[\s|\-|~]*(endfor)[\s|\-|~]*?%}){1}/m';
$spaceless_regex = '/({%[\s|\-|~]*(spaceless|endspaceless)[\s|\-|~]*%})/m';
$metadatadisplay_entity_storage = \Drupal::entityTypeManager()->getStorage('metadatadisplay_entity');
$entity_query = \Drupal::entityQuery('metadatadisplay_entity');
$entity_query->sort('id', 'DESC');
$entity_query->accessCheck(FALSE);
$ids = $entity_query->execute();
$fixed_labels = [];
$failed_labels = [];
foreach ($ids as $id) {
$metadatadisplay_entity_storage->resetCache([$id]);
/* @var $metadatadisplay_entity \Drupal\format_strawberryfield\Entity\MetadataDisplayEntity|null */
$metadatadisplay_entity = $metadatadisplay_entity_storage->load($id);
if (!empty($metadatadisplay_entity)) {
// Count for nested fors
$for_level = 0;
$ifinsideforfound_level = [];
$twigtemplate = $metadatadisplay_entity->get('twig')->getValue();
$twigtemplate = !empty($twigtemplate) ? $twigtemplate[0]['value'] : "{{ 'empty' }}";
strtok('', '');
$fixed = FALSE;
$line = strtok($twigtemplate, $separator);
// after finding a for if, keep counting other fors .. because we could end closing the new if at the first encounter of an for which is wrong
$newtemplate = '';
while ($line !== false) {
// Check for any for so we can keep track of the for deepness. More reliable.
$matches_any_for = [];
preg_match_all($any_for_regex, $line, $matches_any_for, PREG_SET_ORDER, 0);
if (!empty($matches_any_for)) {
$for_level++;
}
$matches_any_endfor = [];
preg_match_all($endfor_regex, $line, $matches_any_endfor, PREG_SET_ORDER, 0);
if (!empty($matches_any_endfor)) {
if (isset($ifinsideforfound_level[$for_level])) {
$line = preg_replace($endfor_regex, '{#- WIZARDLY UPDATED BY ARCHIPELAGO -#}'.$separator.'{% endif %}'.$separator.'${1}', $line);
unset($ifinsideforfound_level[$for_level]);
$fixed = TRUE;
}
$for_level--;
}
// Checks an FOR with an IF
$matches = [];
preg_match_all($ifinsidefor_regex, $line, $matches, PREG_SET_ORDER, 0);
if (!empty($matches)) {
$line = preg_replace($ifinsidefor_regex, '{%${1}%}'.$separator.'{#- WIZARDLY UPDATED BY ARCHIPELAGO -#}'.$separator.'{% ${2}%}', $line);
$ifinsideforfound_level[$for_level] = TRUE;
}
// Checks Spacesless/Endspaceless
$matches3 = [];
preg_match_all($spaceless_regex, $line, $matches3, PREG_SET_ORDER, 0);
if (!empty($matches3)) {
$line = preg_replace($spaceless_regex,'', $line);
$fixed = TRUE;
}
// Appends line
if (!empty($line)) {
$newtemplate = $newtemplate . rtrim($line) .$separator;
}
// Tokenize a new line
$line = strtok( $separator );
}
if ($fixed) {
$metadatadisplay_entity->invalidateTempMetadataDisplayCaches();
$validate = $metadatadisplay_entity->validateSource($newtemplate, TRUE);
if ($validate === TRUE) {
$metadatadisplay_entity->set('twig',$newtemplate);
$metadatadisplay_entity->save();
$fixed_labels[] = $metadatadisplay_entity->label();
}
else {
$failed_labels[] = $metadatadisplay_entity->label();
}
}
}
}
strtok('', '');
$message = t('All done, welcome to Twig 3.0! Templates that needed fix/were fixed: @fixed ; Templates we attempted a fix but we failed and left untouched for you to correct: @failed',
[
'@fixed' => count($fixed_labels) ? implode("; ", $fixed_labels) : 'none',
'@failed' => count($failed_labels) ? implode("; ", $failed_labels) : 'none',
]);
return $message;
}
/**
* Implements hook_update_N().
*
* Installs metadataapi_entity config entity for dynamic APIs.
*
*/
function format_strawberryfield_update_9001() {
$schema = Database::getConnection()->schema();
if (!$schema->tableExists('metadataapi_entity')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->installEntityType(
\Drupal::entityTypeManager()->getDefinition('metadataapi_entity')
);
}
else {
return 'Metadata API Configuration Entity already exists';
}
}