Skip to content

Commit 07ac975

Browse files
authored
Merge pull request #532 from UN-OCHA/develop
Deploy 27/09/2021
2 parents 5161ae4 + 83cca2b commit 07ac975

File tree

13 files changed

+168
-590
lines changed

13 files changed

+168
-590
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"drupal/linkit": "^6.0@beta",
5656
"drupal/maintenance200": "^1.0",
5757
"drupal/media_entity_file_replace": "^1.0",
58-
"drupal/migrate_upgrade": "^3",
5958
"drupal/node_view_permissions": "^1.4",
6059
"drupal/paragraphs": "^1.8",
6160
"drupal/paragraphs_edit": "^2.0@alpha",

composer.lock

+8-133
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
}

html/modules/custom/iasc_hero/src/Entity/Hero.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* revision_metadata_keys = {
5656
* "revision_user" = "revision_user",
5757
* "revision_created" = "revision_created",
58-
* "revision_log_message" = "revision_log",
58+
* "revision_log_message" = "revision_log_message",
5959
* },
6060
* links = {
6161
* "canonical" = "/admin/content/hero/{hero}",

html/modules/custom/iasc_hero/src/Form/HeroRevisionRevertForm.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
102102
$original_revision_timestamp = $this->revision->getRevisionCreationTime();
103103

104104
$this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
105-
$this->revision->revision_log = $this->t('Copy of the revision from %date.', [
105+
$this->revision->revision_log_message = $this->t('Copy of the revision from %date.', [
106106
'%date' => $this->dateFormatter->format($original_revision_timestamp),
107107
]);
108108
$this->revision->save();

html/modules/custom/iasc_migrate/iasc_migrate.info.yml

-5
This file was deleted.

0 commit comments

Comments
 (0)