You are here

protected function NameFormatter::getLinkableTargetUrl in Name Field 8

Gets the URL object.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The name formatters FieldItemList.

Return value

\Drupal\Core\Url Returns a Url object.

1 call to NameFormatter::getLinkableTargetUrl()
NameFormatter::viewElements in src/Plugin/Field/FieldFormatter/NameFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/NameFormatter.php, line 358

Class

NameFormatter
Plugin implementation of the 'name' formatter.

Namespace

Drupal\name\Plugin\Field\FieldFormatter

Code

protected function getLinkableTargetUrl(FieldItemListInterface $items) {
  try {
    $parent = $items
      ->getEntity();
    if ($this->settings['link_target'] == '_self') {
      if (!$parent
        ->isNew() && $parent
        ->access('view')) {
        return $parent
          ->toUrl();
      }
    }
    elseif ($parent
      ->hasField($this->settings['link_target'])) {
      $target_items = $parent
        ->get($this->settings['link_target']);
      if (!$target_items
        ->isEmpty()) {
        $field = $target_items
          ->getFieldDefinition();
        switch ($field
          ->getType()) {
          case 'entity_reference':
            foreach ($target_items as $item) {
              if (!empty($item->entity) && !$item->entity
                ->isNew() && $item->entity
                ->access('view')) {
                return $item->entity
                  ->toUrl();
              }
            }
            break;
          case 'link':
            foreach ($target_items as $item) {
              if ($url = $item
                ->getUrl()) {
                return $url;
              }
            }
            break;
        }
      }
    }
  } catch (UndefinedLinkTemplateException $e) {
  }
  return Url::fromRoute('<none>');
}