Extract switcher link logic from ToolbarHandler into new SwitcherManager service

Created on 26 May 2025, 18 days ago

Problem/Motivation

The ToolbarHandler service currently contains logic for building environment switcher links. This includes loading environment switcher entities, filtering based on status, formatting the links with inline styles and current path, and returning related cache tags.

However, this logic:

  • Is tightly coupled to the toolbar integration, which is being moved to an optional submodule (environment_indicator_toolbar).
  • Appears in multiple places, including hook_page_top() and the procedural _environment_indicator_switcher_links() function.
  • Prevents easier reuse for other UI integrations (e.g., page top, navigation).

Additionally, the module intends to support per-switcher permissions, but this is currently broken and tracked in another issue. Centralizing logic now will make that future enhancement easier to implement.

Steps to reproduce

Not applicable. This is not a user-facing bug. It is a refactor to improve internal architecture.

Proposed resolution

Introduce a new service: SwitcherManager in the Drupal\environment_indicator\Service namespace.

Responsibilities:

  • Load active EnvironmentIndicator config entities
  • Filter based on status
  • Format render-ready link arrays (title, URL, inline styles)
  • Return cache tags related to the switcher list

Then:

  • Replace usage of _environment_indicator_switcher_links() with the service
  • Update ToolbarHandler to delegate to the service (non-breaking)
  • Update hook_page_top() to use the service
  • Document the eventual deprecation path for ToolbarHandler in a separate issue

Remaining tasks

  • Create the SwitcherManager service
  • Move link generation logic into SwitcherManager::getLinks()
  • Move cache tag generation into SwitcherManager::getCacheTags()
  • Update hook_page_top() to use the new service
  • Update ToolbarHandler to call the service
  • Remove the _environment_indicator_switcher_links() function
  • Remove _environment_indicator_switcher_cache_tags() from .module
  • Add a class-level docblock documenting the scope and intent of the service

User interface changes

None.

API changes

New service: environment_indicator.switcher_manager

Data model changes

None.

✨ Feature request
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024