Uninstall a custom field storage definition from an existing entity

Created on 6 July 2023, 12 months ago

Problem/Motivation

**Drupal 10.0.9**

I'm removing a custom field from an existing entity with the following code in a hook_update_N(); executing the database update drush command I get the error:

  >  [error]  Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getTableMapping(): Argument #2 ($storage_definitions) must be of type ?arra
  y, Drupal\Core\Field\BaseFieldDefinition given, called in /var/www/html/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
   on line 744

Steps to reproduce

Add a custom field

function my_module_update_8001(): void {
  $storage_definition = BaseFieldDefinition::create('string_long')
    ->setLabel(t('My field'))
    ->setDescription(t('My custom field.'))
    ->setReadOnly(TRUE)
    ->setDefaultValue('');

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('my_field_name', 'my_entity', 'my_module', $storage_definition);
}

then remove it

function my_module_update_8002(): void {
  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $definition = $update_manager->getFieldStorageDefinition('my_field_name', 'my_entity');
  $update_manager->uninstallFieldStorageDefinition($definition);
}

Proposed resolution

Replace line 744 in the class Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema with

$table_mapping = $this->getTableMapping($this->entityType, [$deleted_storage_definition]);

Remaining tasks

Make the patch.

Release notes snippet

TBD

🐛 Bug report
Status

Closed: cannot reproduce

Version

10.0

Component
Entity 

Last updated about 8 hours ago

Created by

🇮🇹Italy robertoperuzzo 🇮🇹 Tezze sul Brenta, VI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024