You are here

DynamicEntityReference.php in Entity Usage 8.4

File

src/Plugin/EntityTrack/Track/DynamicEntityReference.php
View source
<?php

namespace Drupal\entity_usage\Plugin\EntityTrack\Track;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_usage\EntityUsageBase;

/**
 * Tracks usage of entities related in dynamic_entity_reference fields.
 *
 * @EntityTrack(
 *   id = "dynamic_entity_reference",
 *   label = @Translation("Dynamic Entity Reference"),
 *   description = @Translation("Tracks relationships created with 'Dynamic Entity Reference' fields."),
 *   field_types = {"dynamic_entity_reference"},
 * )
 */
class DynamicEntityReference extends EntityUsageBase {

  /**
   * {@inheritdoc}
   */
  public function getTargetEntities(FieldItemInterface $item) {

    /** @var \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem$item */
    $item_value = $item
      ->getValue();
    if (empty($item_value['target_id']) || empty($item_value['target_type'])) {
      return [];
    }

    // Only return a valid result if the target entity exists.
    if (!$this->entityTypeManager
      ->getStorage($item_value['target_type'])
      ->load($item_value['target_id'])) {
      return [];
    }
    return [
      $item_value['target_type'] . '|' . $item_value['target_id'],
    ];
  }

}

Classes

Namesort descending Description
DynamicEntityReference Tracks usage of entities related in dynamic_entity_reference fields.