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\TrackView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityUsageBase:: |
protected | property | The EntityRepository service. | |
EntityUsageBase:: |
protected | property | The usage config. | |
EntityUsageBase:: |
protected | property | The usage tracking service. | |
EntityUsageBase:: |
public static | function | 2 | |
EntityUsageBase:: |
public | function | ||
EntityUsageBase:: |
public | function | ||
EntityUsageBase:: |
public | function | ||
EntityUsageBase:: |
public | function | Plugin constructor. | 2 |
Link:: |
public | function |
Retrieve the target entity(ies) from a field item value. Overrides EntityUsageBase:: |