Linked Field
Allows you to link any field to a specific destination URL, supporting both field-based and custom destinations with token replacement.
linked_field
Install
composer require 'drupal/linked_field:8.x-1.7'
Overview
Linked Field is a powerful module that adds the ability to turn any field output into a clickable link pointing to a specified destination. Instead of modifying your theme or creating custom field formatters, you can simply configure any field's display settings to wrap its content in an anchor tag.
The module works by injecting third-party settings into existing field formatter configurations. When viewing an entity, Linked Field intercepts the field rendering process and wraps the field output with an anchor tag. This approach means it works with any field type and any formatter without requiring special integration.
Destinations can be configured in two ways: by selecting another field on the same entity (such as a Link field or text field containing a URL), or by entering a custom destination that supports Drupal's token system. The latter enables dynamic linking based on entity properties, such as linking a field to the entity's canonical URL using [node:url] token.
The module also provides an administration interface where site administrators can define which HTML attributes are available for configuration. By default, the module provides title, target, class, and rel attributes, but additional attributes can be added through the YAML-based configuration interface.
Features
- Link any field output to a destination URL without custom theming or development
- Two destination types: Field-based (uses value from another field like Link or text fields) and Custom (supports tokens)
- Full token support in custom destinations allowing dynamic URLs like [node:url] or [node:field_custom_link:uri]
- Configurable HTML attributes including title, target, class, and rel with support for custom attributes
- Intelligent DOM processing that wraps text nodes in anchor tags while handling images and pictures specially
- Class and rel attributes are merged and deduplicated when combining existing attributes with configured values
- Custom link text option that overrides the original field output with a token-replaced value
- XSS filtering on all output to prevent security vulnerabilities
- Integration with Token module for a token browser in the configuration interface
- High module weight (100) ensures it runs after other field formatters for proper output modification
Use Cases
Link an image field to a custom URL field
You have a content type with an Image field and a Link field called 'External URL'. You want clicking the image to navigate to the external URL. Configure the Image field's display: check 'Link this field', select Type: Field, choose 'External URL' from the field dropdown. Now the image will be wrapped in an anchor tag pointing to the Link field's URL.
Link a field to the entity's canonical URL
You want a Title or Description field to link to the full node page. Configure the field's display: check 'Link this field', select Type: Custom, enter '[node:url]' as the destination. The field output will link to the node's canonical URL.
Add CSS class and target attribute to linked fields
You want linked fields to open in a new tab with a specific CSS class. In the Advanced section of the linked field settings, enter '_blank' for Target and 'external-link' for Class. The generated anchor will include these attributes.
Create a teaser link using tokens
On a teaser view mode, you want the body summary to link to the full article with a custom title attribute. Configure the Body field: enable linking, use '[node:url]' as custom destination, set Title attribute to 'Read more about [node:title]'. The summary will have a descriptive link tooltip.
Link to a related entity
You have an Article with an Entity Reference field to an Author profile. You want to link the author's name display to their profile page. Use Custom destination type with token like '[node:field_author:url]' to link to the referenced author's canonical URL.
Add custom HTML attributes
Your site requires 'data-tracking' attributes on links for analytics. Go to /admin/config/linked_field/config and add a new attribute in the YAML configuration: 'data-tracking: {label: Tracking ID, description: Analytics tracking identifier}'. This attribute will now appear in all Linked Field advanced settings.
Tips
- Install the Token module for a convenient token browser in the configuration interface
- Install YAML Editor module for syntax highlighting when editing attribute configuration
- Use '[entity:url]' tokens for linking to entity canonical pages - replace 'entity' with the actual type (node, user, taxonomy_term)
- Class and rel attributes intelligently merge values, so you can combine existing field attributes with your configured values
- For link fields used as destinations, the module preserves attributes like target from the link field and merges with your configured values
- The Text option in Advanced settings completely replaces the field output with your token text - useful for 'Read more' links
- Test token replacements in isolation first using the Token module's token browser to ensure they resolve correctly
Technical Details
Admin Pages 1
/admin/config/linked_field/config
Configuration page for managing available HTML attributes that can be set when linking fields. Attributes are defined in YAML format, each with an optional label and description. The configured attributes appear in field formatter settings under the Advanced section.
Permissions 1
Hooks 4
hook_field_formatter_third_party_settings_form
Adds the Linked Field configuration form to all field formatter settings. Creates the checkbox, destination type selection, field/custom destination inputs, and attribute configuration fields.
hook_field_formatter_settings_summary_alter
Adds a summary of Linked Field configuration to the field formatter summary displayed in Manage Display. Shows destination, configured attributes, and custom text if set.
hook_entity_display_build_alter
Core hook that processes entity display output to apply link wrapping. Iterates through fields, checks for Linked Field settings, resolves destinations, and wraps field output in anchor tags.
hook_help
Provides help text for the module on the help page.
Troubleshooting 6
Link fields are blacklisted because they already output anchor tags. The module prevents double-wrapping. Instead, use the Link field's native display options or link other fields to the Link field's value.
Only certain field types can serve as destinations: link, string, list_float, and list_string. Add a field of one of these types to your content type, or use Custom destination type with tokens.
Ensure the Token module is installed for the token browser. Check that you're using the correct token format with square brackets. Verify the entity type matches the token type (e.g., [node:url] for nodes, [user:url] for users).
For internal paths, prefix with 'internal:/' (e.g., 'internal:/about-us'). Relative paths like '/about-us' or 'about-us' are automatically converted, but explicit internal: prefix is recommended.
Check that attributes are defined in the module configuration at /admin/config/linked_field/config. Verify YAML syntax is valid. Empty attribute values are not rendered in the output.
Linked Field has a high module weight (100) to run after other modules. Check if another module or theme is overriding the field output after Linked Field processes it. Clear all caches after configuration changes.
Security Notes 5
- All output is sanitized through Xss::filterAdmin() to prevent XSS attacks
- HTML entities are properly encoded and decoded during DOM manipulation
- Token replacements use Drupal's secure token service
- URL validation prevents malformed or potentially dangerous URLs
- The module respects Drupal's permission system - only users with 'administer linked_field' can modify available attributes