From 05178a0801b0fd18d2b21ce421910d3fa6c7a73d Mon Sep 17 00:00:00 2001 From: jungle <qiangjun@2919723.no-reply.drupal.org> Date: Thu, 21 Apr 2022 17:50:37 +0000 Subject: [PATCH] Issue #3276361: Increase maximum length for pdf_key if it has values before --- fillpdf.install | 82 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/fillpdf.install b/fillpdf.install index fe1b1e4..749b8f8 100644 --- a/fillpdf.install +++ b/fillpdf.install @@ -283,27 +283,10 @@ function fillpdf_update_8111() { * Increase maximum length for pdf_key. */ function fillpdf_update_9501() { - $db = \Drupal::database(); - - $transaction = $db->startTransaction(); - - // Update field storage definition. - $edum = \Drupal::entityDefinitionUpdateManager(); - $field_storage_definition = $edum->getFieldStorageDefinition('pdf_key', 'fillpdf_form_field'); - $field_storage_definition->setSetting('type', 'string_long'); - $edum->updateFieldStorageDefinition($field_storage_definition); - - // Update database schema. - $spec = [ - 'type' => 'text', - 'size' => 'big', - ]; - $db - ->schema() - ->changeField('fillpdf_fields', 'pdf_key', 'pdf_key', $spec); - - // Commit transaction. - unset($transaction); + // The code of this hook_update_N() implementation is removed in favor of + // fillpdf_update_9503() as it does not cover the case that pdf_key has + // values. + // @see https://www.drupal.org/project/fillpdf/issues/3276361 } /** @@ -362,3 +345,60 @@ function fillpdf_update_9502() { // Commit transaction. unset($transaction); } + +/** + * Increase maximum length for pdf_key. + */ +function fillpdf_update_9503() { + $field_name = 'pdf_key'; + $entity_type = 'fillpdf_form_field'; + $new_field_type = 'string_long'; + + $database = \Drupal::database(); + $transaction = $database->startTransaction(); + + $entity_type_manager = \Drupal::entityTypeManager(); + $storage = $entity_type_manager->getStorage($entity_type); + $entity_definition = $entity_type_manager->getDefinition($entity_type); + $id_key = $entity_definition->getKey('id'); + $table_name = $storage->getBaseTable(); + $definition_manager = \Drupal::entityDefinitionUpdateManager(); + + $field_storage_definition = $definition_manager->getFieldStorageDefinition($field_name, $entity_type); + // If the field type is string_long already, do nothing. + if ($field_storage_definition->getType() === $new_field_type) { + return; + } + + // 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. + $definition_manager->uninstallFieldStorageDefinition($field_storage_definition); + + // Create the new field definition. + $new_field = BaseFieldDefinition::create($new_field_type) + ->setLabel(t('PDF Key')) + ->setDescription(t('The name of the field in the PDF form.')); + // Install the new definition. + $definition_manager->installFieldStorageDefinition($field_name, $entity_type, $entity_type, $new_field); + + // Restore the values. + foreach ($migrated_values as $id => $value) { + $database->update($table_name) + ->fields([$field_name => $value]) + ->condition($id_key, $id) + ->execute(); + } + + // Commit transaction. + unset($transaction); +} -- GitLab