Juan Carlos Orrego
1 min readAug 12, 2022

Adding / Updating custom entities to an existing module

When a module is already enabled, you have to use the Entity Update API to make updates that involve schema changes.

Automatic entity updates are now removed, in this post are some useful samples of update hooks to different cases:

For installing a new Entity, the following code can be used to avoid duplicating the entire definition in an update hook:

/**
* Installs the new "__your_entity__" entity.
*/
function your_module_update_9001() {
$db = \Drupal::database();
if (!$db->schema()->tableExists("__your_entity__")) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()->installEntityType(
\Drupal::entityTypeManager()->getDefinition("__your_entity__")
);
}
}

Related

This link provide initial sample on creating a custom entity:

Updating Entities and Fields in Drupal 8

Juan Carlos Orrego
0 Followers

Developer, heavy user of: Laravel, VueJS, Drupal, TailwindCSS, AWS…