You are here

protected function LinkFormatter::buildUrl in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::buildUrl()

Builds the \Drupal\Core\Url object for a link field item.

Parameters

\Drupal\link\LinkItemInterface $item: The link field item being rendered.

Return value

\Drupal\Core\Url A Url object.

2 calls to LinkFormatter::buildUrl()
LinkFormatter::viewElements in core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php
Builds a renderable array for a field value.
LinkSeparateFormatter::viewElements in core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php
Builds a renderable array for a field value.

File

core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php, line 239

Class

LinkFormatter
Plugin implementation of the 'link' formatter.

Namespace

Drupal\link\Plugin\Field\FieldFormatter

Code

protected function buildUrl(LinkItemInterface $item) {
  $url = $item
    ->getUrl() ?: Url::fromRoute('<none>');
  $settings = $this
    ->getSettings();
  $options = $item->options;
  $options += $url
    ->getOptions();

  // Add optional 'rel' attribute to link options.
  if (!empty($settings['rel'])) {
    $options['attributes']['rel'] = $settings['rel'];
  }

  // Add optional 'target' attribute to link options.
  if (!empty($settings['target'])) {
    $options['attributes']['target'] = $settings['target'];
  }
  $url
    ->setOptions($options);
  return $url;
}