Skip to content
Snippets Groups Projects
Commit 84fa48e2 authored by Liam Morland's avatar Liam Morland
Browse files

Issue #3047913: Convert 'default_entity_id' column into a string

parent 7c15d171
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......@@ -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'))
......
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