Views Base URL
Provides Views field plugins that generate absolute internal URLs based on custom parameters such as internal path, query string, and fragment.
views_base_url
Install
composer require 'drupal/views_base_url:^2.1'
Overview
Originally named Views Base URL, this module provides Views fields that generate absolute internal URLs based on custom parameters. The generated URLs can include an internal path, query string values, and URL fragments.
The module offers two Views field plugins: Global: Custom Internal URL (recommended) and Global: Base URL (deprecated). Both fields can output either a plain URL string or render as a complete HTML link with customizable attributes including class, title, target, and rel.
All field settings support token replacement patterns, allowing dynamic values from other fields in the view (such as node ID, title, etc.) to be used in URL construction. This is particularly useful for creating dynamic links based on row-specific data.
The generated URLs automatically respect the current site language, ensuring multilingual sites produce links in the correct language context. Path aliases are also automatically resolved when generating URLs.
Features
- Generates absolute internal URLs from Views with customizable path, query parameters, and URL fragment
- Supports token replacement patterns for dynamic URL generation using other field values from the view
- Renders URLs as plain text or complete HTML links with customizable attributes (class, title, rel, target)
- Automatically respects current site language for multilingual support
- Resolves path aliases automatically when generating URLs
- Provides two Views field plugins: Custom Internal URL (recommended) and Base URL (deprecated)
Use Cases
Creating dynamic 'Read more' links in Views
When building a content listing view, add the Global: Custom Internal URL field with the URL path set to '/node/{{ nid }}' and link text set to 'Read more'. This creates a proper absolute link to each content item that works correctly in newsletters, RSS feeds, or when the view is displayed outside the normal site context.
Adding query parameters to listing links
For a product catalog view where you want to track the source of clicks, configure the URL query field with 'source=catalog' or use tokens like 'ref={{ nid }}'. This appends query parameters to each link for analytics or referral tracking.
Creating anchor links within pages
When building a view that links to specific sections of a page, set the URL path to the page path and the URL fragment to the section ID. For example, path '/faq' with fragment 'question-{{ nid }}' creates links like '/faq#question-42'.
Building image links with custom attributes
Add an image field to your view, exclude it from display, then add the Custom Internal URL field with 'Render as link' enabled. In the link text field, use the image field token. Configure link attributes like class, title, rel='noopener', and target='_blank' for proper image link behavior.
Email newsletter compatible links
When embedding Views output in email newsletters, the absolute URLs generated by this module ensure links work correctly regardless of the email client, unlike relative URLs which may break when viewed outside the website context.
Creating links with multiple CSS classes
In the link_class field, enter space-separated class names like 'btn btn-primary btn-lg' to apply multiple CSS classes to the generated link for styling purposes.
Tips
- Use the Custom Internal URL field instead of Base URL - it's the recommended approach and will be better maintained
- Always add fields you want to use as tokens BEFORE the URL field in the Views field order
- The URL path field accepts any valid Drupal internal path starting with /, such as /node/123, /admin/content, or /user/{{ uid }}
- When using 'Render as link', leaving link_text empty will display the full URL as the link text
- Multiple CSS classes can be added to the link_class field separated by spaces
- Token patterns use Twig syntax: {{ field_name }} where field_name matches the field's machine name in the view
Technical Details
Hooks 1
hook_views_data_alter
Implements hook_views_data_alter() to register the custom Views field plugins. Adds 'base_url' and 'custom_internal_url' fields to the global 'views' table, making them available as Global fields in Views UI.
Troubleshooting 5
Ensure the field containing the token value (e.g., nid, title) is added to the view and appears BEFORE the Custom Internal URL field in the field order. Fields are processed in order, and tokens from later fields are not available to earlier fields.
The module automatically resolves path aliases. If aliases are not appearing, verify that path aliases exist for the content and that the Path Alias module is enabled. Clear the path alias cache if recently added aliases are not appearing.
Ensure your site has language detection and negotiation configured correctly. The module uses the current language context, so if the request is not properly language-prefixed, the generated URLs may not include the expected language prefix.
Check the format of the url_query field. Parameters should be in 'key=value' format, with multiple parameters separated by spaces or & characters. Do not include the leading '?' character.
The Base URL field is deprecated but still functional. For new implementations, use the Custom Internal URL field instead, which offers the same functionality with a cleaner configuration interface.