1

Yes I have seen previous answers about that, but I'm looking for an new answer :

I'm not familiar enough with the Drupal 8 storage system so I also look for insight.

I have looked into a lot of documentation, including :

What get my attention is this : "Starting with Drupal 8.7.0, the Entity system provides a new Update API for converting the schema of a content entity type from (non-)revisionable/(non-)translatable to revisionable/translatable, which also works when there is pre-existing data for the entity type whose schema is being changed.."

Which indicated some built-in way to change a field type with existing data without having to write lots of code.

So I tried this to change a field type of my custom entity which already have data (~ 10000 rows).

function mymodule_post_update_0011_field_conversion(array $sandbox) {
    $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');

    $entity_type = $definition_update_manager->getEntityType('my_entity_id');
    $field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions('my_entity_id');

    // Update field already existing in $field_storage_definitions
   // I also tried to create a new field, with the same outcome.
    $field_storage_definitions['order_vat'] = BaseFieldDefinition::create('decimal')
      ->setLabel(t('Order VAT Rate'))
      ->setDescription(t('Order VAT Rate.'))
      ->setSettings(['precision' => 5, 'scale' => 2]);

    \Drupal::entityDefinitionUpdateManager()
      ->updateFieldableEntityType($entity_type, $field_storage_definitions,$sandbox);
  }

Which ends with a Drupal\Core\Entity\EntityStorageException : Unsupported entity type

I tried to use this same function to add a new field in the entity, with the same error as outcome.

Is there something wrong with my attempt, and is the only way to manage this is to manually, inside a post_update_hook, to create a new field with the appropriate type, copy all values from the old one to the new one, empty the old one, and delete the old field ?

That seems a lot for a simple operation, which, in my case, I could do with a quite simple SQL statement.

Last precision, this is a custom entity, created from code only (ContentEntityBase), without any revision (for now), which content is only manipulated programmatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.