function LinkFormatter::buildUrl

Same name and namespace in other branches
  1. 8.9.x core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::buildUrl()
  2. 10 core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::buildUrl()
  3. 11.x 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) {
    try {
        $url = $item->getUrl();
    } catch (\InvalidArgumentException $e) {
        // @todo Add logging here in https://www.drupal.org/project/drupal/issues/3348020
        $url = 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;
}

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