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
  • Config Split โ€” often used alongside to manage config per environment
  • Environment Indicator โ€” UI decoration for environments
  • Sites โ€” Supports environment based site settings

๐Ÿ’ฌ Feedback & Contributions

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

Short name
environment_context
Supporting organizations