Drupal is a registered trademark of Dries Buytaert

Leverage the Static Settings API to create Drupal contexts.

Description

The Static Setting Conditions module provides a way to utilize the Static Settings API to provide conditions in Drupal. It utilizes the API to create reusable contexts that can be applied throughout your Drupal site.

Requirements

- Drupal 10.x or 11.x
- PHP 8.1 or higher

Installation

1. Download the module:
You can download the module using Composer:

composer require drupal/static_setting_contexts

2. Enable the module:
After downloading, enable the module using Drush or the Drupal admin interface:

drush en static_setting_contexts

Or, enable it through the admin interface at /admin/modules.

Usage

Once the module is enabled, it will allow you to use any of the Static Settings you have defined as conditions.

Example

Create a custom Static Setting inside your module's src/Plugin/StaticSettings directory, and refer to the docs for Static Settings API
to understand how to use the settings. Below is an example setting.

<?php

declare(strict_types=1);

namespace Drupal\MYMODULE\Plugin\StaticSettings;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\static_setting_contexts\Attribute\StaticSettings;
use StaticSettings\BaseStaticSettingInterface;

/**
 * Enum class for custom static settings.
 */
#[StaticSettings(
  id: 'test_enum',
  label: new TranslatableMarkup('Test Enum'),
  description: new TranslatableMarkup('A test enum for the site.'),
)]
enum TestEnum: string implements BaseStaticSettingInterface {
  case OptionA = 'option_a';
  case OptionB = 'option_b';
  case OptionC = 'option_c';
}

Once you have created at least one setting condition forms (for example Block visibility) will display the available options as conditions.

Note

Due to the way that Drupal namespacing works, your settings will not be able to be validated by the Static Settings API unless you make
composer's autoloader aware of your settings. You can do this by adding the following to your composer.json file:

"autoload": {
  "psr-4": {
    "Drupal\\MYMODULE\\Plugin\\StaticSettings\\": "web/modules/custom/MYMODULE/src/Plugin/StaticSettings"
  }
}

Contributing

Contributions are welcome! If you would like to contribute to this module, please follow these steps:

1. Open an issue on the issue tracker
2. Create a Merge Request or add a patch
3. Mark your issue as Needs Review.

License

This module is licensed under the GPL-2.0+ license.

Maintainers

- Owen Bush

Activity

Total releases
3
First release
Mar 2025
Latest release
10 months ago
Release cadence
25 days
Stability
100% stable

Release Timeline

Releases

Version Type Release date
1.0.2 Stable Apr 25, 2025
1.0.1 Stable Apr 11, 2025
1.0.0 Stable Mar 6, 2025