Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

environment_context

1 sites No security coverage
View on drupal.org

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
1 week ago
Release cadence
301 days
Stability
0% stable

Releases

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