Link Target Service¶
Service identity¶
- Service ID:
name.link_target - Interface:
Drupal\name\Service\LinkTargetInterface - Class:
Drupal\name\Service\LinkTargetService - Visibility:
@internal— powers thename_defaultformatter 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
_selfkeyed to$entityUrlLabel(the formatter passes$this->t('Entity URL')). - Scans the name field's bundle for configurable
entity_referenceandlinkfields (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:
linkfields use the field item URL.entity_referencefields 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()—#optionsfor the Link Target select (when the field has a bundle).settingsSummary()— human-readable link target label vianame.formatter_summary.viewElements()— sets$extra['url']passed intoname.formatterbefore 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.