function DefaultContentSubscriber::preExport
Same name in this branch
- 11.x core/modules/media/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\media\EventSubscriber\DefaultContentSubscriber::preExport()
- 11.x core/modules/layout_builder/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\layout_builder\EventSubscriber\DefaultContentSubscriber::preExport()
- 11.x core/modules/content_moderation/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\content_moderation\EventSubscriber\DefaultContentSubscriber::preExport()
- 11.x core/modules/path/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\path\EventSubscriber\DefaultContentSubscriber::preExport()
Reacts before an entity is exported.
Adds an export callback for `link` field items to ensure that, if the link points to a content entity, it is marked as a dependency of the entity being exported.
Parameters
\Drupal\Core\DefaultContent\PreExportEvent $event: The event object.
File
-
core/
modules/ link/ src/ EventSubscriber/ DefaultContentSubscriber.php, line 43
Class
- DefaultContentSubscriber
- Subscribes to default content-related events.
Namespace
Drupal\link\EventSubscriberCode
public function preExport(PreExportEvent $event) : void {
$event->setCallback('field_item:link', function (LinkItemInterface $item, ExportMetadata $metadata) : array {
$values = $item->getValue();
$url = $item->getUrl();
if (!$url->isRouted()) {
// The URL is not routed, so there's nothing else to do.
return $values;
}
$route_name = explode('.', $url->getRouteName());
// We can rely on this pattern because routed entity URLs are generated
// in a consistent way with the `entity` scheme.
// @see \Drupal\Core\Url::fromUri()
if (count($route_name) === 3 && $route_name[0] === 'entity' && $route_name[2] === 'canonical') {
$target_entity_type_id = $route_name[1];
$route_parameters = $url->getRouteParameters();
$target_id = $route_parameters[$target_entity_type_id];
$target = $this->entityTypeManager
->getStorage($target_entity_type_id)
->load($target_id);
if ($target instanceof ContentEntityInterface) {
$values['target_uuid'] = $target->uuid();
unset($values['uri']);
$metadata->addDependency($target);
}
}
return $values;
});
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.