Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Environment Indicator Plugins
This directory contains plugins for the **Environment Indicator** module, which provide custom implementations to display the current environment (e.g., Development, Staging, Production) in various parts of the Drupal UI.
## Overview
Environment Indicator plugins allow developers to extend or customize how and where the environment information is displayed. By implementing the `EnvironmentIndicatorInterface` and using the `EnvironmentIndicatorPluginBase` class, you can create new plugins to suit your needs.
## Default Plugins
The module includes the following default plugins:
- **`toolbar`**: Displays the environment indicator in the Drupal toolbar.
- **`page_top`**: Displays the environment indicator at the top of the page.
These plugins are defined in the `Plugin/EnvironmentIndicator` folder and can be enabled via the `enabled_plugins` configuration.
## Creating a New Plugin
Follow these steps to create a custom plugin:
### 1. Define the Plugin Class
Create a new PHP class in the `Plugin/EnvironmentIndicator` folder. The class should:
1. Use the `@EnvironmentIndicator` annotation to register the plugin.
2. Extend the `EnvironmentIndicatorPluginBase` class.
3. Optionally implement or override methods like `preprocessHtml()`, `pageTop()`, and `toolbar()`.
#### Example
```php
<?php
declare(strict_types=1);
namespace Drupal\environment_indicator\Plugin\EnvironmentIndicator;
use Drupal\environment_indicator\EnvironmentIndicatorPluginBase;
/**
* @EnvironmentIndicator(
* id = "custom_indicator",
* label = @Translation("Custom Indicator"),
* description = @Translation("Displays a custom environment indicator.")
* )
*/
final class CustomIndicator extends EnvironmentIndicatorPluginBase {
public function preprocessHtml(array &$variables): array {
$variables['attributes']['class'][] = 'custom-environment-indicator';
return $variables;
}
public function pageTop(array &$page_top): array {
$page_top['custom_indicator'] = [
'#type' => 'markup',
'#markup' => '<div class="custom-indicator">Custom Environment</div>',
];
return $page_top;
}
}
```
See the existing plugins located at `src/Plugin/EnvironmentIndicator` for practical examples.
### 2. Add to Configuration
Update the "Enabled plugins" (`enabled_plugins`) configuration to include your plugin "Custom Indicator" (`custom_indicator`), and disable others via the provided administration form located at /admin/config/development/environment-indicator.
## Plugin Methods
The `EnvironmentIndicatorPluginBase` class provides default implementations for the following methods. Override them as needed in your plugin:
- **`preprocessHtml(array &$variables): array`**: Modify HTML attributes or content.
- **`pageTop(array &$page_top): array`**: Add content to the top of the page.
- **`toolbar(): array`**: Add content to the Drupal toolbar.
## Plugin Registration
Plugins are discovered automatically via the `@EnvironmentIndicator` annotation. Make sure to:
1. Use a unique `id` for your plugin.
2. Add a meaningful `label` and `description`.