You are here

public function BackgroundImage::getTargetEntity in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::getTargetEntity()
  2. 2.x src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::getTargetEntity()

Retrieves the target entity, if the type is supported and exists.

Parameters

int $type: The type. Defaults to the currently set type.

string $target: A target identifier split by a colon (:) where the entity type is on the left and the UUID of the entity to load is on the right. Defaults to the currently set target.

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

\Drupal\Core\Entity\EntityInterface|null The target Entity object or NULL if not a valid target.

Overrides BackgroundImageInterface::getTargetEntity

2 calls to BackgroundImage::getTargetEntity()
BackgroundImage::getParent in src/Entity/BackgroundImage.php
Retrieves the parent background image, if one exists.
BackgroundImage::label in src/Entity/BackgroundImage.php
Gets the label of the entity.

File

src/Entity/BackgroundImage.php, line 473

Class

BackgroundImage
Defines the Background Image entity.

Namespace

Drupal\background_image\Entity

Code

public function getTargetEntity($type = NULL, $target = NULL, $langcode = NULL, array $context = []) {
  if (!isset($type)) {
    $type = $this
      ->getType();
  }
  if (!isset($target)) {
    $target = $this
      ->getTarget();
  }
  $entity = NULL;
  if ($type === self::TYPE_ENTITY && isset($target)) {
    [
      $entity_type,
      $entity_id,
    ] = explode(':', $target);
    if (isset($entity_type) && isset($entity_id)) {
      $entity = $this
        ->getEntityRepository()
        ->loadEntityByUuid($entity_type, $entity_id);
    }
  }

  // Load entity with translation context.
  if ($entity instanceof EntityInterface) {
    return $this
      ->getEntityRepository()
      ->getTranslationFromContext($entity, $langcode ?: $this
      ->getLanguageCode(), $context);
  }
  return $entity;
}