/** * Builds the \Drupal\Core\Url object for a link field item. * * @param \Drupal\link\LinkItemInterface $item * The link field item being rendered. * * @return \Drupal\Core\Url * A Url object. */ protected function buildUrl(LinkItemInterface $item) { $url = $item->getUrl() ?: Url::fromRoute('<none>'); $settings = $this->getSettings(); $options = $item->options; // 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; }
/** * Builds the \Drupal\Core\Url object for a link field item. * * @param \Drupal\link\LinkItemInterface $item * The link field item being rendered. * * @return \Drupal\Core\Url * An Url object. */ protected function buildUrl(LinkItemInterface $item) { $settings = $this->getSettings(); $options = $item->options; // 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']; } if ($item->isExternal()) { $url = Url::fromUri($item->url, $options); } else { $url = Url::fromRoute($item->route_name, (array) $item->route_parameters, (array) $options); } return $url; }