You are here

class Link in Entity Usage 8.4

Tracks usage of entities related in Link fields.

Plugin annotation


@EntityTrack(
  id = "link",
  label = @Translation("Link Fields"),
  description = @Translation("Tracks relationships created with 'Link' fields."),
  field_types = {"link"},
)

Hierarchy

  • class \Drupal\entity_usage\EntityUsageBase extends \Drupal\entity_track\EntityTrackBase
    • class \Drupal\entity_usage\Plugin\EntityTrack\Track\Link

Expanded class hierarchy of Link

File

src/Plugin/EntityTrack/Track/Link.php, line 18

Namespace

Drupal\entity_usage\Plugin\EntityTrack\Track
View source
class Link extends EntityUsageBase {

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

    /** @var \Drupal\link\LinkItemInterface $link */

    // Check if the link is referencing an entity.
    $url = $link
      ->getUrl();
    if (!$url
      ->isRouted() || !preg_match('/^entity\\./', $url
      ->getRouteName())) {
      return [];
    }

    // Ge the target entity type and ID.
    $route_parameters = $url
      ->getRouteParameters();
    $target_type = array_keys($route_parameters)[0];
    $target_id = $route_parameters[$target_type];

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

}

Members

Namesort descending Modifiers Type Description Overrides
EntityUsageBase::$entityRepository protected property The EntityRepository service.
EntityUsageBase::$usageConfig protected property The usage config.
EntityUsageBase::$usageService protected property The usage tracking service.
EntityUsageBase::create public static function 2
EntityUsageBase::trackOnEntityCreation public function
EntityUsageBase::trackOnEntityDelete public function
EntityUsageBase::trackOnEntityUpdate public function
EntityUsageBase::__construct public function Plugin constructor. 2
Link::getTargetEntities public function Retrieve the target entity(ies) from a field item value. Overrides EntityUsageBase::getTargetEntities