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