You are here

public function EntityRepository::getCanonical in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getCanonical()

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.

\Drupal\Core\Plugin\Context\ContextInterface[] $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

\Drupal\Core\Entity\EntityInterface|null An entity object variant or NULL if the entity does not exist.

Overrides EntityRepositoryInterface::getCanonical

File

core/lib/Drupal/Core/Entity/EntityRepository.php, line 179

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

public function getCanonical($entity_type_id, $entity_id, array $contexts = NULL) {
  return current($this
    ->getCanonicalMultiple($entity_type_id, [
    $entity_id,
  ], $contexts)) ?: NULL;
}