class EntityRepository implements EntityRepositoryInterface (View source)

Provides several mechanisms for retrieving entities.

Properties

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected LanguageManagerInterface $languageManager

The language manager.

protected ContextRepositoryInterface $contextRepository

The context repository service.

Methods

__construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ContextRepositoryInterface $context_repository)

Constructs a new EntityRepository.

loadEntityByUuid(string $entity_type_id, string $uuid)

Loads an entity by UUID.

loadEntityByConfigTarget(string $entity_type_id, string $target)

Loads an entity by the config target identifier.

getTranslationFromContext(EntityInterface $entity, string $langcode = NULL, array $context = [])

Gets the entity translation to be used in the given context.

getActive(string $entity_type_id, int|string $entity_id, array $contexts = NULL)

Retrieves the active entity variant matching the specified context.

getActiveMultiple(string $entity_type_id, array $entity_ids, array $contexts = NULL)

Retrieves the active entity variants matching the specified context.

getCanonical(string $entity_type_id, int|string $entity_id, array $contexts = NULL)

Retrieves the canonical entity variant matching the specified context.

getCanonicalMultiple(string $entity_type_id, array $entity_ids, array $contexts = NULL)

Retrieves the canonical entity variants matching the specified context.

string|null
getContentLanguageFromContexts(array $contexts)

Retrieves the current content language from the specified contexts.

getLatestTranslationAffectedRevision(RevisionableInterface $entity, string $langcode)

Returns the latest revision translation of the specified entity.

loadRevision(RevisionableInterface $entity, string $revision_id)

Loads the specified entity revision.

Details

__construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ContextRepositoryInterface $context_repository)

Constructs a new EntityRepository.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

LanguageManagerInterface $language_manager

The language manager.

ContextRepositoryInterface $context_repository

The context repository service.

EntityInterface|null loadEntityByUuid(string $entity_type_id, string $uuid)

Loads an entity by UUID.

Note that some entity types may not support UUIDs.

Parameters

string $entity_type_id

The entity type ID to load from.

string $uuid

The UUID of the entity to load.

Return Value

EntityInterface|null

The entity object, or NULL if there is no entity with the given UUID.

Exceptions

EntityStorageException

EntityInterface|null loadEntityByConfigTarget(string $entity_type_id, string $target)

Loads an entity by the config target identifier.

Parameters

string $entity_type_id

The entity type ID to load from.

string $target

The configuration target to load, as returned from \Drupal\Core\Entity\EntityInterface::getConfigTarget().

Return Value

EntityInterface|null

The entity object, or NULL if there is no entity with the given config target identifier.

Exceptions

EntityStorageException

EntityInterface getTranslationFromContext(EntityInterface $entity, string $langcode = NULL, array $context = [])

Gets the entity translation to be used in the given context.

This will check whether a translation for the desired language is available and if not, it will fall back to the most appropriate translation based on the provided context.

Parameters

EntityInterface $entity

The entity whose translation will be returned.

string $langcode

(optional) The language of the current context. Defaults to the current content language.

array $context

(optional) An associative array of arbitrary data that can be useful to determine the proper fallback sequence.

Return Value

EntityInterface

An entity object for the translated data.

EntityInterface|null getActive(string $entity_type_id, int|string $entity_id, array $contexts = NULL)

Retrieves the active entity variant matching the specified context.

If an entity type is revisionable and/or translatable, which entity variant should be handled depends on the context in which the manipulation happens. Based on the specified contextual information, revision and translation negotiation needs to be performed to return the active variant, that is the most up-to-date entity variant in the context scope. This may or may not be an entity variant intended for unprivileged user consumption, in fact it might be a work in progress containing yet to be published information. The active variant should always be retrieved when editing an entity, both in form and in REST workflows, or previewing the related changes.

The negotiation process will not perform any access check, so it is the responsibility of the caller to verify that the user manipulating the entity variant is actually allowed to do so.

Parameters

string $entity_type_id

The entity type identifier.

int|string $entity_id

An entity identifier.

array $contexts

(optional) An associative array of objects representing the contexts the entity will be edited in keyed by fully qualified context ID. Defaults to the currently available contexts.

Return Value

EntityInterface|null

An entity object variant or NULL if the entity does not exist.

EntityInterface[] getActiveMultiple(string $entity_type_id, array $entity_ids, array $contexts = NULL)

Retrieves the active entity variants matching the specified context.

Parameters

string $entity_type_id

The entity type identifier.

array $entity_ids

An array of entity identifiers.

array $contexts

(optional) An associative array of objects representing the contexts the entity will be edited in keyed by fully qualified context ID. Defaults to the currently available contexts.

Return Value

EntityInterface[]

An array of entity object variants keyed by entity ID.

EntityInterface|null getCanonical(string $entity_type_id, int|string $entity_id, array $contexts = NULL)

Retrieves the canonical entity variant matching the specified context.

If an entity type is revisionable and/or translatable, which entity variant should be handled depends on the context in which the manipulation happens. This will return the fittest entity variant intended for unprivileged user consumption matching the specified context. This is typically the variant that would be displayed on the entity's canonical route.

The negotiation process will not perform any access check, so it is the responsibility of the caller to verify that the user manipulating the entity variant is actually allowed to do so.

Parameters

string $entity_type_id

The entity type identifier.

int|string $entity_id

An entity identifier.

array $contexts

(optional) An associative array of objects representing the contexts the entity will be edited in keyed by fully qualified context ID. Defaults to the currently available contexts.

Return Value

EntityInterface|null

An entity object variant or NULL if the entity does not exist.

EntityInterface[] getCanonicalMultiple(string $entity_type_id, array $entity_ids, array $contexts = NULL)

Retrieves the canonical entity variants matching the specified context.

Parameters

string $entity_type_id

The entity type identifier.

array $entity_ids

An array of entity identifiers.

array $contexts

(optional) An associative array of objects representing the contexts the entity will be edited in keyed by fully qualified context ID. Defaults to the currently available contexts.

Return Value

EntityInterface[]

An array of entity object variants keyed by entity ID.

protected string|null getContentLanguageFromContexts(array $contexts)

Retrieves the current content language from the specified contexts.

Parameters

array $contexts

An array of context items.

Return Value

string|null

A language code or NULL if no language context was provided.

protected RevisionableInterface getLatestTranslationAffectedRevision(RevisionableInterface $entity, string $langcode)

Returns the latest revision translation of the specified entity.

Parameters

RevisionableInterface $entity

The default revision of the entity being converted.

string $langcode

The language of the revision translation to be loaded.

Return Value

RevisionableInterface

The latest translation-affecting revision for the specified entity, or just the latest revision, if the specified entity is not translatable or does not have a matching translation yet.

protected RevisionableInterface loadRevision(RevisionableInterface $entity, string $revision_id)

Loads the specified entity revision.

Parameters

RevisionableInterface $entity

The default revision of the entity being converted.

string $revision_id

The identifier of the revision to be loaded.

Return Value

RevisionableInterface

An entity revision object.