Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
environment_indicator
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
environment_indicator
Merge requests
!100
Draft: Issue
#3526657
Create new SwitcherManager service and deprecate ToolbarHandler methods getLinks, getCachetags
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Closed
Draft: Issue
#3526657
Create new SwitcherManager service and deprecate ToolbarHandler methods getLinks, getCachetags
issue/environment_indicator-3526657:3526657-extract-switcher-link
into
4.x
Overview
1
Commits
5
Pipelines
3
Changes
4
Closed
Draft: Issue #3526657 Create new SwitcherManager service and deprecate ToolbarHandler methods getLinks, getCachetags
Chris Green
requested to merge
issue/environment_indicator-3526657:3526657-extract-switcher-link
into
4.x
2 months ago
Overview
1
Commits
5
Pipelines
3
Changes
4
Closes
#3526657
0
0
Merge request reports
Compare
4.x
version 2
d9d98cc4
2 months ago
version 1
6e798368
2 months ago
4.x (base)
and
latest version
latest version
a0a00f4c
5 commits,
2 months ago
version 2
d9d98cc4
2 commits,
2 months ago
version 1
6e798368
1 commit,
2 months ago
4 files
+
154
−
5
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
src/Service/SwitcherManager.php
0 → 100644
+
109
−
0
View file @ a0a00f4c
Edit in single-file editor
Open in Web IDE
<?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