function MediaLinkTarget::getLinkTarget

File

core/modules/media/src/Entity/MediaLinkTarget.php, line 44

Class

MediaLinkTarget
Provides a Media link target handler.

Namespace

Drupal\media\Entity

Code

public function getLinkTarget(EntityInterface $entity) : GeneratedUrl {
  // Below is an example of how to get the generated URL object for a media
  // linked entity which is used in Entity links filter.
  // @see \Drupal\filter\Plugin\Filter\EntityLinks::getUrl().
  // At this point, media entity type is not enabled for entity suggestions in
  // CKEditor.
  // @see \Drupal\ckeditor5\Hook\Ckeditor5Hooks::entityBundleInfoAlter().
  // Technically, media link target handler should not be in the core until
  // core supports media entity by default, consider this is an example on how
  // to build a media link target handler.
  assert($entity instanceof MediaInterface);
  if ($link_target = $entity->getSource()
    ->getMetadata($entity, MediaSourceInterface::METADATA_ATTRIBUTE_LINK_TARGET)) {
    return $link_target;
  }
  // Entities are returned unless standalone URLs are enabled, to avoid
  // meaningless links like this one
  return (new GeneratedUrl())->setGeneratedUrl('')
    ->setCacheMaxAge(Cache::PERMANENT);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.