Change record status: 
Introduced in branch: 
4.x
Introduced in version: 
4.0.22
Description: 

Change description

The logic responsible for determining the active environment title, switcher links and version identifier (used for display in the toolbar and page top) has been moved from \Drupal\environment_indicator\ToolbarHandler into a new service:

environment_indicator.indicator
\Drupal\environment_indicator\Service\EnvironmentIndicator

The following methods have been deprecated and replaced by methods in the environment_indicator.indicator service:

  • ToolbarHandler::getTitle() → EnvironmentIndicator::getTitle()
  • ToolbarHandler::getCurrentRelease() → EnvironmentIndicator::getCurrentRelease()
  • ToolbarHandler::getVersionIdentifier() → (logic now internal toEnvironmentIndicator )
  • ToolbarHandler::getLinks() → EnvironmentIndicator::getLinks()
  • ToolbarHandler::getCacheTags() → EnvironmentIndicator::getCacheTags()

Before:

$title = $toolbarHandler->getTitle();

After:

$title = \Drupal::service('environment_indicator.indicator')->getTitle();

Before:

$release = $toolbarHandler->getCurrentRelease();

After:

$release = \Drupal::service('environment_indicator.indicator')->getCurrentRelease();

Before:

$switcherLinks = $toolbarHandler->getLinks();

After:

$switcherLinks = \Drupal::service('environment_indicator.indicator')->getLinks();

Before:

$cacheTags = $toolbarHandler->getCacheTags();

After:

$cacheTags = \Drupal::service('environment_indicator.indicator')->getCacheTags();

Instead of calling the service via \Drupal::service(), you are encouraged to inject it for improved testability and adherence to Drupal best practices:

use Drupal\environment_indicator\Service\EnvironmentIndicator;

public function __construct(EnvironmentIndicator $environment_indicator) {
  $this->environmentIndicator = $environment_indicator;
}

...

$release = $this->environmentIndicator->getCurrentRelease();
Access control methods (no direct replacement)

The following methods are deprecated but do not currently have a direct replacement in the Indicator service. If you need similar functionality, you may need to implement it manually using \Drupal::currentUser() or wait for a future replacement.

Deprecated:

  • ToolbarHandler::hasAccessAll()
  • ToolbarHandler::hasAccessEnvironment()
  • ToolbarHandler::hasAccessActiveEnvironment()

Example workaround for hasAccessEnvironment():

$has_access = \Drupal::currentUser()->hasPermission('access environment indicator development')
  || \Drupal::currentUser()->hasPermission('access environment indicator');

Example workaround for hasAccessActiveEnvironment():

$env = \Drupal::config('environment_indicator.indicator');
$has_access = \Drupal::currentUser()->hasPermission('access environment indicator ' . $env->get('machine'))
  || \Drupal::currentUser()->hasPermission('access environment indicator');

These methods will be removed in environment_indicator:5.0.0. If you have feedback or a use case for an access control API replacement, please create a new issue in the environment indicator issue queue..

Deprecation schedule

  • Deprecated in: environment_indicator:4.x
  • Removal target: environment_indicator:5.0.0
Impacts: 
Module developers
Site templates, recipes and distribution developers