Skip to content
Snippets Groups Projects
README.md 3.06 KiB
Newer Older
# 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;
  }
}
```
Chris Green's avatar
Chris Green committed

See the existing plugins located at `src/Plugin/EnvironmentIndicator` for practical examples.

Chris Green's avatar
Chris Green committed
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`.