Viewfield

Provides a field type that holds a reference to a Views display and renders it whenever the entity containing the field is displayed.

viewfield
24,840 sites
79
drupal.org

Overview

The Viewfield module defines an entity reference field type that allows administrators and content authors to embed Views displays directly into any fieldable entity, such as nodes, users, taxonomy terms, or paragraphs. This enables powerful content composition by integrating dynamic Views output within entity displays.

A key feature is the "Always use default value" setting, which automatically applies the same view configuration to all entities in a bundle without requiring individual field value assignment. The module supports token replacement in view arguments, allowing contextual data from the parent entity to be passed to the embedded view. Content authors can override the number of items displayed, and multiple view displays can be stored in a single field through unlimited cardinality.

Viewfield provides comprehensive theming support with customizable templates and theme suggestions, making it easy to style the output. The module also includes a migration plugin for seamless upgrades from Drupal 7 Viewfield installations.

Features

  • Field type for embedding Views displays within any fieldable entity (nodes, users, taxonomy terms, paragraphs, etc.)
  • Dynamic display selection with AJAX-powered widget that updates display options when the view selection changes
  • Token-based arguments support allowing contextual data from the parent entity to be passed to the view's contextual filters
  • Items to display override option to limit view results and disable pagers
  • Always use default value setting to automatically apply the same view to all entities in a bundle
  • Allowed views restriction to limit which views content authors can select
  • Allowed display types restriction to control which view display plugins are available (e.g., block, page, attachment)
  • Three formatters: Default (renders view), Title and display name (shows view metadata), and Rendered entities (renders individual entities from view results)
  • View title display options with Above, Inline, Hidden, and Visually Hidden positioning
  • Always build output option to render field even when view produces no results
  • Contextual link integration for quick access to edit the referenced view
  • Comprehensive Twig template system with extensive theme hook suggestions
  • HAL normalizer integration for REST API serialization when HAL module is enabled
  • Drupal 7 migration support for upgrading existing Viewfield data

Use Cases

Related Content Block on Nodes

Add a Viewfield to an Article content type configured to display a view of related articles filtered by shared taxonomy term. Use token-based arguments like [node:field_category:target_id] to pass the current node's category to the view's contextual filter.

User Profile Activity Stream

Attach a Viewfield to user entities to display a view of the user's recent content, comments, or activity. Use the [user:uid] token as an argument to filter results to the profile owner.

Paragraph-based Layout Builder Alternative

Create a Viewfield on Paragraph entities to allow content editors to embed curated content lists within long-form pages. The paragraph can combine static text with dynamic view output.

Consistent Site-wide Featured Content

Enable "Always use default value" on a Viewfield attached to all Article nodes to automatically display a featured content view on every article page without per-node configuration.

Product Catalog with Filtered Results

Add a Viewfield to a Category taxonomy term to display products in that category. Pass the term ID as an argument to show only relevant products when viewing the term page.

Tips

  • Use the "Items to display" advanced option to limit results without modifying the view itself. This automatically disables the pager.
  • Token-based arguments support complex expressions like [node:field_ref:entity:field_category:target_id] for traversing entity references.
  • The Rendered entities formatter is useful when you want more control over individual entity rendering but only works with single-value viewfields.
  • Create dedicated views specifically for viewfield embedding rather than reusing existing views. Use the block display type as it has fewer dependencies.
  • Combine with Paragraphs module to create flexible page layouts where editors can add multiple view-powered content sections.

Technical Details

Hooks 2
hook_help

Provides comprehensive help page documentation for the Viewfield module accessible at admin/help/viewfield

hook_theme

Registers viewfield and viewfield_item theme hooks with their render elements

Troubleshooting 5
Display dropdown is empty after selecting a view

Ensure the selected view has enabled displays of the allowed display types. Check the Allowed display types field setting - if 'block' is the only allowed type, only block displays will appear. Leave all unchecked to allow all display types.

View arguments/tokens are not being replaced

Verify the token syntax matches the entity type containing the viewfield. For taxonomy terms, use [term:*] tokens. Ensure Token module is installed for the token browser to appear.

Field doesn't appear on entity edit form

Check if "Always use default value" is enabled in field settings. When enabled, the field is hidden on edit forms and uses only the configured default value.

HAL serialization missing viewfield properties

The HAL normalizer is only registered when HAL module is enabled. Ensure HAL module is installed and clear caches for the service to be registered.

Migration from Drupal 7 not working

Ensure migrate_drupal module is enabled. The viewfield migration plugin maps D7's vname (view|display format) and vargs fields automatically. Verify source viewfield data format.

Security Notes 2
  • The Rendered entities formatter does not perform additional access checking beyond what the View itself enforces. Ensure view-level access restrictions are properly configured.
  • When using token-based arguments, be aware that user-controllable entity field values could potentially be used to manipulate view filtering if not properly sanitized by the view.