From 84fa48e208698920593ea1095b2acd79bf230bf4 Mon Sep 17 00:00:00 2001 From: Liam Morland <lkmorlan@493050.no-reply.drupal.org> Date: Wed, 6 Apr 2022 16:23:26 +0000 Subject: [PATCH] Issue #3047913: Convert 'default_entity_id' column into a string --- fillpdf.install | 57 ++++++++++++++++++++++++++++++++++++++ src/Entity/FillPdfForm.php | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/fillpdf.install b/fillpdf.install index 6b710d2..fe1b1e4 100644 --- a/fillpdf.install +++ b/fillpdf.install @@ -305,3 +305,60 @@ function fillpdf_update_9501() { // Commit transaction. unset($transaction); } + +/** + * Convert default_entity_id to string. + */ +function fillpdf_update_9502() { + // @see https://www.drupal.org/docs/drupal-apis/update-api/updating-entities-and-fields-in-drupal-8#s-updating-a-base-field-type + $database = \Drupal::database(); + $transaction = $database->startTransaction(); + + $entity_type_manager = \Drupal::entityTypeManager(); + $bundle_of = 'fillpdf_form'; + $field_name = 'default_entity_id'; + + $storage = $entity_type_manager->getStorage($bundle_of); + $bundle_definition = $entity_type_manager->getDefinition($bundle_of); + // Sometimes the primary key isn't 'id'. e.g. 'eid' or 'item_id'. + $id_key = $bundle_definition->getKey('id'); + // If there is no data table defined then use the base table. + $table_name = $storage->getDataTable() ?: $storage->getBaseTable(); + $definition_manager = \Drupal::entityDefinitionUpdateManager(); + + // Store the existing values. + $migrated_values = $database->select($table_name) + ->fields($table_name, [$id_key, $field_name]) + ->execute() + ->fetchAllKeyed(); + + // Clear out the values. + $database->update($table_name) + ->fields([$field_name => NULL]) + ->execute(); + + // Uninstall the field. + $field_storage_definition = $definition_manager->getFieldStorageDefinition($field_name, $bundle_of); + $definition_manager->uninstallFieldStorageDefinition($field_storage_definition); + + // Create a new field definition. + $new_field = BaseFieldDefinition::create('string') + ->setName($field_name) + ->setProvider($bundle_of) + ->setTargetBundle(NULL) + ->setTargetEntityTypeId($bundle_of); + + // Install the new definition. + $definition_manager->installFieldStorageDefinition($field_name, $bundle_of, $bundle_of, $new_field); + + // Restore the values. + foreach ($migrated_values as $id => $value) { + $database->update($table_name) + ->fields([$field_name => (string) $value]) + ->condition($id_key, $id) + ->execute(); + } + + // Commit transaction. + unset($transaction); +} diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php index b54da35..7b5acf4 100644 --- a/src/Entity/FillPdfForm.php +++ b/src/Entity/FillPdfForm.php @@ -110,7 +110,7 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { $fields['default_entity_type'] = BaseFieldDefinition::create('string'); // Form element is set up in FillPdfFormForm. - $fields['default_entity_id'] = BaseFieldDefinition::create('integer'); + $fields['default_entity_id'] = BaseFieldDefinition::create('string'); $fields['destination_path'] = BaseFieldDefinition::create('string') ->setLabel(t('Destination path')) -- GitLab