Problem/Motivation

I have a custom entity type, that already installed on site, and works well.
And in entityType class via annotation I set custom handler for "storage_schema":

 * @ContentEntityType(
 *   id = "my_entity",
...
 *   handlers = {
...
 *     "storage_schema" = "Drupal\my_module\Storage\MyEntityStorageSchema",

In the "MyEntityStorageSchema.php" file I make custom additions to getEntitySchema() (add indexes to some fields).

And on drush entup this class isn't called, so fields are created without my customizations.

Steps to reproduce

1. Create custom entity with some fields, add this fields via `drush entup`.
2. Fill custom handlers.storage_schema value into entity class annotation.
3. Create custom class, extends SqlContentEntityStorageSchema, make function getEntitySchema()
4. Turn on debugger, create a breakpoint on this function.
5. Add some field to your entity, launch drush entup and wait for finish.

You will see that field created successfully, but the MyEntityStorageSchema::getEntitySchema() handler function will not called.

Proposed resolution

The problem is that "Devel Entity Updates" module uses list of handlers from database, that didn't always match to actual from entity class file. If we call

\Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);

before drush entup - it refresh list of handlers in database, and all goes to work well.

Can you extend "Devel Entity Updates" module to use actual entity annotation from file, instead of cached in database, when checking entity changes?

Comments

Murz created an issue. See original summary.

murz’s picture

Seems this is because "Devel Entity Updates" module uses entityLastInstalledSchemaRepository instead of rescan fresh handlers from source code.
If I manually update the storage_schema for entity handler via code like this:

  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager->getEntityType('my_entity');
  $entity_type->setHandlerClass('storage_schema', 'Drupal\my_module\Storage\MyEntityStorageSchema');
  $definition_update_manager->updateEntityType($entity_type);

- class called normally.

Is it possible to include into this module support for entity handlers update too?

murz’s picture

More info: when \Drupal\Core\Entity\Sql\SqlContentEntityStorage is called, it replaces current $entity_type to stored $entity type via this line in function __construct():

$this->entityType = $this->entityTypeManager->getActiveDefinition($entity_type->id());

instead of:

$this->entityType = $this->entityTypeManager->getDefinition($entity_type->id());

As result, all other operations use old entityType data (from database), instead of actual (from code).

murz’s picture

Title: Module didn't call custom storage_schema's handler for custom entities » Module didn't check and update handlers for custom entities
Issue summary: View changes
murz’s picture

Same problem with entity cache settings, when we changing them in code, for example setting the:

 *   render_cache = FALSE,
 *   persistent_cache = FALSE,
 *   translatable = FALSE,

and clearing caches, doing drush entup that show no changes - in the getActiveDefinition() we will see old values (that was TRUE by default).