Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Environment Context

11 sites No security coverage
View on drupal.org

This module allows Drupal sites to detect and react to the current runtime environment, such as development, staging, or production. It integrates with Drupal's systems for context, conditions, and caching, enabling features like varying block visibility or caching based on the detected environment. Extensible by design, it can be easily configured and extended to manage different site environments.

Environment Context provides a robust way to detect and act on the current runtime environment (e.g. development, staging, production) in Drupal. It integrates deeply with Drupal’s context, condition, and caching systems.

βœ… Features

  • Detects the current environment via:
    • settings.php
    • Environment variables (DRUPAL_ENVIRONMENT)
    • Pluggable event-based detection
  • Provides a Context Provider for environment
  • Adds a Condition Plugin to use environment in:
    • Block visibility
    • Layout Builder
    • Other context-aware systems
  • Defines an environment Typed Data plugin for strong typing
  • Adds an environment Cache Context to vary content by environment
  • Easily extendable: other modules can register environments or override detection

βš™οΈ How it works

The module dispatches events to determine the current environment and all known environments. This makes detection and registration extensible without hardcoding logic in one place.

It provides:

  • A context provider plugin: EnvironmentContext
  • A condition plugin: EnvironmentCondition
  • A typed data plugin: Environment
  • A cache context plugin: environment

You can use these in block visibility, service injections, or even configuration splits.

πŸ“¦ Installation

composer require drupal/environment_context
drush en environment_context

This module has no UI β€” it works behind the scenes and via developer APIs.

πŸš€ Example Usage

Get the current environment in a service:

$env = \Drupal::service('environment_context.context.environment')->getCurrentEnvironment();

React to specific environments:

if ($env === 'production') {
  // Do something only on production
}

Define environments via event listener:

$event->addEnvironment('production', ['label' => 'Production']);

Add an environment condition to a block:

  • Use the "Current environment" condition in block visibility settings
  • Select one or more matching environments

πŸ“Ž Requirements

  • Drupal 10 or 11
  • No external dependencies

🧩 Related Projects

πŸ’¬ Feedback & Contributions

Contributions, feature requests, and bug reports are welcome via the issue queue.

Activity

Total releases
2
First release
Jun 2025
Latest release
3 months ago
Releases (12 mo)
1
Maintenance
Active

Releases

Version Type Release date
1.0.0-rc1 Pre-release Apr 7, 2026
1.x-dev Dev Jun 10, 2025