Skip to content

Instantly share code, notes, and snippets.

@klaasvw
Created December 1, 2023 14:14
Show Gist options
  • Save klaasvw/43b73dc42be4d85553f44024b5831d5a to your computer and use it in GitHub Desktop.
Save klaasvw/43b73dc42be4d85553f44024b5831d5a to your computer and use it in GitHub Desktop.
Add base field to existing entity type
/**
* Add the entity_type field_name field.
*/
function module_name_update_9001() {
$type_manager = \Drupal::entityTypeManager();
$type_manager->clearCachedDefinitions();
$entity_type = $type_manager->getDefinition('entity_type');
$storage_definition = EntityTypeClass::baseFieldDefinitions($entity_type);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition(
'field_name',
$entity_type->id(),
'entity_type',
$storage_definition['field_name']
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment