Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Environment Context

10 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.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Jun 2025
Latest release
2 weeks ago
Releases (12 mo)
2 ▲ from 1
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
1.0.0-rc2 Pre-release 10–11 Aug 17, 2026
1.0.0-rc1 Pre-release 10–11 Apr 7, 2026
1.x-dev Dev 10–11 Jun 10, 2025