I was writing an update hook to add a new field to a custom entity and was following the pattern shown here
https://www.drupal.org/node/2554097
/** * Add 'revision_translation_affected' field to 'node' entities. */ function node_update_8001() { // Install the definition that this field had in // DrupalnodeEntityNode::baseFieldDefinitions() // at the time that this update function was written. If/when code is // deployed that changes that definition, the corresponding module must // implement an update function that invokes // Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition() // with the new definition. $storage_definition = BaseFieldDefinition::create('boolean') ->setLabel(t('Revision translation affected')) ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) ->setReadOnly(TRUE) ->setRevisionable(TRUE) ->setTranslatable(TRUE); Drupal::entityDefinitionUpdateManager() ->installFieldStorageDefinition('revision_translation_affected', 'node', 'node', $storage_definition); }
I felt I was duplicating the BaseFieldDefinition in both the custom entity class as well as in the install file. Shouldn’t I be able to use the static function
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
from the entity class to load the field definition and install it?