Skip to content
Snippets Groups Projects

Draft: Issue #3526657 Create new SwitcherManager service and deprecate ToolbarHandler methods getLinks, getCachetags

Closed Draft: Issue #3526657 Create new SwitcherManager service and deprecate ToolbarHandler methods getLinks, getCachetags

Files

+ 109
0
<?php
declare(strict_types=1);
namespace Drupal\environment_indicator\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Url;
/**
* Manages active environment switcher entities and builds UI-ready link data.
*
* This service centralizes logic related to the environment switcher list,
* including filtering active switchers and formatting their metadata as
* render-ready link arrays. It also provides cache tags to support proper
* cache invalidation when switcher configuration changes.
*
* Responsibilities:
* - Load and filter switchers based on status (and eventually permissions).
* - Build renderable links for UI components (e.g., page top, toolbar).
* - Provide cache tags related to switcher listings.
*
* Future enhancements may include:
* - Per-switcher access checks (once fixed in the module).
* - Domain/path/language-aware filtering.
* - Grouping, prioritization, or transformations.
*
* This service intentionally keeps responsibilities simple and centralized.
* If needed, future refactors may extract access filtering, link rendering,
* or entity loading into dedicated services or helpers.
*/
class SwitcherManager {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The current route match service.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected CurrentRouteMatch $routeMatch;
/**
* Constructs a new SwitcherManager instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\Routing\CurrentRouteMatch $routeMatch
* The current route match service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, CurrentRouteMatch $routeMatch) {
$this->entityTypeManager = $entityTypeManager;
$this->routeMatch = $routeMatch;
}
/**
* Builds an array of environment switcher links.
*
* @return array[]
* A render array of link definitions for each active environment.
*/
public function getLinks(): array {
/** @var \Drupal\environment_indicator\Entity\EnvironmentIndicator[] $entities */
$entities = $this->entityTypeManager->getStorage('environment_indicator')->loadMultiple();
$current_path = Url::fromRoute('<current>')->toString();
$links = [];
foreach ($entities as $entity) {
if (!$entity->status() || empty($entity->getUrl())) {
continue;
}
$links[] = [
'attributes' => [
'style' => sprintf('color: %s; background-color: %s;', $entity->getFgColor(), $entity->getBgColor()),
'title' => t('Opens the current page in the selected environment.'),
],
'title' => t('Open on @label', ['@label' => $entity->label()]),
'url' => Url::fromUri($entity->getUrl() . $current_path),
'type' => 'link',
'weight' => $entity->getWeight(),
];
}
uasort($links, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
return $links;
}
/**
* Returns cache tags related to the switcher list.
*
* @return string[]
* An array of cache tags.
*/
public function getCacheTags(): array {
return $this->entityTypeManager
->getDefinition('environment_indicator')
->getListCacheTags();
}
}
Loading