-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathewp_core.post_update.php
More file actions
51 lines (40 loc) · 1.59 KB
/
ewp_core.post_update.php
File metadata and controls
51 lines (40 loc) · 1.59 KB
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
<?php
/**
* Replaces empty strings with NULL in the 'lang' property of language typed fields.
*/
function ewp_core_post_update_nullify_empty_lang(&$sandbox) {
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_type_manager = \Drupal::entityTypeManager();
$database = \Drupal::database();
$field_types = [
'ewp_http_lang',
'ewp_multiline_lang',
'ewp_string_lang',
];
foreach ($field_types as $field_type) {
$field_map = $entity_field_manager->getFieldMapByFieldType($field_type);
foreach ($field_map as $entity_type_id => $fields) {
$storage = $entity_type_manager->getStorage($entity_type_id);
if (!$storage instanceof \Drupal\Core\Entity\Sql\SqlEntityStorageInterface) {
continue;
}
$table_mapping = $storage->getTableMapping();
$storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
foreach (array_keys($fields) as $field_name) {
if (!isset($storage_definitions[$field_name])) {
continue;
}
$storage_definition = $storage_definitions[$field_name];
$column = $table_mapping->getFieldColumnName($storage_definition, 'lang');
$tables = $table_mapping->getAllFieldTableNames($field_name);
foreach ($tables as $table) {
$database->update($table)
->expression($column, "NULLIF($column, '')")
->condition($column, '', '=')
->execute();
}
}
}
}
return 'Database columns for the "lang" property have been updated to NULL where empty.';
}