You are here

public function DynamicEntityReference::getTarget in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/DataType/DynamicEntityReference.php \Drupal\dynamic_entity_reference\Plugin\DataType\DynamicEntityReference::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/Plugin/DataType/DynamicEntityReference.php, line 41

Class

DynamicEntityReference
Defines a 'dynamic_entity_reference' data type.

Namespace

Drupal\dynamic_entity_reference\Plugin\DataType

Code

public function getTarget() {

  // If we have a valid reference, return the entity's TypedData adapter.
  if (!isset($this->target) && isset($this->id)) {

    // For \Drupal\Core\Entity\Plugin\DataType\EntityReference
    // $this->getTargetDefinition()->getEntityTypeId() will always be set
    // because $target_type exists in EntityReferenceItem storage settings but
    // for
    // \Drupal\dynamic_entity_reference\Plugin\DataType\DynamicEntityReference
    // $target_type will be NULL because it doesn't exist in
    // DynamicEntityReferenceItem storage settings it is selected dynamically
    // so it exists in DynamicEntityReferenceItem::values['target_type'].
    $target_type = $this->parent
      ->getValue()['target_type'];
    $entity = \Drupal::entityTypeManager()
      ->getStorage($target_type)
      ->load($this->id);
    $this->target = isset($entity) ? $entity
      ->getTypedData() : NULL;
  }
  return $this->target;
}