Views Token Argument
Allows using tokens as contextual filters (arguments) in Drupal Views, enabling dynamic filtering based on current user or entity values.
views_argument_token
Install
composer require 'drupal/views_argument_token:^2.0'
Overview
Views Token Argument extends Drupal Views by providing a powerful argument default plugin that allows tokens to be used as contextual filter values. This enables Views to dynamically filter content based on token values from the current user or the entity related to the current page.
The module is particularly useful for creating personalized content displays. For example, you can show content related to categories that the current user has selected in their profile, or display content that shares taxonomy terms with the currently viewed node.
Key capabilities include processing both current user tokens and current entity tokens (such as the current node on node/x pages), extracting raw field values instead of formatted output, handling multiple values with AND/OR operators, and providing a debug mode to verify the final processed values.
Features
- Use tokens as Views contextual filter default values (e.g., [current-user:uid], [node:nid])
- Support for current user tokens to personalize Views based on the logged-in user
- Support for current entity tokens from the page context (node, user, taxonomy term, etc.)
- Option to extract raw field values instead of formatted/linked output provided by Token module
- Multiple value handling with configurable AND/OR operators for complex filtering
- Fallback to 'all' argument when no value is found, allowing Views to show all results
- Debug mode to display the final token-processed value for troubleshooting
- Automatic cache context handling for proper caching of personalized Views
Use Cases
Personalized Content Based on User Taxonomy Preferences
A user profile contains an entity reference field to taxonomy terms representing their interests. Create a View that displays content tagged with those same taxonomy terms by using [current-user:field_interests] as the contextual filter default value with the Token plugin. This creates a personalized feed showing only content matching the user's interests.
Related Content Block on Node Pages
Display a 'Related Articles' block showing other content in the same category as the currently viewed node. Configure a View with a contextual filter on the category taxonomy field, and use [node:field_category] as the token argument default. The View will automatically filter to show content sharing the same category.
User's Own Content Dashboard
Create a dashboard View showing all content authored by the current user. Use [current-user:uid] as the token for the author contextual filter. This allows users to see only their own content without any additional configuration.
Content by User's Organization
When users have an organization reference field on their profile, show content that belongs to the same organization. Use [current-user:field_organization] to filter a View's organization contextual filter, displaying only content relevant to the user's organization.
Multi-value Taxonomy Filtering
When a user has selected multiple taxonomy terms in their profile, display content matching any (OR) or all (AND) of those terms. Configure the multiple value handling option to control whether the View shows content matching any selected term or only content matching all selected terms.
Tips
- Always enable the 'Process raw value' option when filtering by entity reference fields, as the default token output is the linked title rather than the ID needed for filtering.
- Use the Debug mode during development to verify that tokens are resolving to the expected values before deploying to production.
- When using the 'Send all if empty' option, ensure you have configured the 'all' exception in the contextual filter's 'Exceptions' section.
- For Views with token arguments that depend on the current user, ensure Views caching is properly configured to avoid showing cached results from other users.
- Combine multiple token-based contextual filters to create highly personalized content displays based on various user or entity attributes.
Technical Details
Hooks 1
hook_help
Implements hook_help() to provide help text for the module on the admin help pages.
Troubleshooting 4
Ensure the Token module is installed and enabled. Verify that the token syntax is correct by using the token browser. Check that you are on a page where the token context is available (e.g., [node:*] tokens require being on a node page).
Enable Debug mode to see the actual value being used. If using entity reference fields, enable 'Process raw value' to get the entity ID instead of the formatted title. Verify the token is available in the current page context.
Check if the token is resolving to an empty value. If 'Send all if empty' is enabled, an empty token value will result in showing all content. Disable this option or ensure the token has a value.
The module adds cache contexts automatically, but verify your Views cache settings. For user-specific content, ensure the cache varies by user. Consider disabling Views result caching for highly personalized Views.