interface EntityDefinitionUpdateManagerInterface (View source)

Defines an interface for managing entity definition updates.

During the application lifetime, the definitions of various entity types and their data components (e.g., fields for fieldable entity types) can change. For example, updated code can be deployed. Some entity handlers may need to perform complex or long-running logic in response to the change. For example, a SQL-based storage handler may need to update the database schema.

To support this, \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface has methods to retrieve the last installed definitions as well as the definitions specified by the current codebase. It also has create/update/delete methods to bring the former up to date with the latter.

However, it is not the responsibility of the entity manager to decide how to report the differences or when to apply each update. This interface is for managing that.

This interface also provides methods to retrieve instances of the definitions to be updated ready to be manipulated. In fact when definitions change in code the system needs to be notified about that and the definitions stored in state need to be reconciled with the ones living in code. This typically happens in Update API functions, which need to take the system from a known state to another known state. Relying on the definitions living in code might prevent this, as the system might transition directly to the last available state, and thus skipping the intermediate steps. Manipulating the definitions in state allows to avoid this and ensures that the various steps of the update process are predictable and repeatable.

Constants

DEFINITION_CREATED

Indicates that a definition has just been created.

DEFINITION_UPDATED

Indicates that a definition has changes.

DEFINITION_DELETED

Indicates that a definition has just been deleted.

Methods

bool
needsUpdates()

Checks if there are any definition updates that need to be applied.

array
getChangeSummary()

Gets a human readable summary of the detected changes.

array
getChangeList()

Gets a list of changes to entity type and field storage definitions.

applyUpdates() deprecated

Applies all the detected valid changes.

getEntityType(string $entity_type_id)

Returns an entity type definition ready to be manipulated.

getEntityTypes()

Returns all the entity type definitions, ready to be manipulated.

installEntityType(EntityTypeInterface $entity_type)

Installs a new entity type definition.

installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions)

Installs a new fieldable entity type definition.

updateEntityType(EntityTypeInterface $entity_type)

Applies any change performed to the passed entity type definition.

uninstallEntityType(EntityTypeInterface $entity_type)

Uninstalls an entity type definition.

updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array $sandbox = NULL)

Applies any change performed to a fieldable entity type definition.

getFieldStorageDefinition(string $name, string $entity_type_id)

Returns a field storage definition ready to be manipulated.

installFieldStorageDefinition(string $name, string $entity_type_id, string $provider, FieldStorageDefinitionInterface $storage_definition)

Installs a new field storage definition.

updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition)

Applies any change performed to the passed field storage definition.

uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition)

Uninstalls a field storage definition.

Details

bool needsUpdates()

Checks if there are any definition updates that need to be applied.

Return Value

bool

TRUE if updates are needed.

array getChangeSummary()

Gets a human readable summary of the detected changes.

Return Value

array

An associative array keyed by entity type id. Each entry is an array of human-readable strings, each describing a change.

array getChangeList()

Gets a list of changes to entity type and field storage definitions.

Return Value

array

An associative array keyed by entity type ID of change descriptors. Every entry is an associative array with the following optional keys:

  • entity_type: a scalar having one value among:
    • EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED
    • EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED
  • field_storage_definitions: an associative array keyed by field name of scalars having one value among:
    • EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED
    • EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED
    • EntityDefinitionUpdateManagerInterface::DEFINITION_DELETED

applyUpdates() deprecated

deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::getChangeList() and execute each entity type and field storage update manually instead.

Applies all the detected valid changes.

Use this with care, as it will apply updates for any module, which will lead to unpredictable results.

EntityTypeInterface getEntityType(string $entity_type_id)

Returns an entity type definition ready to be manipulated.

When needing to apply updates to existing entity type definitions, this method should always be used to retrieve a definition ready to be manipulated.

Parameters

string $entity_type_id

The entity type identifier.

Return Value

EntityTypeInterface

The entity type definition.

EntityTypeInterface[] getEntityTypes()

Returns all the entity type definitions, ready to be manipulated.

When needing to apply updates to existing entity type definitions, this method should always be used to retrieve all the definitions ready to be manipulated.

Return Value

EntityTypeInterface[]

The last installed entity type definitions, keyed by the entity type ID.

installEntityType(EntityTypeInterface $entity_type)

Installs a new entity type definition.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions)

Installs a new fieldable entity type definition.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

array $field_storage_definitions

The entity type's field storage definitions.

updateEntityType(EntityTypeInterface $entity_type)

Applies any change performed to the passed entity type definition.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

uninstallEntityType(EntityTypeInterface $entity_type)

Uninstalls an entity type definition.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array $sandbox = NULL)

Applies any change performed to a fieldable entity type definition.

Parameters

EntityTypeInterface $entity_type

The updated entity type definition.

array $field_storage_definitions

The updated field storage definitions, including possibly new ones.

array $sandbox

(optional) A sandbox array provided by a hook_update_N() implementation or a Batch API callback. If the entity schema update requires a data migration, this parameter is mandatory. Defaults to NULL.

FieldStorageDefinitionInterface getFieldStorageDefinition(string $name, string $entity_type_id)

Returns a field storage definition ready to be manipulated.

When needing to apply updates to existing field storage definitions, this method should always be used to retrieve a storage definition ready to be manipulated.

Make this return a mutable storage definition interface when we have one. See https://www.drupal.org/node/2346329.

Parameters

string $name

The field name.

string $entity_type_id

The entity type identifier.

Return Value

FieldStorageDefinitionInterface

The field storage definition.

installFieldStorageDefinition(string $name, string $entity_type_id, string $provider, FieldStorageDefinitionInterface $storage_definition)

Installs a new field storage definition.

Parameters

string $name

The field storage definition name.

string $entity_type_id

The target entity type identifier.

string $provider

The name of the definition provider.

FieldStorageDefinitionInterface $storage_definition

The field storage definition.

updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition)

Applies any change performed to the passed field storage definition.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field storage definition.

uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition)

Uninstalls a field storage definition.

Parameters

FieldStorageDefinitionInterface $storage_definition

The field storage definition.