Views Default Argument: Entity Field Values

A Views argument_default plugin that automatically extracts field values from the current entity being viewed and uses them as default contextual filter arguments.

views_arg_entity_field
605 sites
29
drupal.org

Install

Drupal 10, 9, 8 v2.0.5
composer require 'drupal/views_arg_entity_field:^2.0'

Overview

Views Default Argument: Entity Field Values provides a Views contextual filter (argument) default plugin that dynamically pulls field values from the 'current' entity. The current entity is determined automatically from the route parameters, making it seamless to use on entity pages.

When you place a View block or embed a View on an entity page (such as a node page), this plugin can extract any field value from that entity and use it as the default argument for the View's contextual filter. This enables powerful dynamic filtering without requiring custom code or complex configuration.

The plugin supports all content entity types, including nodes, users, taxonomy terms, and custom entities. It can extract values from any field property, handle multi-value fields with configurable concatenation or single-value selection, and provides fallback values for empty fields.

Features

  • Automatically detects the current entity from route parameters for any content entity type (nodes, users, taxonomy terms, custom entities, etc.)
  • Supports extraction of any field property, not just main values (e.g., extract 'title' or 'uri' from link fields)
  • Configurable multi-value field handling: concatenate all values with OR (+) or AND (,) separator, or select a specific value by delta index
  • Fallback value support for empty fields, allowing graceful handling when the source field has no value
  • Proper cache invalidation through entity cache tags and URL cache context
  • Dynamic form UI with conditional visibility based on entity type selection
  • Works with any field type including entity references, text fields, integer fields, link fields, and more

Use Cases

Related Content Block Based on Taxonomy

Display a block of related content on node pages. Create a View with a taxonomy term ID contextual filter, then use this plugin to automatically pull the term ID(s) from the current node's taxonomy reference field. All nodes sharing the same terms will be displayed.

Author's Other Content

Show other content by the same author on node pages. Configure a contextual filter on author user ID and use this plugin to extract the 'uid' field value from the current node. No custom code needed.

Entity Reference Cross-Linking

On a 'Project' node page, display all related 'Task' nodes. If Tasks have an entity reference field pointing to Projects, create a View of Tasks with a Project ID contextual filter, and use this plugin to pass the current Project's node ID.

Multi-Value Field Filtering

Display content that shares any tag with the current content. Extract all taxonomy term IDs from a multi-value field using concatenation with '+' separator, enabling OR filtering across all values.

Link Field Property Extraction

Extract specific properties from complex fields like Link fields. For example, extract just the 'title' property or the 'uri' property to use as filter criteria for finding other content with matching link titles or URLs.

Fallback for Empty Fields

Configure a safe fallback when the source field might be empty. Set an empty_value that matches an exception value in Views to gracefully skip the contextual filter rather than showing no results or an error.

Tips

  • When using multi-value fields with concatenation, make sure to enable 'Allow multiple values' in the contextual filter's 'More' section
  • Use the empty_value setting with a corresponding exception value in Views to gracefully handle empty fields
  • The plugin works with any content entity type in your Drupal installation, including custom entities
  • Field property names follow Drupal's field API conventions - use ':target_id' for entity references, ':value' for simple fields, ':uri' or ':title' for link fields
  • For debugging, check that your entity is properly available in the route parameters - the entity type machine name must match a route parameter

Technical Details

Hooks 1
hook_help

Provides help text on the module's help page

Troubleshooting 4
The View shows no results or an error when the source field is empty

Configure the 'empty_value' setting to return a fallback value, and set up a corresponding exception value in the contextual filter's 'Exceptions' section to skip the filter when empty.

Only the first value from a multi-value field is being used

Make sure 'Multiple Values Handling' is set to 'Concatenate values' and that 'Allow multiple values' is enabled in the contextual filter's 'More' section.

The field I need is not appearing in the field selection dropdown

Ensure the field is configured on the selected entity type. The plugin only shows fields that exist on the entity type's field storage definitions.

The entity is not being detected from the current page

The plugin uses route parameters to detect the current entity. The entity must be a direct route parameter (like on canonical entity pages). Embedded entities in other contexts may not be detected.