How to install a new entity type from class annotations in an update hook

Created on 5 October 2022, over 1 year ago
Updated 30 September 2023, 9 months ago

Problem/Motivation

In the CAPTCHA Riddler module we just ran into this issue: #3313384: Error "Entity type riddle must be installed" after update β†’

TL;DR:
Version 2.x of the module now uses a new Riddle ConfigEntityType instead of custom storage config to store the riddles.

For folks fresh installing the 2.x version, everything is fine.
But if someone upgrades from 8.x-1.x to 2.x, the status report complains, that the entity or field definition is missing.
I know this comes from [#2554097] but the situation how to handle this seems super bad UX (or I don't know how to do it right).

I couldn't believe I have to duplicate all the properties of the entity type again as array in the update hook, but I found similar implementations, for example here: #3172255: Install new config entity type via update hook. β†’

What we have is https://www.drupal.org/docs/drupal-apis/update-api/updating-configuratio... β†’
But what would help a lot here to stop duplicating things and allow to make an update as easy as an installation would be to be able to install the entity type from the Riddle ConfigEntityType annotations.

What I'd expect would be something like:

use Drupal\riddler\Entity\Riddle;
\Drupal::entityDefinitionUpdateManager()->installEntityTypeFromClass(Riddle::class);

to install the type from the class and its annotations.

Is there anything like this? If yes, I'd like to add it to documentation. If not, we should really discuss adding such helpers or developers will lose a lot of fun with Drupal with this huge and hard difference between installation and update!

Code duplication is bad, anyway.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ’¬ Support request
Status

Active

Version

11.0 πŸ”₯

Component
Configuration entityΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡­πŸ‡·Croatia Aporie

    Hi Anybody,

    Yes it works. Was also looking for something like that for a while. Would be great to add it to the doc indeed.

    On a custom module, this can be reduced to two lines (so don't think we need a wrapper):

    $entity_type_definition = \Drupal::service('entity_type.manager')->getDefinition('custom_entity_type');
    \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type_definition);
    
Production build 0.69.0 2024