This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_version.post_update.php
57 lines (49 loc) · 2.05 KB
/
entity_version.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
<?php
/**
* @file
* Entity version post updates.
*/
declare(strict_types = 1);
/**
* Migrate to configuration-based version field settings.
*/
function entity_version_post_update_configure_settings() {
// Get entity types and bundles where the entity_version field is present.
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = \Drupal::service('entity_field.manager');
$versioned_entity_types = $entity_field_manager->getFieldMapByFieldType('entity_version');
$version_settings_storage = \Drupal::entityTypeManager()->getStorage('entity_version_settings');
$entity_bundles_with_field = [];
// Collect entity types and bundles that have entity version field.
foreach ($versioned_entity_types as $entity_type_id => $fields) {
$entity_bundles_with_field[$entity_type_id] = [];
foreach ($fields as $field_name => $bundle_info) {
if (!empty($bundle_info['bundles'])) {
$entity_bundles_with_field[$entity_type_id] = array_merge($entity_bundles_with_field[$entity_type_id], $bundle_info['bundles']);
}
}
}
// Loop through the collected entity types and bundles and load the field
// definitions. We need to ensure that we retrieve the version field the
// same way as we did before the config entity was in place.
foreach ($entity_bundles_with_field as $entity_type_id => $bundles) {
foreach ($bundles as $bundle) {
if ($version_settings_storage->load("$entity_type_id.$bundle")) {
continue;
}
$field_definitions = $entity_field_manager->getFieldDefinitions($entity_type_id, $bundle);
/** @var \Drupal\field\FieldConfigInterface $field */
foreach ($field_definitions as $field) {
if ($field->getType() === 'entity_version') {
// Create the entity version setting.
$version_settings_storage->create([
'target_entity_type_id' => $entity_type_id,
'target_bundle' => $bundle,
'target_field' => $field->getName(),
])->save();
break;
}
}
}
}
}