Drupal is a registered trademark of Dries Buytaert
Release: Modeler API 1.1.5 Minor update available for module modeler_api (1.1.5). Release: Admin Audit Trail 1.0.12 Minor update available for module admin_audit_trail (1.0.12). Release: Workflow Modeler 1.0.7 Minor update available for module modeler (1.0.7). Release: Image Scale and Fill Background Effect 1.1.0 Minor update available for module image_scale_fill (1.1.0). Release: Captcha Keypad 2.0.0 Major update available for module captcha_keypad (2.0.0). Release: CMRF Form Processor 2.2.20 Minor update available for module cmrf_form_processor (2.2.20). Release: CMRF Core 2.1.17 Minor update available for module cmrf_core (2.1.17). Release: Yet another statistics module 2.3.1 Minor update available for module yasm (2.3.1). Usage Milestone: Role Theme Switcher Module role_theme_switcher crossed 1,000 active installs. Module Revived: Simple XML sitemap DiWoo 1.0.6 Module simple_sitemap_diwoo updated after 9 months of inactivity (1.0.6).

This module provides a runtime context stack to track entities during rendering, especially useful in complex nested structures. It allows custom code, like field formatters, to easily access parent entity information and manage cacheability metadata, aiding in tasks such as parent-aware formatting and dynamic component building.

The Entity Build Context module provides a lightweight runtime context stack that tracks entities as they are being rendered.

In complex Drupal architectures—especially those relying heavily on nested entities like Paragraphs, Layout Builder, Blocks, or Media—it can be difficult for a deeply nested child entity or custom field formatter to know what its parent container is. This module solves that problem by tracking the current entity view context and exposing it via a central service.

How It Works

For bundles where Entity Build Context is enabled, the module tracks the rendering lifecycle. Each item in the context stack captures:

  • The active Entity object
  • The Entity View Display (config entity)
  • The active View Mode (e.g., 'default', 'teaser')
  • The Render Build Array

The entity_build_context_manager service allows your custom code to read this data at any point during the render cycle, ensuring safe application of cacheability metadata.

Typical Use Cases

  • Parent-Aware Formatting: Access the parent node's data from inside a nested Paragraph or Media item custom field formatter.
  • Inherited Cacheability: Automatically inherit and bubble up cache tags/contexts from a parent entity to nested render arrays.
  • Dynamic Components: Build decoupled or component-driven layouts that need to adapt based on the context of the page or host entity they are rendered within.
  • Debugging: Easily inspect complex, deeply nested entity rendering behavior.

Configuration

  1. Install and enable the Entity Build Context module.
  2. Navigate to the configuration page of any supported bundle (e.g., Structure > Content types > Manage [Type]).
  3. Check the box for Enable entity build context.
  4. Save the bundle.

Developer Usage

The Service

The module provides the entity_build_context_manager service to interact with the runtime stack.

Example: Fetching Context in a Custom Formatter or Preprocess

// Retrieve the service.
$context_manager = \Drupal::service('entity_build_context_manager');

// Get the immediate parent entity context.
if ($parent_context = $context_manager->getParentContext()) {
  $parent_entity = $parent_context->getEntity();
  $view_mode = $parent_context->getViewMode();
  
  // Do something custom based on the parent context...

  // Safely apply cache metadata from the entire build stack to a build array.
  $context_manager->applyCacheMetadata($my_custom_render_array);
}

Technical Specifications & Requirements

  • Drupal Compatibility: Drupal 10.3+ or Drupal 11+
  • Dependencies: None (Zero contrib dependencies)

Activity

Tracked releases
1
Tracked since
May 2026
Latest release
2 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Release date
1.0.0-beta1 Pre-release May 26, 2026