Skip to content
Snippets Groups Projects
Commit 05178a08 authored by jungle's avatar jungle Committed by Liam Morland
Browse files

Issue #3276361: Increase maximum length for pdf_key if it has values before

parent c10c7532
No related branches found
No related tags found
No related merge requests found
...@@ -283,27 +283,10 @@ function fillpdf_update_8111() { ...@@ -283,27 +283,10 @@ function fillpdf_update_8111() {
* Increase maximum length for pdf_key. * Increase maximum length for pdf_key.
*/ */
function fillpdf_update_9501() { function fillpdf_update_9501() {
$db = \Drupal::database(); // 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
$transaction = $db->startTransaction(); // values.
// @see https://www.drupal.org/project/fillpdf/issues/3276361
// 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);
} }
/** /**
...@@ -362,3 +345,60 @@ function fillpdf_update_9502() { ...@@ -362,3 +345,60 @@ function fillpdf_update_9502() {
// Commit transaction. // Commit transaction.
unset($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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment