Skip to content

Link Target Service

Service identity

  • Service ID: name.link_target
  • Interface: Drupal\name\Service\LinkTargetInterface
  • Class: Drupal\name\Service\LinkTargetService
  • Visibility: @internal — powers the name_default formatter plugin; not a supported extension point for custom modules.

Resolves formatter link target options and URLs for the Name field display formatter (link_target setting).

Methods

getTargets(FieldDefinitionInterface $fieldDefinition, TranslatableMarkup $entityUrlLabel): array

Builds the select-list options for the formatter Link Target setting.

  • Always includes _self keyed to $entityUrlLabel (the formatter passes $this->t('Entity URL')).
  • Scans the name field's bundle for configurable entity_reference and link fields (base fields are skipped).
  • Returns machine names keyed to field labels.

resolveUrl(FieldItemListInterface $items, string $linkTarget): Url

Resolves the URL used when wrapping formatted name output in a link.

  • _self — parent entity URL when the entity is not new, is viewable, and has a link template; otherwise <none>.
  • Field machine name — first non-empty value on that field on the parent entity:
  • link fields use the field item URL.
  • entity_reference fields use the first viewable referenced entity URL.
  • Returns Url::fromRoute('<none>') when no applicable URL exists.

Where it is used

Drupal\name\Plugin\Field\FieldFormatter\NameFormatter injects this service:

  • settingsForm()#options for the Link Target select (when the field has a bundle).
  • settingsSummary() — human-readable link target label via name.formatter_summary.
  • viewElements() — sets $extra['url'] passed into name.formatter before formatting each item.

When to use

Do not inject name.link_target from application code. Configure linking through the formatter link_target setting on the entity view display. See Link targets in formatter output.

For debugging only:

$link_target = \Drupal::service('name.link_target');
$options = $link_target->getTargets(
  $field_definition,
  t('Entity URL'),
);

Prefer display configuration or the examples above for production behavior.