|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * IASC Content helpers. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Rename revision metadata key. |
| 10 | + */ |
| 11 | +function iasc_hero_update_8106() { |
| 12 | + $entity_type_id = 'hero'; |
| 13 | + $current_field_name = 'revision_log'; |
| 14 | + $target_field_name = 'revision_log_message'; |
| 15 | + |
| 16 | + // Get the table name. |
| 17 | + $definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id); |
| 18 | + $table_name = $definition->getRevisionTable(); |
| 19 | + |
| 20 | + // Check if the change should be applied. |
| 21 | + $schema = \Drupal::database()->schema(); |
| 22 | + if (!$schema->fieldExists($table_name, $current_field_name)) { |
| 23 | + // We are good the column 'revision_log' does not exist. |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Rename the revision metadata key in the entity type. |
| 28 | + iasc_hero_rename_metadata_key($entity_type_id, $target_field_name); |
| 29 | + |
| 30 | + // Rename the field in the last installed field storage definitions. |
| 31 | + iasc_hero_rename_field_storage_definition($entity_type_id, $current_field_name, $target_field_name); |
| 32 | + |
| 33 | + // Rename the field in the entity storage schema. |
| 34 | + $field_schema = iasc_hero_rename_field_entity_storage_schema($entity_type_id, $current_field_name, $target_field_name, $table_name); |
| 35 | + |
| 36 | + // Update the column name in the database table. |
| 37 | + if (!empty($field_schema)) { |
| 38 | + $schema->changeField($table_name, $current_field_name, $target_field_name, $field_schema); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Rename the 'revision_log_message' revision metadata key in the entity type. |
| 44 | + * |
| 45 | + * @param string $entity_type_id |
| 46 | + * The entity type ID. |
| 47 | + * @param string $target_field_name |
| 48 | + * The target field name. |
| 49 | + */ |
| 50 | +function iasc_hero_rename_metadata_key($entity_type_id, $target_field_name) { |
| 51 | + // Change the Revision metadata key in the entity type. |
| 52 | + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); |
| 53 | + |
| 54 | + /** @var \Drupal\Core\Entity\ContentEntityType $entity_type */ |
| 55 | + $entity_type = $definition_update_manager->getEntityType($entity_type_id); |
| 56 | + |
| 57 | + $metadata_key = 'revision_log_message'; |
| 58 | + $metadata_key_value = $entity_type->getRevisionMetadataKey($metadata_key); |
| 59 | + if ($metadata_key_value == $target_field_name) { |
| 60 | + // The metadata key already has the target value, we are good. |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Set the metadata key value. |
| 65 | + $entity_type->setRevisionMetadataKey($metadata_key, $target_field_name); |
| 66 | + |
| 67 | + /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */ |
| 68 | + $schema_repository = \Drupal::service('entity.last_installed_schema.repository'); |
| 69 | + |
| 70 | + // Store the changes in the last installed definition. |
| 71 | + $schema_repository->setLastInstalledDefinition($entity_type); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Rename a given field in its last installed field storage definitions. |
| 76 | + * |
| 77 | + * @param string $entity_type_id |
| 78 | + * The entity type ID. |
| 79 | + * @param string $current_field_name |
| 80 | + * The current field name. |
| 81 | + * @param string $target_field_name |
| 82 | + * The target field name. |
| 83 | + */ |
| 84 | +function iasc_hero_rename_field_storage_definition($entity_type_id, $current_field_name, $target_field_name) { |
| 85 | + /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */ |
| 86 | + $schema_repository = \Drupal::service('entity.last_installed_schema.repository'); |
| 87 | + $field_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id); |
| 88 | + $field_definition_changed = FALSE; |
| 89 | + |
| 90 | + /** @var \Drupal\Core\Field\BaseFieldDefinition $target_field_definition */ |
| 91 | + $target_field_definition = NULL; |
| 92 | + if (isset($field_storage_definitions[$current_field_name])) { |
| 93 | + $target_field_definition = $field_storage_definitions[$current_field_name]; |
| 94 | + // Delete the old field definition. |
| 95 | + unset($field_storage_definitions[$current_field_name]); |
| 96 | + $field_definition_changed = TRUE; |
| 97 | + } |
| 98 | + |
| 99 | + if (!$target_field_definition) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // Rename the field in the field definition. |
| 104 | + $target_field_definition->setName($target_field_name); |
| 105 | + if (!isset($field_storage_definitions[$target_field_name])) { |
| 106 | + // Add the new field definition. |
| 107 | + $field_storage_definitions[$target_field_name] = $target_field_definition; |
| 108 | + $field_definition_changed = TRUE; |
| 109 | + } |
| 110 | + if ($field_definition_changed) { |
| 111 | + // Stores the changes. |
| 112 | + $schema_repository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Rename a given field entity storage schema. |
| 118 | + * |
| 119 | + * @param string $entity_type_id |
| 120 | + * The entity type ID. |
| 121 | + * @param string $current_field_name |
| 122 | + * The current field name. |
| 123 | + * @param string $target_field_name |
| 124 | + * The target field name. |
| 125 | + * @param string $table_name |
| 126 | + * The table name. |
| 127 | + * |
| 128 | + * @return array|null |
| 129 | + * The field schema. |
| 130 | + */ |
| 131 | +function iasc_hero_rename_field_entity_storage_schema($entity_type_id, $current_field_name, $target_field_name, $table_name) { |
| 132 | + $key_value = \Drupal::keyValue('entity.storage_schema.sql'); |
| 133 | + $key_name = $entity_type_id . '.field_schema_data.' . $current_field_name; |
| 134 | + $table_storage_schema = $key_value->get($key_name); |
| 135 | + $field_schema = $table_storage_schema[$table_name]['fields'][$current_field_name] ?? NULL; |
| 136 | + |
| 137 | + if (empty($field_schema)) { |
| 138 | + // The 'revision_log' schema does not exist, therefore stop here. |
| 139 | + return NULL; |
| 140 | + } |
| 141 | + |
| 142 | + // Delete the current field schema. |
| 143 | + unset($table_storage_schema[$table_name]['fields'][$current_field_name]); |
| 144 | + |
| 145 | + // Add the target field schema. |
| 146 | + $table_storage_schema[$table_name]['fields'][$target_field_name] = $field_schema; |
| 147 | + |
| 148 | + // Stores the changes. |
| 149 | + $key_value->delete($key_name); |
| 150 | + $new_key_name = $entity_type_id . '.field_schema_data.' . $target_field_name; |
| 151 | + $key_value->set($new_key_name, $table_storage_schema); |
| 152 | + |
| 153 | + // Return the field schema. |
| 154 | + return $field_schema; |
| 155 | +} |
0 commit comments