You are here

public function EntityReference::getTarget in Multiversion 8

Same name and namespace in other branches
  1. 8.2 src/EntityReference.php \Drupal\multiversion\EntityReference::getTarget()

Gets the referenced data.

Return value

\Drupal\Core\TypedData\TypedDataInterface|null The referenced typed data object, or NULL if the reference is unset.

Overrides EntityReference::getTarget

File

src/EntityReference.php, line 23

Class

EntityReference
Alternative entity reference data type class.

Namespace

Drupal\multiversion

Code

public function getTarget() {

  // Try getting the target entity the default way. If no target was found
  // this might mean it was deleted. In that case, we try to load the deleted
  // entity instead.
  parent::getTarget();
  if (!isset($this->target) && isset($this->id)) {

    /** @var EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');

    /** @var \Drupal\multiversion\MultiversionManagerInterface $multiversion_manager */
    $multiversion_manager = \Drupal::service('multiversion.manager');
    $entity_type_id = $this
      ->getTargetDefinition()
      ->getEntityTypeId();
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    if ($multiversion_manager
      ->isEnabledEntityType($entity_type)) {

      /** @var \Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface $storage */
      $storage = $entity_type_manager
        ->getStorage($entity_type_id);
      $entity = $storage
        ->loadDeleted($this->id);
      $this->target = isset($entity) ? $entity
        ->getTypedData() : NULL;
    }
  }
  return $this->target;
}