You are here

public function FieldLinkDetector::getTargetEntities in Lingotek Translation 3.6.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::getTargetEntities()
  2. 3.7.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::getTargetEntities()
  3. 3.8.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::getTargetEntities()

Get the target entity of a given link.

Parameters

\Drupal\Core\Field\FieldItemInterface $link: The link field item.

Return value

array Array of (target type, target id). Empty if no entity was linked.

1 call to FieldLinkDetector::getTargetEntities()
FieldLinkDetector::extract in src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php
Extract nested and related content.

File

src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php, line 141

Class

FieldLinkDetector
@RelatedEntitiesDetector ( id = "field_link_detector", title = Plugin annotation @Translation("Get editor linked entities with html links"), description = @translation("Get editor linked entities with html links."), weight = 7, )

Namespace

Drupal\lingotek\Plugin\RelatedEntitiesDetector

Code

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,
  ];
}