From aecc698548556035066a5a5e9f1f8fb91feee372 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 30 Oct 2024 17:07:28 -0700 Subject: [PATCH 01/56] Close #3484735 Move toolbar integration into separate module --- css/environment_indicator.css | 42 ------- environment_indicator.install | 24 ++++ environment_indicator.module | 107 ++++++++++-------- js/environment_indicator.js | 29 ----- .../css/toolbar.css | 46 ++++++++ .../environment_indicator_toolbar.info.yml | 9 ++ ...nvironment_indicator_toolbar.libraries.yml | 10 ++ .../environment_indicator_toolbar.module | 51 +++++++++ ...environment_indicator_toolbar.services.yml | 4 +- .../images}/env-bebebe.png | Bin .../images}/env-bebebe.svg | 0 .../images}/env-ffffff.png | Bin .../images}/env-ffffff.svg | 0 .../js/toolbar.js | 34 ++++++ .../src/Service}/ToolbarHandler.php | 55 ++++----- src/Entity/EnvironmentIndicator.php | 29 +++++ 16 files changed, 284 insertions(+), 156 deletions(-) create mode 100644 environment_indicator.install create mode 100644 modules/environment_indicator_toolbar/css/toolbar.css create mode 100644 modules/environment_indicator_toolbar/environment_indicator_toolbar.info.yml create mode 100644 modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml create mode 100644 modules/environment_indicator_toolbar/environment_indicator_toolbar.module rename environment_indicator.services.yml => modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml (58%) rename {images => modules/environment_indicator_toolbar/images}/env-bebebe.png (100%) rename {images => modules/environment_indicator_toolbar/images}/env-bebebe.svg (100%) rename {images => modules/environment_indicator_toolbar/images}/env-ffffff.png (100%) rename {images => modules/environment_indicator_toolbar/images}/env-ffffff.svg (100%) create mode 100644 modules/environment_indicator_toolbar/js/toolbar.js rename {src => modules/environment_indicator_toolbar/src/Service}/ToolbarHandler.php (86%) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index d3a3c7d3..f3a9c32c 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -54,45 +54,3 @@ border-radius: 5px; background-color: #EDEDE0; } - -.toolbar-bar .toolbar-icon-environment:before { - background-image: url("../images/env-bebebe.svg"); -} -.no-svg .toolbar-bar .toolbar-icon-environment:before { - background-image: url("../images/env-bebebe.png"); -} -.toolbar-bar .toolbar-icon-environment.is-active:before { - background-image: url("../images/env-ffffff.svg"); -} -.no-svg .toolbar-bar .toolbar-icon-environment.is-active:before { - background-image: url("../images/env-ffffff.png"); -} -.toolbar .toolbar-tray-vertical .edit-environments { - text-align: right; - padding: 1em; -} -.toolbar .toolbar-tray-horizontal .edit-environments { - float: right; -} - -/* Style fixes for gin_toolbar */ -.gin--vertical-toolbar .toolbar-menu-administration { - border-left: var(--enviroment-indicator-border-width) solid; -} - -.gin--horizontal-toolbar #toolbar-item-administration-tray { - border-top: var(--enviroment-indicator-border-width) solid; - border-bottom: 0; -} - -.gin--horizontal-toolbar .gin-secondary-toolbar { - margin-top: var(--enviroment-indicator-border-width); -} - -[dir="ltr"] .gin--vertical-toolbar .toolbar-menu-administration > .toolbar-menu > .menu-item .toolbar-menu { - margin-left: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); -} - -[dir="rtl"] .gin--vertical-toolbar .toolbar-menu-administration > .toolbar-menu > .menu-item .toolbar-menu { - margin-right: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); -} diff --git a/environment_indicator.install b/environment_indicator.install new file mode 100644 index 00000000..9a2514d1 --- /dev/null +++ b/environment_indicator.install @@ -0,0 +1,24 @@ +<?php + +/** + * @file + * Install, update and uninstall functions for the Environment Indicator module. + */ + + +/** + * Ensure environment_indicator_toolbar module is installed. + */ +function environment_indicator_update_110401999() { + // Check if toolbar integration is enabled. + $config = \Drupal::config('environment_indicator.settings'); + if ($config->get('toolbar_integration')['toolbar'] === '0') { + return; + } + // List of dependencies. + $modules = [ + 'environment_indicator_toolbar', + 'toolbar', + ]; + \Drupal::service('module_installer')->install($modules); +} diff --git a/environment_indicator.module b/environment_indicator.module index 03fb99ac..48f791cc 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -8,7 +8,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; -use Drupal\environment_indicator\ToolbarHandler; +use Drupal\environment_indicator\Entity\EnvironmentIndicator; /** * Implements hook_help(). @@ -54,11 +54,62 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc return NULL; } +/** + * Get all environment links. + * + * @return array + * An array of links for each environment. + */ +function _environment_indicator_switcher_links() { + /** @var \Drupal\environment_indicator\Entity\EnvironmentIndicator[] $environment_switcher_entities */ + if (!$environment_switcher_entities = EnvironmentIndicator::loadMultiple()) { + return []; + } + + $current = Url::fromRoute('<current>'); + $current_path = $current->toString(); + $environment_switcher_entities = array_filter( + $environment_switcher_entities, + function (EnvironmentIndicator $entity) { + return $entity->status() + && !empty($entity->getUrl()); + // && \Drupal::service('environment_indicator_toolbar.toolbar_handler')->hasAccessEnvironment($entity->id()); + } + ); + + $links = array_map( + function (EnvironmentIndicator $entity) use ($current_path) { + return [ + 'attributes' => [ + 'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $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(), + ]; + }, + $environment_switcher_entities + ); + + if (!$links) { + return []; + } + + uasort($links, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); + + return $links; +} + /** * Implements hook_preprocess_HOOK() for page templates. */ function environment_indicator_preprocess_html(&$variables) { - if (_environment_indicator_external_integration_is_enabled('toolbar')) { + // Check if environment_indicator_toolbar is enabled. + $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; + $toolbar_integration_setting_enabled = \Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar'] ?? FALSE; + if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; } $active_environment = \Drupal::config('environment_indicator.indicator'); @@ -74,7 +125,10 @@ function environment_indicator_preprocess_html(&$variables) { * Implements hook_page_top(). */ function environment_indicator_page_top(array &$page_top) { - if (_environment_indicator_external_integration_is_enabled('toolbar')) { + // Check if environment_indicator_toolbar is enabled. + $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; + $toolbar_integration_setting_enabled = \Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar'] ?? FALSE; + if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; } $active_environment = \Drupal::config('environment_indicator.indicator'); @@ -118,9 +172,10 @@ function environment_indicator_page_top(array &$page_top) { 'style' => 'cursor: pointer', 'title' => t('Show the environment switcher.'), ]; + $environment_indicator_switcher_cache_tags = \Drupal::entityTypeManager()->getDefinition('environment_indicator')->getListCacheTags(); $page_top['indicator'] += [ '#cache' => [ - 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], _environment_indicator_switcher_cache_tags()), + 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], $environment_indicator_switcher_cache_tags), ], ]; } @@ -194,50 +249,6 @@ function _environment_indicator_parse_style(string $style): array { return $structured_styles; } -/** - * Implements hook_toolbar(). - */ -function environment_indicator_toolbar() { - return \Drupal::service('environment_indicator.toolbar_handler') - ->toolbar(); -} - -/** - * Helper function that generates the environment switcher links. - * - * @return array - * A renderable array with the links. - */ -function _environment_indicator_switcher_links(): array { - return \Drupal::service('environment_indicator.toolbar_handler') - ->getLinks(); -} - -/** - * Helper function that checks if there is external integration. - * - * @param string $integration - * Name of the integration: toolbar, admin_menu, ... - * - * @return bool - * TRUE if integration is enabled. FALSE otherwise. - */ -function _environment_indicator_external_integration_is_enabled(string $integration): bool { - return \Drupal::service('environment_indicator.toolbar_handler') - ->externalIntegration($integration); -} - -/** - * Get the cache tags for the environment indicator switcher. - * - * @return string[] - * The cache tags. - */ -function _environment_indicator_switcher_cache_tags(): array { - return \Drupal::service('environment_indicator.toolbar_handler') - ->getCacheTags(); -} - /** * Loads an environment indicator in a procedural way. * diff --git a/js/environment_indicator.js b/js/environment_indicator.js index cfd7c585..c0c15d80 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -10,35 +10,6 @@ } }; - Drupal.behaviors.environmentIndicatorToolbar = { - attach: function (context, settings) { - if (typeof(settings.environmentIndicator) != 'undefined') { - const $body = $('body'); - const borderWidth = getComputedStyle(document.body).getPropertyValue('--enviroment-indicator-border-width') || '6px'; - - // Only apply text and background color if not using gin_toolbar - if (!$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar')) { - $('#toolbar-bar', context).css('background-color', settings.environmentIndicator.bgColor); - $('#toolbar-bar .toolbar-item a', context).not('.is-active').css('color', settings.environmentIndicator.fgColor); - } - - // Set environment color for gin_toolbar vertical toolbar. - if ($body.hasClass('gin--vertical-toolbar')) { - $('.toolbar-menu-administration', context).css({'border-left-color': settings.environmentIndicator.bgColor, 'border-left-width': borderWidth}); - $('.toolbar-tray-horizontal .toolbar-menu li.menu-item', context).css({'margin-left': 'calc(var(--enviroment-indicator-border-width) * -0.5)'}); - } - // Set environment color for gin_toolbar horizontal toolbar. - if ($body.hasClass('gin--horizontal-toolbar')) { - $('#toolbar-item-administration-tray').css({'border-top-color': settings.environmentIndicator.bgColor, 'border-top-width': borderWidth}); - } - // Set environment color on the icon of the gin_toolbar - if($body.hasClass('gin--horizontal-toolbar') || $body.hasClass('gin--vertical-toolbar')) { - $('head', context).append("<style>.toolbar .toolbar-bar #toolbar-item-administration-tray a.toolbar-icon-admin-toolbar-tools-help.toolbar-icon-default::before{ background-color: " + settings.environmentIndicator.bgColor + " }</style>"); - } - } - } - }; - Drupal.behaviors.environmentIndicatorTinycon = { attach: function (context, settings) { $(once('env-ind-tinycon', 'html', context)).each(function() { diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css new file mode 100644 index 00000000..fb4c009e --- /dev/null +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -0,0 +1,46 @@ +.toolbar-bar .toolbar-icon-environment:before { + background-image: url("../images/env-bebebe.svg"); +} + +.no-svg .toolbar-bar .toolbar-icon-environment:before { + background-image: url("../images/env-bebebe.png"); +} + +.toolbar-bar .toolbar-icon-environment.is-active:before { + background-image: url("../images/env-ffffff.svg"); +} + +.no-svg .toolbar-bar .toolbar-icon-environment.is-active:before { + background-image: url("../images/env-ffffff.png"); +} + +.toolbar .toolbar-tray-vertical .edit-environments { + text-align: right; + padding: 1em; +} + +.toolbar .toolbar-tray-horizontal .edit-environments { + float: right; +} + +/* Style fixes for gin_toolbar */ +.gin--vertical-toolbar .toolbar-menu-administration { + border-left: var(--enviroment-indicator-border-width) solid; +} + +.gin--horizontal-toolbar #toolbar-item-administration-tray { + border-top: var(--enviroment-indicator-border-width) solid; + border-bottom: 0; +} + +.gin--horizontal-toolbar .gin-secondary-toolbar { + margin-top: var(--enviroment-indicator-border-width); +} + +[dir="ltr"] .gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { + margin-left: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); +} + +[dir="rtl"] .gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { + margin-right: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); +} \ No newline at end of file diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.info.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.info.yml new file mode 100644 index 00000000..0556d481 --- /dev/null +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.info.yml @@ -0,0 +1,9 @@ +name: 'Environment Indicator - Toolbar Integration' +type: module +description: 'Changes the toolbar background and text color depending on environment.' +package: Development +core_version_requirement: '^9.3 || ^10 || ^11' +dependencies: + - drupal:toolbar + - environment_indicator:environment_indicator +configure: environment_indicator_ui.settings diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml new file mode 100644 index 00000000..6a3161de --- /dev/null +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml @@ -0,0 +1,10 @@ +toolbar: + js: + js/toolbar.js: {} + css: + component: + css/toolbar.css: {} + dependencies: + - core/drupal + - core/jquery + - core/drupalSettings \ No newline at end of file diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module new file mode 100644 index 00000000..25876733 --- /dev/null +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module @@ -0,0 +1,51 @@ +<?php + +/** + * @file + * Module implementation file. + */ + +/** + * Implements hook_toolbar(). + */ +function environment_indicator_toolbar_toolbar() { + return \Drupal::service('environment_indicator_toolbar.toolbar_handler') + ->toolbar(); +} + +/** + * Helper function that generates the environment switcher links. + * + * @return array + * A renderable array with the links. + */ +function _environment_indicator_toolbar_switcher_links(): array { + return \Drupal::service('environment_indicator_toolbar.toolbar_handler') + ->getLinks(); +} + +/** + * Helper function that checks if there is external integration. + * + * @param string $integration + * Name of the integration: toolbar, admin_menu, ... + * + * @return bool + * TRUE if integration is enabled. FALSE otherwise. + */ +function _environment_indicator_toolbar_external_integration_is_enabled(string $integration): bool { + return \Drupal::service('environment_indicator_toolbar.toolbar_handler') + ->externalIntegration($integration); +} + +/** + * Get the cache tags for the environment indicator switcher. + * + * @return string[] + * The cache tags. + */ +function _environment_indicator_toolbar_switcher_cache_tags(): array { + return \Drupal::service('environment_indicator_toolbar.toolbar_handler') + ->getCacheTags(); +} + diff --git a/environment_indicator.services.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml similarity index 58% rename from environment_indicator.services.yml rename to modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml index 6115b450..292f16ba 100644 --- a/environment_indicator.services.yml +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml @@ -1,6 +1,6 @@ services: - environment_indicator.toolbar_handler: - class: Drupal\environment_indicator\ToolbarHandler + environment_indicator_toolbar.toolbar_handler: + class: Drupal\environment_indicator_toolbar\Service\ToolbarHandler arguments: - '@module_handler' - '@config.factory' diff --git a/images/env-bebebe.png b/modules/environment_indicator_toolbar/images/env-bebebe.png similarity index 100% rename from images/env-bebebe.png rename to modules/environment_indicator_toolbar/images/env-bebebe.png diff --git a/images/env-bebebe.svg b/modules/environment_indicator_toolbar/images/env-bebebe.svg similarity index 100% rename from images/env-bebebe.svg rename to modules/environment_indicator_toolbar/images/env-bebebe.svg diff --git a/images/env-ffffff.png b/modules/environment_indicator_toolbar/images/env-ffffff.png similarity index 100% rename from images/env-ffffff.png rename to modules/environment_indicator_toolbar/images/env-ffffff.png diff --git a/images/env-ffffff.svg b/modules/environment_indicator_toolbar/images/env-ffffff.svg similarity index 100% rename from images/env-ffffff.svg rename to modules/environment_indicator_toolbar/images/env-ffffff.svg diff --git a/modules/environment_indicator_toolbar/js/toolbar.js b/modules/environment_indicator_toolbar/js/toolbar.js new file mode 100644 index 00000000..17a7d169 --- /dev/null +++ b/modules/environment_indicator_toolbar/js/toolbar.js @@ -0,0 +1,34 @@ +(function ($, Drupal) { + + "use strict"; + + Drupal.behaviors.environmentIndicatorToolbar = { + attach: function (context, settings) { + if (typeof(settings.environmentIndicator) != 'undefined') { + const $body = $('body'); + const borderWidth = getComputedStyle(document.body).getPropertyValue('--enviroment-indicator-border-width') || '6px'; + + // Only apply text and background color if not using gin_toolbar + if (!$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar')) { + $('#toolbar-bar', context).css('background-color', settings.environmentIndicator.bgColor); + $('#toolbar-bar .toolbar-item a', context).not('.is-active').css('color', settings.environmentIndicator.fgColor); + } + + // Set environment color for gin_toolbar vertical toolbar. + if ($body.hasClass('gin--vertical-toolbar')) { + $('.toolbar-menu-administration', context).css({'border-left-color': settings.environmentIndicator.bgColor, 'border-left-width': borderWidth}); + $('.toolbar-tray-horizontal .toolbar-menu li.menu-item', context).css({'margin-left': 'calc(var(--enviroment-indicator-border-width) * -0.5)'}); + } + // Set environment color for gin_toolbar horizontal toolbar. + if ($body.hasClass('gin--horizontal-toolbar')) { + $('#toolbar-item-administration-tray').css({'border-top-color': settings.environmentIndicator.bgColor, 'border-top-width': borderWidth}); + } + // Set environment color on the icon of the gin_toolbar + if($body.hasClass('gin--horizontal-toolbar') || $body.hasClass('gin--vertical-toolbar')) { + $('head', context).append("<style>.toolbar .toolbar-bar #toolbar-item-administration-tray a.toolbar-icon-admin-toolbar-tools-help.toolbar-icon-default::before{ background-color: " + settings.environmentIndicator.bgColor + " }</style>"); + } + } + } + }; + +})(jQuery, Drupal); diff --git a/src/ToolbarHandler.php b/modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php similarity index 86% rename from src/ToolbarHandler.php rename to modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php index c2cccc7c..2f67b641 100644 --- a/src/ToolbarHandler.php +++ b/modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php @@ -1,6 +1,6 @@ <?php -namespace Drupal\environment_indicator; +namespace Drupal\environment_indicator_toolbar\Service; use Drupal\Core\Cache\Cache; use Drupal\Core\Config\ConfigFactoryInterface; @@ -169,7 +169,10 @@ class ToolbarHandler { '#heading' => $this->t('Environments menu'), ], '#attached' => [ - 'library' => ['environment_indicator/drupal.environment_indicator'], + 'library' => [ + 'environment_indicator_toolbar/toolbar', + 'environment_indicator/drupal.environment_indicator', + ], 'drupalSettings' => [ 'environmentIndicator' => [ 'name' => $title ?: ' ', @@ -283,43 +286,25 @@ class ToolbarHandler { * @return array */ public function getLinks(): array { - if (!$environment_entities = EnvironmentIndicator::loadMultiple()) { - return []; - } - - $current = Url::fromRoute('<current>'); - $current_path = $current->toString(); - $environment_entities = array_filter( - $environment_entities, - function (EnvironmentIndicator $entity) { - return $entity->status() - && !empty($entity->getUrl()) - && $this->hasAccessEnvironment($entity->id()); + $links = []; + if ($environment_entities = EnvironmentIndicator::loadMultiple()) { + foreach ($environment_entities as $entity) { + // Assuming you have a method to check access. + if ($entity->status() && $this->hasAccessEnvironment($entity->id())) { + $links = array_merge($links, $entity->getLinks()); + } } - ); - - $links = array_map( - function (EnvironmentIndicator $entity) use ($current_path) { - return [ - 'attributes' => [ - 'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $entity->getBgColor() . ';', - 'title' => $this->t('Opens the current page in the selected environment.'), + + if ($links) { + $items['environment_indicator']['tray']['environment_links'] = [ + '#theme' => 'links__toolbar_shortcuts', + '#links' => $links, + '#attributes' => [ + 'class' => ['toolbar-menu'], ], - 'title' => $this->t('Open on @label', ['@label' => $entity->label()]), - 'url' => Url::fromUri($entity->getUrl() . $current_path), - 'type' => 'link', - 'weight' => $entity->getWeight(), ]; - }, - $environment_entities - ); - - if (!$links) { - return []; + } } - - uasort($links, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); - return $links; } diff --git a/src/Entity/EnvironmentIndicator.php b/src/Entity/EnvironmentIndicator.php index 935e44c9..a8c4d486 100644 --- a/src/Entity/EnvironmentIndicator.php +++ b/src/Entity/EnvironmentIndicator.php @@ -4,6 +4,7 @@ namespace Drupal\environment_indicator\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\Core\Url; /** * Defines a Environment configuration entity. @@ -201,6 +202,34 @@ class EnvironmentIndicator extends ConfigEntityBase implements ConfigEntityInter */ public function setBgColor($bg_color) { $this->set('bg_color', $bg_color); + + } + + /** + * Gets the links for the environment switcher. + * + * @return array + * An array of environment links. + */ + public function getLinks(): array { + $links = []; + $current_path = Url::fromRoute('<current>')->toString(); + + // Build the link for the current environment. + if ($this->status() && !empty($this->getUrl())) { + $links[] = [ + 'attributes' => [ + 'style' => 'color: ' . $this->getFgColor() . '; background-color: ' . $this->getBgColor() . ';', + 'title' => t('Opens the current page in the selected environment.'), + ], + 'title' => t('Open on @label', ['@label' => $this->label()]), + 'url' => Url::fromUri($this->getUrl() . $current_path), + 'type' => 'link', + 'weight' => $this->getWeight(), + ]; + } + + return $links; } } -- GitLab From ffcb44daadb776d2dc3bd26c24422cf86281a13e Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Mon, 18 Nov 2024 21:30:26 +0000 Subject: [PATCH 02/56] Apply 1 suggestion(s) to 1 file(s) --- environment_indicator.module | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/environment_indicator.module b/environment_indicator.module index 48f791cc..d0f213ca 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -249,6 +249,42 @@ function _environment_indicator_parse_style(string $style): array { return $structured_styles; } +/** + * Helper function that generates the environment switcher links. + * + * @return array + * A renderable array with the links. + */ +function _environment_indicator_switcher_links(): array { + return \Drupal::service('environment_indicator.toolbar_handler') + ->getLinks(); +} + +/** + * Helper function that checks if there is external integration. + * + * @param string $integration + * Name of the integration: toolbar, admin_menu, ... + * + * @return bool + * TRUE if integration is enabled. FALSE otherwise. + */ +function _environment_indicator_external_integration_is_enabled(string $integration): bool { + return \Drupal::service('environment_indicator.toolbar_handler') + ->externalIntegration($integration); +} + +/** + * Get the cache tags for the environment indicator switcher. + * + * @return string[] + * The cache tags. + */ +function _environment_indicator_switcher_cache_tags(): array { + return \Drupal::service('environment_indicator.toolbar_handler') + ->getCacheTags(); +} + /** * Loads an environment indicator in a procedural way. * -- GitLab From 8965a8d2e016ed2f329a11800269dd67b0885017 Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Mon, 18 Nov 2024 21:46:48 +0000 Subject: [PATCH 03/56] Apply 1 suggestion(s) to 1 file(s) --- environment_indicator.module | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index d0f213ca..48f791cc 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -249,42 +249,6 @@ function _environment_indicator_parse_style(string $style): array { return $structured_styles; } -/** - * Helper function that generates the environment switcher links. - * - * @return array - * A renderable array with the links. - */ -function _environment_indicator_switcher_links(): array { - return \Drupal::service('environment_indicator.toolbar_handler') - ->getLinks(); -} - -/** - * Helper function that checks if there is external integration. - * - * @param string $integration - * Name of the integration: toolbar, admin_menu, ... - * - * @return bool - * TRUE if integration is enabled. FALSE otherwise. - */ -function _environment_indicator_external_integration_is_enabled(string $integration): bool { - return \Drupal::service('environment_indicator.toolbar_handler') - ->externalIntegration($integration); -} - -/** - * Get the cache tags for the environment indicator switcher. - * - * @return string[] - * The cache tags. - */ -function _environment_indicator_switcher_cache_tags(): array { - return \Drupal::service('environment_indicator.toolbar_handler') - ->getCacheTags(); -} - /** * Loads an environment indicator in a procedural way. * -- GitLab From 77ad727552a9930b9f8de523519908aaf2be71ec Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 21 Feb 2025 19:25:54 -0700 Subject: [PATCH 04/56] Fix Stylelint issues --- css/environment_indicator.css | 48 ++++++++++++------- .../css/toolbar.css | 30 ++++++++---- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index f3a9c32c..6ba2a587 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -3,54 +3,68 @@ } #environment-indicator { + z-index: 10; + right: 0; + left: 0; + padding: 5px; text-align: center; white-space: nowrap; color: white; + background-image: linear-gradient(to bottom, + rgba(207, 207, 207, 0.3) 19%, + rgba(250, 250, 250, 0.3) 60%, + rgba(255, 255, 255, 0.3) 80%); text-shadow: 0 -1px 1px #333; font-weight: bold; - z-index: 10; - left: 0; - right: 0; - padding: 5px; - background-image: linear-gradient(bottom, rgba(207,207,207,0.3) 19%, rgba(250,250,250,0.3) 60%, rgba(255,255,255,0.3) 80%); } -#environment-indicator .description { font-size: 0.85em; } -#environment-indicator .description::before { content: "("; } -#environment-indicator .description::after { content: ")"; } +#environment-indicator .description { + font-size: 0.85em; +} + +#environment-indicator .description::before { + content: "("; +} -#environment-indicator ul { overflow: hidden; margin: 0; } +#environment-indicator .description::after { + content: ")"; +} + +#environment-indicator ul { + overflow: hidden; + margin: 0; +} #environment-indicator ul.environment-switcher-container li { - list-style: none; float: left; + list-style: none; } #environment-indicator ul.environment-switcher-container li a { margin: 0 3px; padding: 3px 8px; + border: none; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; - font-size: 0.8em; text-shadow: none; - border: none; + font-size: 0.8em; } #environment-indicator ul.environment-switcher-container li a:hover { - background-color: #DDD; + background-color: #ddd; box-shadow: inset 0 1px 5px #333; } #environment-indicator ul.environment-switcher-container { display: none; - padding: 8px 10px 6px 10px; margin-top: 1px; + padding: 8px 10px 6px 10px; } .environment-indicator-color { - font-size: 1em; text-align: center; border-radius: 5px; - background-color: #EDEDE0; -} + background-color: #edede0; + font-size: 1em; +} \ No newline at end of file diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index ca1f43ca..cd07c0f2 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -3,28 +3,29 @@ } .toolbar .toolbar-bar .toolbar-tab>a.toolbar-icon.is-active { - background-image: linear-gradient(rgba(255, 255, 255, 0.25) 20%, transparent 200%); + background-image: linear-gradient(rgba(255, 255, 255, 0.25) 20%, + transparent 200%); } -.toolbar-bar .toolbar-icon-environment:before { +.toolbar-bar .toolbar-icon-environment::before { background-image: url("../images/env-bebebe.svg"); } -.no-svg .toolbar-bar .toolbar-icon-environment:before { +.no-svg .toolbar-bar .toolbar-icon-environment::before { background-image: url("../images/env-bebebe.png"); } -.toolbar-bar .toolbar-icon-environment.is-active:before { +.toolbar-bar .toolbar-icon-environment.is-active::before { background-image: url("../images/env-ffffff.svg"); } -.no-svg .toolbar-bar .toolbar-icon-environment.is-active:before { +.no-svg .toolbar-bar .toolbar-icon-environment.is-active::before { background-image: url("../images/env-ffffff.png"); } .toolbar .toolbar-tray-vertical .edit-environments { - text-align: right; padding: 1em; + text-align: right; } .toolbar .toolbar-tray-horizontal .edit-environments { @@ -45,10 +46,19 @@ margin-top: var(--enviroment-indicator-border-width); } -[dir="ltr"] .gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { - margin-left: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); +.gin--vertical-toolbar { + .toolbar-menu-administration { + >.toolbar-menu>.menu-item .toolbar-menu { + margin-inline-start: calc(var(--gin-toolbar-width-collapsed, + var(--gin-toolbar-width-collapsed-fallback)) - 4px); + } + } } -[dir="rtl"] .gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { - margin-right: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); +.gin--vertical-toolbar[data-toolbar-menu="open"] { + .toolbar-menu-administration { + >.toolbar-menu>.menu-item .toolbar-menu { + margin-inline-start: calc(var(--gin-toolbar-width, var(--gin-toolbar-width-fallback)) - 4px); + } + } } \ No newline at end of file -- GitLab From 6a8a1a8c62a209d161c9f1ce27f2451ed6a6da3b Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 21 Feb 2025 19:30:21 -0700 Subject: [PATCH 05/56] Stylelint --- css/environment_indicator.css | 12 +++++++----- .../environment_indicator_toolbar/css/toolbar.css | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index 6ba2a587..fdbfaa1b 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -10,10 +10,12 @@ text-align: center; white-space: nowrap; color: white; - background-image: linear-gradient(to bottom, - rgba(207, 207, 207, 0.3) 19%, - rgba(250, 250, 250, 0.3) 60%, - rgba(255, 255, 255, 0.3) 80%); + background-image: linear-gradient( + to bottom, + rgba(207, 207, 207, 0.3) 19%, + rgba(250, 250, 250, 0.3) 60%, + rgba(255, 255, 255, 0.3) 80% + ); text-shadow: 0 -1px 1px #333; font-weight: bold; } @@ -67,4 +69,4 @@ border-radius: 5px; background-color: #edede0; font-size: 1em; -} \ No newline at end of file +} diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index cd07c0f2..8a9b2424 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -61,4 +61,4 @@ margin-inline-start: calc(var(--gin-toolbar-width, var(--gin-toolbar-width-fallback)) - 4px); } } -} \ No newline at end of file +} -- GitLab From ed76ef56a1c6bbd7aa777b9143898ded969202fd Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Wed, 7 May 2025 23:40:23 +0000 Subject: [PATCH 06/56] Apply 1 suggestion(s) to 1 file(s) --- css/environment_indicator.css | 1 - 1 file changed, 1 deletion(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index 9b6cdec0..5aa58d8f 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -53,5 +53,4 @@ text-align: center; border-radius: 5px; background-color: #edede0; - font-size: 1em; } \ No newline at end of file -- GitLab From 4da6ffa99bf7ea6f1932067dfa3f87afcbe510cc Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Wed, 7 May 2025 23:41:38 +0000 Subject: [PATCH 07/56] This is fixed in another MR --- css/environment_indicator.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index 5aa58d8f..b1d02d2e 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -52,5 +52,5 @@ font-size: 1em; text-align: center; border-radius: 5px; - background-color: #edede0; + background-color: #EDEDE0; } \ No newline at end of file -- GitLab From 2ede00979ad400ecfe1ee4d1a7d6ebfcb7300ff7 Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Wed, 7 May 2025 23:46:13 +0000 Subject: [PATCH 08/56] Edit environment_indicator.css --- css/environment_indicator.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index b1d02d2e..3179ffe1 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -53,4 +53,4 @@ text-align: center; border-radius: 5px; background-color: #EDEDE0; -} \ No newline at end of file +} -- GitLab From 7338f1205432d2628b17c101a5f66de5e3eb8b8c Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 7 May 2025 16:48:38 -0700 Subject: [PATCH 09/56] Fix merge issues. --- environment_indicator.install | 2 +- .../css/toolbar.css | 38 +++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/environment_indicator.install b/environment_indicator.install index 8631f2e6..409018a7 100644 --- a/environment_indicator.install +++ b/environment_indicator.install @@ -41,4 +41,4 @@ function environment_indicator_update_110401999() { 'toolbar', ]; \Drupal::service('module_installer')->install($modules); -} \ No newline at end of file +} diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index 8a9b2424..c28ee546 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -3,29 +3,28 @@ } .toolbar .toolbar-bar .toolbar-tab>a.toolbar-icon.is-active { - background-image: linear-gradient(rgba(255, 255, 255, 0.25) 20%, - transparent 200%); + background-image: linear-gradient(rgba(255, 255, 255, 0.25) 20%, transparent 200%); } -.toolbar-bar .toolbar-icon-environment::before { +.toolbar-bar .toolbar-icon-environment:before { background-image: url("../images/env-bebebe.svg"); } -.no-svg .toolbar-bar .toolbar-icon-environment::before { +.no-svg .toolbar-bar .toolbar-icon-environment:before { background-image: url("../images/env-bebebe.png"); } -.toolbar-bar .toolbar-icon-environment.is-active::before { +.toolbar-bar .toolbar-icon-environment.is-active:before { background-image: url("../images/env-ffffff.svg"); } -.no-svg .toolbar-bar .toolbar-icon-environment.is-active::before { +.no-svg .toolbar-bar .toolbar-icon-environment.is-active:before { background-image: url("../images/env-ffffff.png"); } .toolbar .toolbar-tray-vertical .edit-environments { - padding: 1em; text-align: right; + padding: 1em; } .toolbar .toolbar-tray-horizontal .edit-environments { @@ -34,31 +33,22 @@ /* Style fixes for gin_toolbar */ .gin--vertical-toolbar .toolbar-menu-administration { - border-left: var(--enviroment-indicator-border-width) solid; + border-left: var(--environment-indicator-border-width) solid; } .gin--horizontal-toolbar #toolbar-item-administration-tray { - border-top: var(--enviroment-indicator-border-width) solid; + border-top: var(--environment-indicator-border-width) solid; border-bottom: 0; } .gin--horizontal-toolbar .gin-secondary-toolbar { - margin-top: var(--enviroment-indicator-border-width); + margin-top: var(--environment-indicator-border-width); } -.gin--vertical-toolbar { - .toolbar-menu-administration { - >.toolbar-menu>.menu-item .toolbar-menu { - margin-inline-start: calc(var(--gin-toolbar-width-collapsed, - var(--gin-toolbar-width-collapsed-fallback)) - 4px); - } - } +.gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { + margin-inline-start: calc(var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px); } -.gin--vertical-toolbar[data-toolbar-menu="open"] { - .toolbar-menu-administration { - >.toolbar-menu>.menu-item .toolbar-menu { - margin-inline-start: calc(var(--gin-toolbar-width, var(--gin-toolbar-width-fallback)) - 4px); - } - } -} +.gin--vertical-toolbar[data-toolbar-menu="open"] .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { + margin-inline-start: calc(var(--gin-toolbar-width, var(--ginToolbarWidth)) - 4px); +} \ No newline at end of file -- GitLab From 24c579a8aaaf381a2fc70bd146bc1749680d6f64 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 7 May 2025 16:49:03 -0700 Subject: [PATCH 10/56] Fix merge issues. --- modules/environment_indicator_toolbar/css/toolbar.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index c28ee546..5b1f096a 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -51,4 +51,4 @@ .gin--vertical-toolbar[data-toolbar-menu="open"] .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { margin-inline-start: calc(var(--gin-toolbar-width, var(--ginToolbarWidth)) - 4px); -} \ No newline at end of file +} -- GitLab From a47d8cd866fe3b396bdd9d7eff71d992d7c8a112 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 7 May 2025 16:56:03 -0700 Subject: [PATCH 11/56] Code style fixes --- .../schema/environment_indicator.schema.yml | 24 +++++++++---------- .../js/toolbar.js | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/config/schema/environment_indicator.schema.yml b/config/schema/environment_indicator.schema.yml index 6c8a09f0..f133e035 100644 --- a/config/schema/environment_indicator.schema.yml +++ b/config/schema/environment_indicator.schema.yml @@ -49,15 +49,15 @@ environment_indicator.settings: default_value: 'deployment_identifier' environment_indicator.indicator: - type: config_object - label: 'Environment indicator local environment' - mapping: - name: - type: string - label: 'Environment name' - fg_color: - type: string - label: 'Foreground color' - bg_color: - type: string - label: 'Background color' + type: config_object + label: 'Environment indicator local environment' + mapping: + name: + type: string + label: 'Environment name' + fg_color: + type: string + label: 'Foreground color' + bg_color: + type: string + label: 'Background color' diff --git a/modules/environment_indicator_toolbar/js/toolbar.js b/modules/environment_indicator_toolbar/js/toolbar.js index 23ef4c62..3de70e42 100644 --- a/modules/environment_indicator_toolbar/js/toolbar.js +++ b/modules/environment_indicator_toolbar/js/toolbar.js @@ -1,5 +1,5 @@ (function ($, Drupal) { - Drupal.behaviors.environmentIndicatorToolbar = { + Drupal.behaviors.environmentIndicatorToolbar = { attach(context, settings) { if (settings.environmentIndicator !== undefined) { const $body = $('body'); -- GitLab From 96b7a1e62b1277f92330f228b3dd9f7c1298ae66 Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Thu, 8 May 2025 00:02:27 +0000 Subject: [PATCH 12/56] Apply 1 suggestion(s) to 1 file(s) --- environment_indicator.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment_indicator.module b/environment_indicator.module index 399ddac4..8b3f271f 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -189,7 +189,7 @@ function environment_indicator_page_top(array &$page_top) { 'style' => 'cursor: pointer', 'title' => t('Show the environment switcher.'), ]; - $environment_indicator_switcher_cache_tags = \Drupal::entityTypeManager()->getDefinition('environment_indicator')->getListCacheTags(); + $environment_indicator_switcher_cache_tags = \Drupal::entityTypeManager()->getDefinition('environment_indicator')->getListCacheTags(); $page_top['indicator'] += [ '#cache' => [ 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], $environment_indicator_switcher_cache_tags), -- GitLab From 89e212b42e394fb69a73d289761b278e7c3aa8c1 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 7 May 2025 17:23:54 -0700 Subject: [PATCH 13/56] Fix PHPStan error --- environment_indicator.module | 1 + 1 file changed, 1 insertion(+) diff --git a/environment_indicator.module b/environment_indicator.module index 399ddac4..bd9fcabe 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -8,6 +8,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; +use Drupal\environment_indicator\Entity\EnvironmentIndicator; /** * Implements hook_help(). -- GitLab From e1dca6c26d42530c1e378393f6ed5392f5958fd5 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Wed, 21 May 2025 08:43:04 -0700 Subject: [PATCH 14/56] Remove js from main module. --- js/environment_indicator.js | 67 ------------------------------------- 1 file changed, 67 deletions(-) diff --git a/js/environment_indicator.js b/js/environment_indicator.js index 6535af41..b5d42141 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -16,71 +16,4 @@ }); }, }; - - Drupal.behaviors.environmentIndicatorToolbar = { - attach(context, settings) { - if (settings.environmentIndicator !== undefined) { - const $body = $('body'); - const borderWidth = - getComputedStyle(document.body).getPropertyValue( - '--environment-indicator-border-width', - ) || '6px'; - // Set environment color if not using gin_toolbar. - if ( - settings.environmentIndicator.toolbars.toolbar === 'toolbar' && - !$body.hasClass('gin--vertical-toolbar') && - !$body.hasClass('gin--horizontal-toolbar') - ) { - // Replaced $.css with direct style assignment using JavaScript. - document.querySelector('#toolbar-bar').style.backgroundColor = - settings.environmentIndicator.bgColor; - document - .querySelectorAll('#toolbar-bar .toolbar-item a:not(.is-active)') - .forEach((el) => { - el.style.color = settings.environmentIndicator.fgColor; - return el; - }); - } - - // Set environment color for gin_toolbar vertical toolbar. - if ($body.hasClass('gin--vertical-toolbar')) { - document.querySelector( - '.toolbar-menu-administration', - ).style.borderLeftColor = settings.environmentIndicator.bgColor; - document.querySelector( - '.toolbar-menu-administration', - ).style.borderLeftWidth = borderWidth; - document - .querySelectorAll( - '.toolbar-tray-horizontal .toolbar-menu li.menu-item', - ) - .forEach((el) => { - el.style.marginLeft = - 'calc(var(--environment-indicator-border-width) * -0.5)'; - return el; - }); - } - - // Set environment color for gin_toolbar horizontal toolbar. - if ($body.hasClass('gin--horizontal-toolbar')) { - document.querySelector( - '#toolbar-item-administration-tray', - ).style.borderTopColor = settings.environmentIndicator.bgColor; - document.querySelector( - '#toolbar-item-administration-tray', - ).style.borderTopWidth = borderWidth; - } - - // Set environment color on the icon of the gin_toolbar - if ( - $body.hasClass('gin--horizontal-toolbar') || - $body.hasClass('gin--vertical-toolbar') - ) { - $('head', context).append( - `<style>.toolbar .toolbar-bar #toolbar-item-administration-tray a.toolbar-icon-admin-toolbar-tools-help.toolbar-icon-default::before{ background-color: ${settings.environmentIndicator.bgColor} }</style>`, - ); - } - } - }, - }; })(jQuery, Drupal, once); -- GitLab From 84b0fb50eba0f68b4e5b27c55345005ae7f948a9 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Tue, 27 May 2025 20:15:40 -0700 Subject: [PATCH 15/56] PHPCS --- .../schema/environment_indicator.schema.yml | 1 + environment_indicator.module | 117 ++--- environment_indicator.services.yml | 23 + ...environment_indicator_toolbar.services.yml | 2 + .../js/toolbar.js | 1 + .../src/{Service => }/ToolbarHandler.php | 4 - src/Form/EnvironmentIndicatorSettingsForm.php | 14 +- src/Service/EnvironmentIndicator.php | 105 +++++ src/Service/SwitcherManager.php | 109 +++++ src/ToolbarHandler.php | 443 ++++++++++++++++++ 10 files changed, 761 insertions(+), 58 deletions(-) create mode 100644 environment_indicator.services.yml rename modules/environment_indicator_toolbar/src/{Service => }/ToolbarHandler.php (99%) create mode 100644 src/Service/EnvironmentIndicator.php create mode 100644 src/Service/SwitcherManager.php create mode 100644 src/ToolbarHandler.php diff --git a/config/schema/environment_indicator.schema.yml b/config/schema/environment_indicator.schema.yml index f133e035..af6697a6 100644 --- a/config/schema/environment_indicator.schema.yml +++ b/config/schema/environment_indicator.schema.yml @@ -36,6 +36,7 @@ environment_indicator.settings: sequence: type: string label: 'Toolbar identifier' + deprecated: "The 'toolbar_integration' config schema is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Enable the environment_indicator_toolbar module for toolbar integration. See http://drupal.org/node/the-change-notice-drerp." favicon: type: boolean label: 'Show a colored favicon for environment' diff --git a/environment_indicator.module b/environment_indicator.module index 42e31d8a..3c99dc1b 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -8,7 +8,6 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; -use Drupal\environment_indicator\Entity\EnvironmentIndicator; /** * Implements hook_help(). @@ -72,54 +71,6 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc return NULL; } -/** - * Get all environment links. - * - * @return array - * An array of links for each environment. - */ -function _environment_indicator_switcher_links() { - /** @var \Drupal\environment_indicator\Entity\EnvironmentIndicator[] $environment_switcher_entities */ - if (!$environment_switcher_entities = EnvironmentIndicator::loadMultiple()) { - return []; - } - - $current = Url::fromRoute('<current>'); - $current_path = $current->toString(); - $environment_switcher_entities = array_filter( - $environment_switcher_entities, - function (EnvironmentIndicator $entity) { - return $entity->status() - && !empty($entity->getUrl()); - // && \Drupal::service('environment_indicator_toolbar.toolbar_handler')->hasAccessEnvironment($entity->id()); - } - ); - - $links = array_map( - function (EnvironmentIndicator $entity) use ($current_path) { - return [ - 'attributes' => [ - 'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $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(), - ]; - }, - $environment_switcher_entities - ); - - if (!$links) { - return []; - } - - uasort($links, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); - - return $links; -} - /** * Implements hook_preprocess_HOOK() for page templates. */ @@ -181,8 +132,7 @@ function environment_indicator_page_top(array &$page_top) { } // Only add the environment indicator switcher if there are environments to // switch to. - if ($items = _environment_indicator_switcher_links()) { - uasort($items, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); + if ($items = \Drupal::service('environment_indicator.switcher_manager')->getLinks()) { $page_top['indicator']['switcher'] = [ '#theme' => 'links', '#links' => $items, @@ -196,10 +146,9 @@ function environment_indicator_page_top(array &$page_top) { 'style' => 'cursor: pointer', 'title' => t('Show the environment switcher.'), ]; - $environment_indicator_switcher_cache_tags = \Drupal::entityTypeManager()->getDefinition('environment_indicator')->getListCacheTags(); $page_top['indicator'] += [ '#cache' => [ - 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], $environment_indicator_switcher_cache_tags), + 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], \Drupal::service('environment_indicator.switcher_manager')->getCacheTags()), ], ]; } @@ -287,3 +236,65 @@ function environment_indicator_load(string $environment_id) { ->getStorage('environment_indicator') ->load($environment_id); } + +/** + * Implements hook_toolbar(). + */ +function environment_indicator_toolbar() { + return \Drupal::service('environment_indicator.toolbar_handler') + ->toolbar(); +} + +/** + * Helper function that generates the environment switcher links. + * + * @return array + * A renderable array with the links. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use \Drupal\environment_indicator\Service\SwitcherManager::getLinks(). + * + * @see https://www.drupal.org/node/3526893 + */ +function _environment_indicator_switcher_links(): array { + return \Drupal::service('environment_indicator.toolbar_handler') + ->getLinks(); +} + +/** + * Helper function that checks if there is external integration. + * + * @param string $integration + * Name of the integration: toolbar, admin_menu, ... + * + * @return bool + * TRUE if integration is enabled. FALSE otherwise. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * This functionality is no longer needed because now to add toolbar + * integration, you enable the environment_indicator_toolbar module. + * Then you can replicate this functionality by using the + * core.extension.service to check if the module is enabled. + * + * @see https://www.drupal.org/node/ spllingsljldskajflj + */ +function _environment_indicator_external_integration_is_enabled(string $integration): bool { + return \Drupal::service('environment_indicator.toolbar_handler') + ->externalIntegration($integration); +} + +/** + * Get the cache tags for the environment indicator switcher. + * + * @return string[] + * The cache tags. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use \Drupal\environment_indicator\Service\SwitcherManager::getCacheTags(). + * + * @see https://www.drupal.org/node/3526893 + */ +function _environment_indicator_switcher_cache_tags(): array { + return \Drupal::service('environment_indicator.toolbar_handler') + ->getCacheTags(); +} diff --git a/environment_indicator.services.yml b/environment_indicator.services.yml new file mode 100644 index 00000000..3f6e5ccf --- /dev/null +++ b/environment_indicator.services.yml @@ -0,0 +1,23 @@ +services: + environment_indicator.toolbar_handler: + class: Drupal\environment_indicator\ToolbarHandler + arguments: + - '@module_handler' + - '@config.factory' + - '@current_user' + - '@state' + - '@settings' + - '@entity_type.manager' + - '@environment_indicator.indicator' + - '@environment_indicator.switcher_manager' + environment_indicator.indicator: + class: Drupal\environment_indicator\Service\EnvironmentIndicator + arguments: + - '@config.factory' + - '@state' + - '@settings' + environment_indicator.switcher_manager: + class: Drupal\environment_indicator\Service\SwitcherManager + arguments: + - '@entity_type.manager' + - '@current_route_match' diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml index 292f16ba..6097e829 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml @@ -8,3 +8,5 @@ services: - '@state' - '@settings' - '@entity_type.manager' + - '@environment_indicator.indicator' + - '@environment_indicator.switcher_manager' diff --git a/modules/environment_indicator_toolbar/js/toolbar.js b/modules/environment_indicator_toolbar/js/toolbar.js index 3de70e42..6085ea69 100644 --- a/modules/environment_indicator_toolbar/js/toolbar.js +++ b/modules/environment_indicator_toolbar/js/toolbar.js @@ -9,6 +9,7 @@ ) || '6px'; // Set environment color if not using gin_toolbar. if ( + settings.environmentIndicator.toolbars.toolbar === 'toolbar' && !$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar') ) { diff --git a/modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php b/modules/environment_indicator_toolbar/src/ToolbarHandler.php similarity index 99% rename from modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php rename to modules/environment_indicator_toolbar/src/ToolbarHandler.php index cde0f59a..72e0375f 100644 --- a/modules/environment_indicator_toolbar/src/Service/ToolbarHandler.php +++ b/modules/environment_indicator_toolbar/src/ToolbarHandler.php @@ -174,7 +174,6 @@ class ToolbarHandler { '#attached' => [ 'library' => [ 'environment_indicator_toolbar/toolbar', - 'environment_indicator/drupal.environment_indicator', ], 'drupalSettings' => [ 'environmentIndicator' => [ @@ -372,6 +371,3 @@ class ToolbarHandler { } } - - -} diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index bd857d67..4cba8aa1 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -22,9 +22,10 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('environment_indicator.settings'); + $toolbar_integration_module_enabled = $this->moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; + $toolbar_integration_setting_enabled = $config->get('toolbar_integration')[0] ?? FALSE; $form = parent::buildForm($form, $form_state); - $form['toolbar_integration'] = [ '#type' => 'checkboxes', '#title' => $this->t('Toolbar integration'), @@ -34,6 +35,17 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { '#description' => $this->t('Select the toolbars that you want to integrate with.'), '#default_value' => $config->get('toolbar_integration') ?: [], ]; + // If the toolbar integration settings are empty, we remove the + // toolbar integration settings from the form. + if (!$toolbar_integration_module_enabled || !$toolbar_integration_setting_enabled) { + unset($form['toolbar_integration']); + // Add a message to inform the user that the toolbar integration is not enabled. We can + // inform them of the environment_indicator_toolbar module. + $form['toolbar_integration_message'] = [ + '#type' => 'item', + '#markup' => $this->t('The <strong>Environment Indicator Toolbar</strong> module is not enabled. Please enable it to use toolbar integration.'), + ]; + } $form['favicon'] = [ '#type' => 'checkbox', diff --git a/src/Service/EnvironmentIndicator.php b/src/Service/EnvironmentIndicator.php new file mode 100644 index 00000000..de55e3c0 --- /dev/null +++ b/src/Service/EnvironmentIndicator.php @@ -0,0 +1,105 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\environment_indicator\Service; + +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Site\Settings; +use Drupal\Core\State\StateInterface; + +/** + * Provides contextual info about the active environment indicator. + */ +class EnvironmentIndicator { + + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected ConfigFactoryInterface $configFactory; + + /** + * The state service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected StateInterface $state; + + /** + * The settings service. + * + * @var \Drupal\Core\Site\Settings + */ + protected Settings $settings; + + /** + * Indicator constructor. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory service. + * @param \Drupal\Core\State\StateInterface $state + * The state service. + * @param \Drupal\Core\Site\Settings $settings + * The settings service. + */ + public function __construct( + ConfigFactoryInterface $config_factory, + StateInterface $state, + Settings $settings, + ) { + $this->configFactory = $config_factory; + $this->state = $state; + $this->settings = $settings; + } + + /** + * Gets the current release string based on version ID config and fallback. + * + * @return string|null + * The current release identifier, or NULL if not set. + */ + public function getCurrentRelease(): ?string { + $config = $this->configFactory->get('environment_indicator.settings'); + + $primary = $config->get('version_identifier') ?? 'environment_indicator_current_release'; + $fallback = $config->get('version_identifier_fallback') ?? 'deployment_identifier'; + + return $this->getVersionIdentifier($primary) + ?? ($primary !== $fallback ? $this->getVersionIdentifier($fallback) : NULL); + } + + /** + * Resolves a version identifier by type. + * + * @param string $type + * The type of version identifier to retrieve. + * + * @return string|null + * The version identifier string, or NULL if not applicable. + */ + protected function getVersionIdentifier(string $type): ?string { + return match ($type) { + 'environment_indicator_current_release' => $this->state->get('environment_indicator.current_release'), + 'deployment_identifier' => $this->settings->get('deployment_identifier'), + 'drupal_version' => \Drupal::VERSION, + default => NULL, + }; + } + + /** + * Returns the combined label for the current environment. + * + * @return string|null + * The title string combining the environment name and current release, + * or NULL if no environment is set. + */ + public function getTitle(): ?string { + $env = $this->configFactory->get('environment_indicator.indicator'); + $environment = $env->get('name'); + $release = $this->getCurrentRelease(); + return $environment ? ($release ? "($release) $environment" : $environment) : NULL; + } + +} diff --git a/src/Service/SwitcherManager.php b/src/Service/SwitcherManager.php new file mode 100644 index 00000000..15cec074 --- /dev/null +++ b/src/Service/SwitcherManager.php @@ -0,0 +1,109 @@ +<?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' => $this->t('Opens the current page in the selected environment.'), + ], + 'title' => $this->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(); + } + +} diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php new file mode 100644 index 00000000..c8210f56 --- /dev/null +++ b/src/ToolbarHandler.php @@ -0,0 +1,443 @@ +<?php + +namespace Drupal\environment_indicator; + +use Drupal\Core\Cache\Cache; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ImmutableConfig; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Session\AccountProxyInterface; +use Drupal\Core\Site\Settings; +use Drupal\Core\State\StateInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Url; +use Drupal\environment_indicator\Service\EnvironmentIndicator; +use Drupal\environment_indicator\Service\SwitcherManager; + +@trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. See https://www.drupal.org/node/the-change-notice-nid', E_USER_DEPRECATED); + +/** + * Toolbar integration handler. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Use + * \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. + * + * @see https://www.drupal.org/node/the-change-notice-nid + */ +class ToolbarHandler { + + use StringTranslationTrait; + + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected ModuleHandlerInterface $moduleHandler; + + /** + * The environment indicator config. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + protected ImmutableConfig $config; + + /** + * The active environment. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + protected ImmutableConfig $activeEnvironment; + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected AccountProxyInterface $account; + + /** + * The state system. + * + * @var \Drupal\Core\State\StateInterface + */ + protected StateInterface $state; + + /** + * Drupal settings. + * + * @var \Drupal\Core\Site\Settings + */ + protected Settings $settings; + + /** + * Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected EntityTypeManagerInterface $entityTypeManager; + + /** + * The indicator service. + * + * @var \Drupal\environment_indicator\Service\EnvironmentIndicator + */ + protected EnvironmentIndicator $environmentIndicator; + + /** + * The SwitcherManager service. + * + * @var \Drupal\environment_indicator\Service\SwitcherManager + */ + protected SwitcherManager $switcherManager; + + /** + * ToolbarHandler constructor. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + * @param \Drupal\Core\Session\AccountProxyInterface $account + * The current user. + * @param \Drupal\Core\State\StateInterface $state + * The state system. + * @param \Drupal\Core\Site\Settings $settings + * The settings. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\environment_indicator\Service\EnvironmentIndicator $environment_indicator + * The environment indicator service. + * @param \Drupal\environment_indicator\Service\SwitcherManager $switcher_manager + * The switcher manager service. + */ + public function __construct( + ModuleHandlerInterface $module_handler, + ConfigFactoryInterface $config_factory, + AccountProxyInterface $account, + StateInterface $state, + Settings $settings, + EntityTypeManagerInterface $entity_type_manager, + EnvironmentIndicator $environment_indicator, + SwitcherManager $switcher_manager, + ) { + $this->moduleHandler = $module_handler; + $this->config = $config_factory->get('environment_indicator.settings'); + $this->activeEnvironment = $config_factory->get('environment_indicator.indicator'); + $this->account = $account; + $this->state = $state; + $this->settings = $settings; + $this->entityTypeManager = $entity_type_manager; + $this->environmentIndicator = $environment_indicator; + $this->switcherManager = $switcher_manager; + } + + /** + * User can access all indicators. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. There is no + * direct replacement for this method. + * @see https://www.drupal.org/node/3526812 + */ + public function hasAccessAll(): bool { + return $this->account->hasPermission('access environment indicator'); + } + + /** + * User can access a specific indicator. + * + * @param object $environment + * The environment identifier. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. There is no + * direct replacement for this method. + * @see https://www.drupal.org/node/3526812 + */ + public function hasAccessEnvironment($environment): bool { + return $this->hasAccessAll() || $this->account->hasPermission('access environment indicator ' . $environment); + } + + /** + * User can access the indicator for the active environment. + * + * @return bool + * TRUE on success, FALSE on failure. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. There is no + * direct replacement for this method. + * @see https://www.drupal.org/node/3526812 + */ + public function hasAccessActiveEnvironment(): bool { + return $this->hasAccessEnvironment($this->activeEnvironment->get('machine')); + } + + /** + * Hook bridge. + * + * @return array + * The environment indicator toolbar items render array. + * + * @see hook_toolbar() + */ + public function toolbar(): array { + $items['environment_indicator'] = [ + '#cache' => [ + 'contexts' => ['user.permissions'], + ], + ]; + + if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { + + $title = $this->getTitle(); + + $items['environment_indicator'] += [ + '#type' => 'toolbar_item', + '#weight' => 125, + 'tab' => [ + '#type' => 'link', + '#title' => $title, + '#url' => Url::fromRoute('environment_indicator.settings'), + '#attributes' => [ + 'title' => $this->t('Environments'), + 'class' => ['toolbar-icon', 'toolbar-icon-environment'], + ], + '#access' => !empty($title), + ], + 'tray' => [ + '#heading' => $this->t('Environments menu'), + ], + '#attached' => [ + 'library' => ['environment_indicator/drupal.environment_indicator'], + 'drupalSettings' => [ + 'environmentIndicator' => [ + 'name' => $this->activeEnvironment->get('name') ?: ' ', + 'fgColor' => $this->activeEnvironment->get('fg_color'), + 'bgColor' => $this->activeEnvironment->get('bg_color'), + 'toolbars' => $this->config->get('toolbar_integration'), + ], + ], + ], + ]; + if ($this->config->get('favicon')) { + $items['environment_indicator']['#attached']['drupalSettings']['environmentIndicator']['addFavicon'] = $this->config->get('favicon'); + $items['environment_indicator']['#attached']['library'][] = 'environment_indicator/favicon'; + } + // Add cache tags to the toolbar item while preserving context. + $items['environment_indicator']['#cache']['tags'] = Cache::mergeTags( + [ + 'config:environment_indicator.settings', + 'config:environment_indicator.indicator', + ], + $this->getCacheTags() + ); + if ($this->account->hasPermission('administer environment indicator settings')) { + $items['environment_indicator']['tray']['configuration'] = [ + '#type' => 'link', + '#title' => $this->t('Configure'), + '#url' => Url::fromRoute('environment_indicator.settings'), + '#options' => [ + 'attributes' => ['class' => ['edit-environments']], + ], + ]; + } + + if ($links = $this->switcherManager->getLinks()) { + $items['environment_indicator']['tray']['environment_links'] = [ + '#theme' => 'links__toolbar_shortcuts', + '#links' => $links, + '#attributes' => [ + 'class' => ['toolbar-menu'], + ], + ]; + } + } + + return $items; + } + + /** + * Retrieve value from the selected version identifier source. + * + * @return string|null + * The current release identifier as a string, or NULL if not available. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use + * \Drupal::service('environment_indicator.indicator')->getCurrentRelease() + * instead. + * @see https://www.drupal.org/node/3526812 + */ + public function getCurrentRelease(): ?string { + $version_identifier = $this->config->get('version_identifier') ?? 'environment_indicator_current_release'; + $version_identifier_fallback = $this->config->get('version_identifier_fallback') ?? 'deployment_identifier'; + + $release = $this->getVersionIdentifier($version_identifier); + if ($release !== NULL) { + return $release; + } + + if ($version_identifier !== $version_identifier_fallback) { + return $this->getVersionIdentifier($version_identifier_fallback); + } + + return NULL; + } + + /** + * Helper function to get version identifier based on the type. + * + * @param string $type + * The type of version identifier. + * + * @return string|null + * The version identifier. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use + * \Drupal::service('environment_indicator.indicator')->getVersionIdentifier() + * instead. + * @see https://www.drupal.org/node/3526812 + */ + protected function getVersionIdentifier(string $type): ?string { + switch ($type) { + case 'environment_indicator_current_release': + $current_release = $this->state->get('environment_indicator.current_release'); + return $current_release !== NULL ? (string) $current_release : NULL; + + case 'deployment_identifier': + $deployment_identifier = $this->settings->get('deployment_identifier'); + return $deployment_identifier !== NULL ? (string) $deployment_identifier : NULL; + + case 'drupal_version': + return \Drupal::VERSION; + + case 'none': + default: + return NULL; + } + } + + /** + * Construct the title for the active environment. + * + * @return string|null + * The constructed title, including the release if available, or NULL if the + * environment name is not set. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use + * \Drupal::service('environment_indicator.indicator')->getTitle() + * instead. + * @see https://www.drupal.org/node/3526812 + */ + public function getTitle(): ?string { + $environment = $this->activeEnvironment->get('name'); + $release = $this->getCurrentRelease(); + return ($release) ? '(' . $release . ') ' . $environment : $environment; + } + + /** + * Helper function that checks if there is external integration. + * + * @param string $integration + * Name of the integration: toolbar, admin_menu, ... + * + * @return bool + * TRUE if integration is enabled. FALSE otherwise. + */ + public function externalIntegration($integration): bool { + if ($integration == 'toolbar') { + if ($this->moduleHandler->moduleExists('toolbar')) { + $toolbar_integration = $this->config->get('toolbar_integration') ?? []; + if (in_array('toolbar', $toolbar_integration)) { + if ($this->account->hasPermission('access toolbar')) { + return TRUE; + } + } + } + } + return FALSE; + } + + /** + * Get the cache tags for the environment indicator switcher. + * + * @return array + * The cache tags. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use + * \Drupal::service('environment_indicator.switcher_manager')->getCacheTags() + * instead. + * @see https://www.drupal.org/node/3526893 + */ + public function getCacheTags(): array { + return $this->entityTypeManager->getDefinition('environment_indicator')->getListCacheTags(); + } + + /** + * Get all the links for the switcher. + * + * @return array + * Returns all the links. + * + * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. + * Use + * \Drupal::service('environment_indicator.switcher_manager')->getLinks() + * instead. + * @see https://www.drupal.org/node/3526893 + */ + public function getLinks(): array { + if (!$environment_entities = EnvironmentIndicator::loadMultiple()) { + return []; + } + + $current = Url::fromRoute('<current>'); + $current_path = $current->toString(); + $url = parse_url($current_path); + $path = $url['path']; + if (isset($url['query'])) { + $path .= '?' . $url['query']; + } + $environment_entities = array_filter( + $environment_entities, + function (EnvironmentIndicator $entity) { + return $entity->status() + && !empty($entity->getUrl()) + && $this->hasAccessEnvironment($entity->id()); + } + ); + + $links = array_map( + function (EnvironmentIndicator $entity) use ($path) { + return [ + 'attributes' => [ + 'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $entity->getBgColor() . ';', + 'title' => $this->t('Opens the current page in the selected environment.'), + ], + 'title' => $this->t('Open on @label', ['@label' => $entity->label()]), + 'url' => Url::fromUri($entity->getUrl() . $path), + 'type' => 'link', + 'weight' => $entity->getWeight(), + ]; + }, + $environment_entities + ); + + if (!$links) { + return []; + } + + uasort($links, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); + + return $links; + } + +} -- GitLab From 4bedc52995c2e46b0efc5082582c2472fc57c588 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Tue, 27 May 2025 21:12:36 -0700 Subject: [PATCH 16/56] PHPCS --- environment_indicator.module | 30 +++++------ src/Entity/EnvironmentIndicator.php | 4 +- src/Form/EnvironmentIndicatorSettingsForm.php | 53 +++++++++++++++++-- src/Service/SwitcherManager.php | 2 + 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index 3c99dc1b..b9579594 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -222,21 +222,6 @@ function _environment_indicator_parse_style(string $style): array { return $structured_styles; } -/** - * Loads an environment indicator in a procedural way. - * - * @param string $environment_id - * The entity ID. - * - * @return \Drupal\Core\Entity\EntityInterface|null - * The loaded entity or null otherwise. - */ -function environment_indicator_load(string $environment_id) { - return \Drupal::entityTypeManager() - ->getStorage('environment_indicator') - ->load($environment_id); -} - /** * Implements hook_toolbar(). */ @@ -298,3 +283,18 @@ function _environment_indicator_switcher_cache_tags(): array { return \Drupal::service('environment_indicator.toolbar_handler') ->getCacheTags(); } + +/** + * Loads an environment indicator in a procedural way. + * + * @param string $environment_id + * The entity ID. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The loaded entity or null otherwise. + */ +function environment_indicator_load(string $environment_id) { + return \Drupal::entityTypeManager() + ->getStorage('environment_indicator') + ->load($environment_id); +} diff --git a/src/Entity/EnvironmentIndicator.php b/src/Entity/EnvironmentIndicator.php index 3546f9b9..501dc8f1 100644 --- a/src/Entity/EnvironmentIndicator.php +++ b/src/Entity/EnvironmentIndicator.php @@ -219,9 +219,9 @@ class EnvironmentIndicator extends ConfigEntityBase implements ConfigEntityInter $links[] = [ 'attributes' => [ 'style' => 'color: ' . $this->getFgColor() . '; background-color: ' . $this->getBgColor() . ';', - 'title' => t('Opens the current page in the selected environment.'), + 'title' => $this->t('Opens the current page in the selected environment.'), ], - 'title' => t('Open on @label', ['@label' => $this->label()]), + 'title' => $this->t('Open on @label', ['@label' => $this->label()]), 'url' => Url::fromUri($this->getUrl() . $current_path), 'type' => 'link', 'weight' => $this->getWeight(), diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index 4cba8aa1..c2d74a55 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -4,12 +4,56 @@ namespace Drupal\environment_indicator\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\TypedConfigManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Basic Environment Indicator controls form. + * Configure System settings for this site. */ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * Constructs a MenuLinksetSettingsForm object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager + * The typed config manager. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + */ + public function __construct( + ConfigFactoryInterface $config_factory, + TypedConfigManagerInterface $typedConfigManager, + protected readonly ModuleHandlerInterface $module_handler, + ) { + parent::__construct( + $config_factory, + $typedConfigManager + ); + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('config.typed'), + $container->get('module_handler') + ); + } + /** * {@inheritdoc} */ @@ -22,7 +66,7 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('environment_indicator.settings'); - $toolbar_integration_module_enabled = $this->moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; + $toolbar_integration_module_enabled = $this->moduleHandler->moduleExists('environment_indicator_toolbar') ?? FALSE; $toolbar_integration_setting_enabled = $config->get('toolbar_integration')[0] ?? FALSE; $form = parent::buildForm($form, $form_state); @@ -39,8 +83,9 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { // toolbar integration settings from the form. if (!$toolbar_integration_module_enabled || !$toolbar_integration_setting_enabled) { unset($form['toolbar_integration']); - // Add a message to inform the user that the toolbar integration is not enabled. We can - // inform them of the environment_indicator_toolbar module. + // Add a message to inform the user that the toolbar integration is not + // enabled. We can inform them of the environment_indicator_toolbar + // module. $form['toolbar_integration_message'] = [ '#type' => 'item', '#markup' => $this->t('The <strong>Environment Indicator Toolbar</strong> module is not enabled. Please enable it to use toolbar integration.'), diff --git a/src/Service/SwitcherManager.php b/src/Service/SwitcherManager.php index 15cec074..9b851b01 100644 --- a/src/Service/SwitcherManager.php +++ b/src/Service/SwitcherManager.php @@ -7,6 +7,7 @@ namespace Drupal\environment_indicator\Service; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Url; +use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Manages active environment switcher entities and builds UI-ready link data. @@ -31,6 +32,7 @@ use Drupal\Core\Url; * or entity loading into dedicated services or helpers. */ class SwitcherManager { + use StringTranslationTrait; /** * The entity type manager service. -- GitLab From 6207a822455bea4cdf9a585cc405faa32ef3d228 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 08:42:28 -0700 Subject: [PATCH 17/56] More clean up --- environment_indicator.module | 6 +- environment_indicator.services.yml | 7 +- .../environment_indicator_toolbar.module | 113 ++++-- ...environment_indicator_toolbar.services.yml | 12 - .../src/ToolbarHandler.php | 373 ------------------ src/Entity/EnvironmentIndicator.php | 28 -- src/Service/EnvironmentIndicator.php | 67 ++++ src/Service/SwitcherManager.php | 111 ------ src/ToolbarHandler.php | 18 +- 9 files changed, 152 insertions(+), 583 deletions(-) delete mode 100644 modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml delete mode 100644 modules/environment_indicator_toolbar/src/ToolbarHandler.php delete mode 100644 src/Service/SwitcherManager.php diff --git a/environment_indicator.module b/environment_indicator.module index b9579594..e085232a 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -132,7 +132,7 @@ function environment_indicator_page_top(array &$page_top) { } // Only add the environment indicator switcher if there are environments to // switch to. - if ($items = \Drupal::service('environment_indicator.switcher_manager')->getLinks()) { + if ($items = \Drupal::service('environment_indicator.indicator')->getLinks()) { $page_top['indicator']['switcher'] = [ '#theme' => 'links', '#links' => $items, @@ -148,7 +148,7 @@ function environment_indicator_page_top(array &$page_top) { ]; $page_top['indicator'] += [ '#cache' => [ - 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], \Drupal::service('environment_indicator.switcher_manager')->getCacheTags()), + 'tags' => Cache::mergeTags(['config:environment_indicator.settings'], \Drupal::service('environment_indicator.indicator')->getCacheTags()), ], ]; } @@ -275,7 +275,7 @@ function _environment_indicator_external_integration_is_enabled(string $integrat * The cache tags. * * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. - * Use \Drupal\environment_indicator\Service\SwitcherManager::getCacheTags(). + * Use \Drupal\environment_indicator\Service\EnvironmentIndicator::getCacheTags(). * * @see https://www.drupal.org/node/3526893 */ diff --git a/environment_indicator.services.yml b/environment_indicator.services.yml index 3f6e5ccf..eeeca3ce 100644 --- a/environment_indicator.services.yml +++ b/environment_indicator.services.yml @@ -9,15 +9,10 @@ services: - '@settings' - '@entity_type.manager' - '@environment_indicator.indicator' - - '@environment_indicator.switcher_manager' environment_indicator.indicator: class: Drupal\environment_indicator\Service\EnvironmentIndicator arguments: - '@config.factory' + - '@entity_type.manager' - '@state' - '@settings' - environment_indicator.switcher_manager: - class: Drupal\environment_indicator\Service\SwitcherManager - arguments: - - '@entity_type.manager' - - '@current_route_match' diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module index 4938a829..2c1980e8 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module @@ -5,46 +5,89 @@ * Module implementation file. */ +use Drupal\Core\Cache\Cache; +use Drupal\Core\Url; + /** * Implements hook_toolbar(). */ function environment_indicator_toolbar_toolbar() { - return \Drupal::service('environment_indicator_toolbar.toolbar_handler') - ->toolbar(); -} + $config = \Drupal::config('environment_indicator.settings'); + $activeEnvironment = \Drupal::config('environment_indicator.indicator'); + $environmentIndicator = \Drupal::service('environment_indicator.indicator'); + $account = \Drupal::currentUser(); + $items = []; + $items['environment_indicator'] = [ + '#cache' => [ + 'contexts' => ['user.permissions'], + ], + ]; + $title = $environmentIndicator->getTitle(); + $activeEnvironment = $config->get('environment_indicator.indicator'); + $name = $activeEnvironment->get('name'); + $items['environment_indicator'] += [ + '#type' => 'toolbar_item', + '#weight' => 125, + 'tab' => [ + '#type' => 'link', + '#title' => $title, + '#url' => Url::fromRoute('environment_indicator.settings'), + '#attributes' => [ + 'title' => t('Environments'), + 'class' => ['toolbar-icon', 'toolbar-icon-environment'], + ], + '#access' => !empty($name) && $account->hasPermission('access environment indicator'), + ], + 'tray' => [ + '#heading' => t('Environments menu'), + ], + '#attached' => [ + 'library' => [ + 'environment_indicator_toolbar/toolbar', + ], + 'drupalSettings' => [ + 'environmentIndicator' => [ + 'name' => $name, + 'fgColor' => $activeEnvironment->get('fg_color'), + 'bgColor' => $activeEnvironment->get('bg_color'), + 'toolbars' => ['toolbar' => 'toolbar'], + ], + ], + ], + ]; + if ($config->get('favicon')) { + $items['environment_indicator']['#attached']['drupalSettings']['environmentIndicator']['addFavicon'] = $config->get('favicon'); + $items['environment_indicator']['#attached']['library'][] = 'environment_indicator/favicon'; + } + // Add cache tags to the toolbar item while preserving context. + $items['environment_indicator']['#cache']['tags'] = Cache::mergeTags( + [ + 'config:environment_indicator.settings', + 'config:environment_indicator.indicator', + ], + $environmentIndicator->getCacheTags() + ); + if ($account->hasPermission('administer environment indicator settings')) { + $items['environment_indicator']['tray']['configuration'] = [ + '#type' => 'link', + '#title' => t('Configure'), + '#url' => Url::fromRoute('environment_indicator.settings'), + '#options' => [ + 'attributes' => ['class' => ['edit-environments']], + ], + ]; + } -/** - * Helper function that generates the environment switcher links. - * - * @return array - * A renderable array with the links. - */ -function _environment_indicator_toolbar_switcher_links(): array { - return \Drupal::service('environment_indicator_toolbar.toolbar_handler') - ->getLinks(); -} + if ($links = $environmentIndicator->getLinks()) { + $items['environment_indicator']['tray']['environment_links'] = [ + '#theme' => 'links__toolbar_shortcuts', + '#links' => $links, + '#attributes' => [ + 'class' => ['toolbar-menu'], + ], + ]; + } -/** - * Helper function that checks if there is external integration. - * - * @param string $integration - * Name of the integration: toolbar, admin_menu, ... - * - * @return bool - * TRUE if integration is enabled. FALSE otherwise. - */ -function _environment_indicator_toolbar_external_integration_is_enabled(string $integration): bool { - return \Drupal::service('environment_indicator_toolbar.toolbar_handler') - ->externalIntegration($integration); + return $items; } -/** - * Get the cache tags for the environment indicator switcher. - * - * @return string[] - * The cache tags. - */ -function _environment_indicator_toolbar_switcher_cache_tags(): array { - return \Drupal::service('environment_indicator_toolbar.toolbar_handler') - ->getCacheTags(); -} diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml deleted file mode 100644 index 6097e829..00000000 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.services.yml +++ /dev/null @@ -1,12 +0,0 @@ -services: - environment_indicator_toolbar.toolbar_handler: - class: Drupal\environment_indicator_toolbar\Service\ToolbarHandler - arguments: - - '@module_handler' - - '@config.factory' - - '@current_user' - - '@state' - - '@settings' - - '@entity_type.manager' - - '@environment_indicator.indicator' - - '@environment_indicator.switcher_manager' diff --git a/modules/environment_indicator_toolbar/src/ToolbarHandler.php b/modules/environment_indicator_toolbar/src/ToolbarHandler.php deleted file mode 100644 index 72e0375f..00000000 --- a/modules/environment_indicator_toolbar/src/ToolbarHandler.php +++ /dev/null @@ -1,373 +0,0 @@ -<?php - -namespace Drupal\environment_indicator_toolbar\Service; - -use Drupal\Core\Cache\Cache; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Config\ImmutableConfig; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Session\AccountProxyInterface; -use Drupal\Core\Site\Settings; -use Drupal\Core\State\StateInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\Url; -use Drupal\environment_indicator\Entity\EnvironmentIndicator; - -/** - * Toolbar integration handler. - */ -class ToolbarHandler { - - use StringTranslationTrait; - - /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected ModuleHandlerInterface $moduleHandler; - - /** - * The environment indicator config. - * - * @var \Drupal\Core\Config\ImmutableConfig - */ - protected ImmutableConfig $config; - - /** - * The active environment. - * - * @var \Drupal\Core\Config\ImmutableConfig - */ - protected ImmutableConfig $activeEnvironment; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountProxyInterface - */ - protected AccountProxyInterface $account; - - /** - * The state system. - * - * @var \Drupal\Core\State\StateInterface - */ - protected StateInterface $state; - - /** - * Drupal settings. - * - * @var \Drupal\Core\Site\Settings - */ - protected Settings $settings; - - /** - * Entity Type Manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected EntityTypeManagerInterface $entityTypeManager; - - /** - * ToolbarHandler constructor. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The config factory. - * @param \Drupal\Core\Session\AccountProxyInterface $account - * The current user. - * @param \Drupal\Core\State\StateInterface $state - * The state system. - * @param \Drupal\Core\Site\Settings $settings - * The settings. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - */ - public function __construct( - ModuleHandlerInterface $module_handler, - ConfigFactoryInterface $config_factory, - AccountProxyInterface $account, - StateInterface $state, - Settings $settings, - EntityTypeManagerInterface $entity_type_manager, - ) { - $this->moduleHandler = $module_handler; - $this->config = $config_factory->get('environment_indicator.settings'); - $this->activeEnvironment = $config_factory->get('environment_indicator.indicator'); - $this->account = $account; - $this->state = $state; - $this->settings = $settings; - $this->entityTypeManager = $entity_type_manager; - } - - /** - * User can access all indicators. - * - * @return bool - * TRUE on success, FALSE on failure. - */ - public function hasAccessAll(): bool { - return $this->account->hasPermission('access environment indicator'); - } - - /** - * User can access a specific indicator. - * - * @param object $environment - * The environment identifier. - * - * @return bool - * TRUE on success, FALSE on failure. - */ - public function hasAccessEnvironment($environment): bool { - return $this->hasAccessAll() || $this->account->hasPermission('access environment indicator ' . $environment); - } - - /** - * User can access the indicator for the active environment. - * - * @return bool - * TRUE on success, FALSE on failure. - */ - public function hasAccessActiveEnvironment(): bool { - return $this->hasAccessEnvironment($this->activeEnvironment->get('machine')); - } - - /** - * Hook bridge. - * - * @return array - * The environment indicator toolbar items render array. - * - * @see hook_toolbar() - */ - public function toolbar(): array { - $items['environment_indicator'] = [ - '#cache' => [ - 'contexts' => ['user.permissions'], - ], - ]; - - if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { - - $title = $this->getTitle(); - - $items['environment_indicator'] += [ - '#type' => 'toolbar_item', - '#weight' => 125, - 'tab' => [ - '#type' => 'link', - '#title' => $title, - '#url' => Url::fromRoute('environment_indicator.settings'), - '#attributes' => [ - 'title' => $this->t('Environments'), - 'class' => ['toolbar-icon', 'toolbar-icon-environment'], - ], - '#access' => !empty($title), - ], - 'tray' => [ - '#heading' => $this->t('Environments menu'), - ], - '#attached' => [ - 'library' => [ - 'environment_indicator_toolbar/toolbar', - ], - 'drupalSettings' => [ - 'environmentIndicator' => [ - 'name' => $this->activeEnvironment->get('name') ?: ' ', - 'fgColor' => $this->activeEnvironment->get('fg_color'), - 'bgColor' => $this->activeEnvironment->get('bg_color'), - 'toolbars' => $this->config->get('toolbar_integration'), - ], - ], - ], - ]; - if ($this->config->get('favicon')) { - $items['environment_indicator']['#attached']['drupalSettings']['environmentIndicator']['addFavicon'] = $this->config->get('favicon'); - $items['environment_indicator']['#attached']['library'][] = 'environment_indicator/favicon'; - } - // Add cache tags to the toolbar item while preserving context. - $items['environment_indicator']['#cache']['tags'] = Cache::mergeTags( - [ - 'config:environment_indicator.settings', - 'config:environment_indicator.indicator', - ], - $this->getCacheTags() - ); - if ($this->account->hasPermission('administer environment indicator settings')) { - $items['environment_indicator']['tray']['configuration'] = [ - '#type' => 'link', - '#title' => $this->t('Configure'), - '#url' => Url::fromRoute('environment_indicator.settings'), - '#options' => [ - 'attributes' => ['class' => ['edit-environments']], - ], - ]; - } - - if ($links = $this->getLinks()) { - $items['environment_indicator']['tray']['environment_links'] = [ - '#theme' => 'links__toolbar_shortcuts', - '#links' => $links, - '#attributes' => [ - 'class' => ['toolbar-menu'], - ], - ]; - } - } - - return $items; - } - - /** - * Retrieve value from the selected version identifier source. - * - * @return string|null - * The current release identifier as a string, or NULL if not available. - */ - public function getCurrentRelease(): ?string { - $version_identifier = $this->config->get('version_identifier') ?? 'environment_indicator_current_release'; - $version_identifier_fallback = $this->config->get('version_identifier_fallback') ?? 'deployment_identifier'; - - $release = $this->getVersionIdentifier($version_identifier); - if ($release !== NULL) { - return $release; - } - - if ($version_identifier !== $version_identifier_fallback) { - return $this->getVersionIdentifier($version_identifier_fallback); - } - - return NULL; - } - - /** - * Helper function to get version identifier based on the type. - * - * @param string $type - * The type of version identifier. - * - * @return string|null - * The version identifier. - */ - protected function getVersionIdentifier(string $type): ?string { - switch ($type) { - case 'environment_indicator_current_release': - $current_release = $this->state->get('environment_indicator.current_release'); - return $current_release !== NULL ? (string) $current_release : NULL; - - case 'deployment_identifier': - $deployment_identifier = $this->settings->get('deployment_identifier'); - return $deployment_identifier !== NULL ? (string) $deployment_identifier : NULL; - - case 'drupal_version': - return \Drupal::VERSION; - - case 'none': - default: - return NULL; - } - } - - /** - * Construct the title for the active environment. - * - * @return string|null - * The constructed title, including the release if available, or NULL if the - * environment name is not set. - */ - public function getTitle(): ?string { - $environment = $this->activeEnvironment->get('name'); - $release = $this->getCurrentRelease(); - return ($release) ? '(' . $release . ') ' . $environment : $environment; - } - - /** - * Helper function that checks if there is external integration. - * - * @param string $integration - * Name of the integration: toolbar, admin_menu, ... - * - * @return bool - * TRUE if integration is enabled. FALSE otherwise. - */ - public function externalIntegration($integration): bool { - if ($integration == 'toolbar') { - if ($this->moduleHandler->moduleExists('toolbar')) { - $toolbar_integration = $this->config->get('toolbar_integration') ?? []; - if (in_array('toolbar', $toolbar_integration)) { - if ($this->account->hasPermission('access toolbar')) { - return TRUE; - } - } - } - } - return FALSE; - } - - /** - * Get the cache tags for the environment indicator switcher. - * - * @return array - * The cache tags. - */ - public function getCacheTags(): array { - return $this->entityTypeManager->getDefinition('environment_indicator')->getListCacheTags(); - } - - /** - * Get all the links for the switcher. - * - * @return array - * Returns all the links. - */ - public function getLinks(): array { - if (!$environment_entities = EnvironmentIndicator::loadMultiple()) { - return []; - } - - $current = Url::fromRoute('<current>'); - $current_path = $current->toString(); - $url = parse_url($current_path); - $path = $url['path']; - if (isset($url['query'])) { - $path .= '?' . $url['query']; - } - $environment_entities = array_filter( - $environment_entities, - function (EnvironmentIndicator $entity) { - return $entity->status() - && !empty($entity->getUrl()) - && $this->hasAccessEnvironment($entity->id()); - } - ); - - $links = array_map( - function (EnvironmentIndicator $entity) use ($path) { - return [ - 'attributes' => [ - 'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $entity->getBgColor() . ';', - 'title' => $this->t('Opens the current page in the selected environment.'), - ], - 'title' => $this->t('Open on @label', ['@label' => $entity->label()]), - 'url' => Url::fromUri($entity->getUrl() . $path), - 'type' => 'link', - 'weight' => $entity->getWeight(), - ]; - }, - $environment_entities - ); - - if (!$links) { - return []; - } - - uasort($links, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); - - return $links; - } - -} diff --git a/src/Entity/EnvironmentIndicator.php b/src/Entity/EnvironmentIndicator.php index 501dc8f1..8b8c1f22 100644 --- a/src/Entity/EnvironmentIndicator.php +++ b/src/Entity/EnvironmentIndicator.php @@ -201,34 +201,6 @@ class EnvironmentIndicator extends ConfigEntityBase implements ConfigEntityInter */ public function setBgColor($bg_color) { $this->set('bg_color', $bg_color); - - } - - /** - * Gets the links for the environment switcher. - * - * @return array - * An array of environment links. - */ - public function getLinks(): array { - $links = []; - $current_path = Url::fromRoute('<current>')->toString(); - - // Build the link for the current environment. - if ($this->status() && !empty($this->getUrl())) { - $links[] = [ - 'attributes' => [ - 'style' => 'color: ' . $this->getFgColor() . '; background-color: ' . $this->getBgColor() . ';', - 'title' => $this->t('Opens the current page in the selected environment.'), - ], - 'title' => $this->t('Open on @label', ['@label' => $this->label()]), - 'url' => Url::fromUri($this->getUrl() . $current_path), - 'type' => 'link', - 'weight' => $this->getWeight(), - ]; - } - - return $links; } } diff --git a/src/Service/EnvironmentIndicator.php b/src/Service/EnvironmentIndicator.php index de55e3c0..d427527e 100644 --- a/src/Service/EnvironmentIndicator.php +++ b/src/Service/EnvironmentIndicator.php @@ -7,11 +7,15 @@ namespace Drupal\environment_indicator\Service; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Site\Settings; use Drupal\Core\State\StateInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Url; /** * Provides contextual info about the active environment indicator. */ class EnvironmentIndicator { + use StringTranslationTrait; /** * The config factory service. @@ -20,6 +24,13 @@ class EnvironmentIndicator { */ protected ConfigFactoryInterface $configFactory; + /** + * The entity type manager service. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected EntityTypeManagerInterface $entityTypeManager; + /** * The state service. * @@ -39,6 +50,8 @@ class EnvironmentIndicator { * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager service. * @param \Drupal\Core\State\StateInterface $state * The state service. * @param \Drupal\Core\Site\Settings $settings @@ -46,10 +59,12 @@ class EnvironmentIndicator { */ public function __construct( ConfigFactoryInterface $config_factory, + EntityTypeManagerInterface $entity_type_manager, StateInterface $state, Settings $settings, ) { $this->configFactory = $config_factory; + $this->entityTypeManager = $entity_type_manager; $this->state = $state; $this->settings = $settings; } @@ -102,4 +117,56 @@ class EnvironmentIndicator { return $environment ? ($release ? "($release) $environment" : $environment) : NULL; } + /** + * 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 = Url::fromRoute('<current>'); + $current_path = $current->toString(); + $url = parse_url($current_path); + $path = $url['path']; + if (isset($url['query'])) { + $path .= '?' . $url['query']; + } + + $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' => $this->t('Opens the current page in the selected environment.'), + ], + 'title' => $this->t('Open on @label', ['@label' => $entity->label()]), + 'url' => Url::fromUri($entity->getUrl() . $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(); + } + } diff --git a/src/Service/SwitcherManager.php b/src/Service/SwitcherManager.php deleted file mode 100644 index 9b851b01..00000000 --- a/src/Service/SwitcherManager.php +++ /dev/null @@ -1,111 +0,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; -use Drupal\Core\StringTranslation\StringTranslationTrait; - -/** - * 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 { - use StringTranslationTrait; - - /** - * 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' => $this->t('Opens the current page in the selected environment.'), - ], - 'title' => $this->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(); - } - -} diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index c8210f56..ff091315 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -13,7 +13,6 @@ use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Drupal\environment_indicator\Service\EnvironmentIndicator; -use Drupal\environment_indicator\Service\SwitcherManager; @trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. See https://www.drupal.org/node/the-change-notice-nid', E_USER_DEPRECATED); @@ -85,13 +84,6 @@ class ToolbarHandler { */ protected EnvironmentIndicator $environmentIndicator; - /** - * The SwitcherManager service. - * - * @var \Drupal\environment_indicator\Service\SwitcherManager - */ - protected SwitcherManager $switcherManager; - /** * ToolbarHandler constructor. * @@ -109,8 +101,6 @@ class ToolbarHandler { * The entity type manager. * @param \Drupal\environment_indicator\Service\EnvironmentIndicator $environment_indicator * The environment indicator service. - * @param \Drupal\environment_indicator\Service\SwitcherManager $switcher_manager - * The switcher manager service. */ public function __construct( ModuleHandlerInterface $module_handler, @@ -120,7 +110,6 @@ class ToolbarHandler { Settings $settings, EntityTypeManagerInterface $entity_type_manager, EnvironmentIndicator $environment_indicator, - SwitcherManager $switcher_manager, ) { $this->moduleHandler = $module_handler; $this->config = $config_factory->get('environment_indicator.settings'); @@ -130,7 +119,6 @@ class ToolbarHandler { $this->settings = $settings; $this->entityTypeManager = $entity_type_manager; $this->environmentIndicator = $environment_indicator; - $this->switcherManager = $switcher_manager; } /** @@ -248,7 +236,7 @@ class ToolbarHandler { ]; } - if ($links = $this->switcherManager->getLinks()) { + if ($links = $this->environmentIndicator->getLinks()) { $items['environment_indicator']['tray']['environment_links'] = [ '#theme' => 'links__toolbar_shortcuts', '#links' => $links, @@ -374,7 +362,7 @@ class ToolbarHandler { * * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. * Use - * \Drupal::service('environment_indicator.switcher_manager')->getCacheTags() + * \Drupal::service('environment_indicator.indicator')->getCacheTags() * instead. * @see https://www.drupal.org/node/3526893 */ @@ -390,7 +378,7 @@ class ToolbarHandler { * * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. * Use - * \Drupal::service('environment_indicator.switcher_manager')->getLinks() + * \Drupal::service('environment_indicator.indicator')->getLinks() * instead. * @see https://www.drupal.org/node/3526893 */ -- GitLab From c9911c2203f5688fdb0c054ade5290a77eceb282 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 17:49:13 -0700 Subject: [PATCH 18/56] Update schema nid --- .../schema/environment_indicator.schema.yml | 2 +- css/environment_indicator.css | 74 +++++++++++++++++++ environment_indicator.module | 31 +++++++- js/environment_indicator.js | 68 +++++++++++++++++ .../css/toolbar.css | 4 + .../environment_indicator_toolbar.module | 1 - src/Form/EnvironmentIndicatorSettingsForm.php | 26 ++++--- src/ToolbarHandler.php | 15 +++- 8 files changed, 199 insertions(+), 22 deletions(-) diff --git a/config/schema/environment_indicator.schema.yml b/config/schema/environment_indicator.schema.yml index af6697a6..ac686f2c 100644 --- a/config/schema/environment_indicator.schema.yml +++ b/config/schema/environment_indicator.schema.yml @@ -36,7 +36,7 @@ environment_indicator.settings: sequence: type: string label: 'Toolbar identifier' - deprecated: "The 'toolbar_integration' config schema is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Enable the environment_indicator_toolbar module for toolbar integration. See http://drupal.org/node/the-change-notice-drerp." + deprecated: "The 'toolbar_integration' config schema is deprecated in environment_indicator:4.0.22 and and is removed from environment_indicator:5.0.0. Use submodules for integrations instead. See http://drupal.org/node/3526893." favicon: type: boolean label: 'Show a colored favicon for environment' diff --git a/css/environment_indicator.css b/css/environment_indicator.css index 2ec3f288..ee67f362 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -70,3 +70,77 @@ background-color: #edede0; font-size: 1em; } + +/* @todo: Remove everything below this comment in environment_indicator:5.0.0. */ +.toolbar .toolbar-bar .toolbar-tab > a.toolbar-icon.is-active::before { + filter: invert(0%); +} + +.toolbar .toolbar-bar .toolbar-tab > a.toolbar-icon.is-active { + background-image: linear-gradient( + rgba(255, 255, 255, 0.25) 20%, + transparent 200% + ); +} + +.toolbar-bar .toolbar-icon-environment::before { + background-image: url("../images/env-bebebe.svg"); +} +.no-svg .toolbar-bar .toolbar-icon-environment::before { + background-image: url("../images/env-bebebe.png"); +} +.toolbar-bar .toolbar-icon-environment.is-active::before { + background-image: url("../images/env-ffffff.svg"); +} +.no-svg .toolbar-bar .toolbar-icon-environment.is-active::before { + background-image: url("../images/env-ffffff.png"); +} +.toolbar .toolbar-tray-vertical .edit-environments { + padding: 1em; + text-align: right; +} +.toolbar .toolbar-tray-horizontal .edit-environments { + float: right; +} + +/* Style fixes for gin_toolbar */ +.gin--vertical-toolbar .toolbar-menu-administration { + border-left: var(--environment-indicator-border-width) solid; +} + +.gin--horizontal-toolbar #toolbar-item-administration-tray { + border-top: var(--environment-indicator-border-width) solid; + border-bottom: 0; +} + +.gin--horizontal-toolbar .gin-secondary-toolbar { + margin-top: var(--environment-indicator-border-width); +} + +.gin--vertical-toolbar + .toolbar-menu-administration + > .toolbar-menu + > .menu-item + .toolbar-menu { + margin-inline-start: calc( + /* stylelint-disable custom-property-pattern */ + /* See https://www.drupal.org/i/3309113 */ + /* See https://www.drupal.org/i/3524015 */ + var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px + /* stylelint-enable custom-property-pattern */ + ); +} + +.gin--vertical-toolbar[data-toolbar-menu="open"] + .toolbar-menu-administration + > .toolbar-menu + > .menu-item + .toolbar-menu { + margin-inline-start: calc( + /* stylelint-disable custom-property-pattern */ + /* See https://www.drupal.org/i/3309113 */ + /* See https://www.drupal.org/i/3524015 */ + var(--gin-toolbar-width, var(--ginToolbarWidth)) - 4px + /* stylelint-enable custom-property-pattern */ + ); +} diff --git a/environment_indicator.module b/environment_indicator.module index e085232a..e2a22b69 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -77,7 +77,13 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc function environment_indicator_preprocess_html(&$variables) { // Check if environment_indicator_toolbar is enabled. $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; - $toolbar_integration_setting_enabled = \Drupal::config('environment_indicator.settings')->get('toolbar_integration')[0] ?? FALSE; + // @todo Remove this check in environment_indicator:5.0.0. + // This is a temporary solution to avoid breaking existing sites that use + // the environment_indicator_toolbar module. + // In the future, we will rely on the environment_indicator_toolbar module + // to handle the toolbar integration, and this check will no longer be needed. + // @see https://www.drupal.org/node/3454735 + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; } @@ -96,9 +102,18 @@ function environment_indicator_preprocess_html(&$variables) { function environment_indicator_page_top(array &$page_top) { // Check if environment_indicator_toolbar is enabled. $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; - $toolbar_integration_setting_enabled = \Drupal::config('environment_indicator.settings')->get('toolbar_integration')[0] ?? FALSE; - if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { - return; + if($toolbar_integration_module_enabled) { + return $page_top; + } + // @todo Remove this check in environment_indicator:5.0.0. + // This is a temporary solution to avoid breaking existing sites that use + // the environment_indicator_toolbar module. + // In the future, we will rely on the environment_indicator_toolbar module + // to handle the toolbar integration, and this check will no longer be needed. + // @see https://www.drupal.org/node/3454735 + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); + if ($toolbar_integration_setting_enabled) { + return $page_top; } $active_environment = \Drupal::config('environment_indicator.indicator'); $title = $active_environment->get('name'); @@ -224,8 +239,16 @@ function _environment_indicator_parse_style(string $style): array { /** * Implements hook_toolbar(). + * + * @todo Remove this in environment_indicator:5.0.0. */ function environment_indicator_toolbar() { + // Check if environment_indicator_toolbar is enabled. + $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; + if ($toolbar_integration_module_enabled) { + return []; + } + // @todo Remove this check in environment_indicator:5.0.0. return \Drupal::service('environment_indicator.toolbar_handler') ->toolbar(); } diff --git a/js/environment_indicator.js b/js/environment_indicator.js index b5d42141..2c94cc72 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -16,4 +16,72 @@ }); }, }; + + /** @todo Remove this function in environment_indicator 5.0.0 */ + Drupal.behaviors.environmentIndicatorToolbar = { + attach(context, settings) { + if (settings.environmentIndicator !== undefined) { + const $body = $('body'); + const borderWidth = + getComputedStyle(document.body).getPropertyValue( + '--environment-indicator-border-width', + ) || '6px'; + // Set environment color if not using gin_toolbar. + if ( + settings.environmentIndicator.toolbars.toolbar === 'toolbar' && + !$body.hasClass('gin--vertical-toolbar') && + !$body.hasClass('gin--horizontal-toolbar') + ) { + // Replaced $.css with direct style assignment using JavaScript. + document.querySelector('#toolbar-bar').style.backgroundColor = + settings.environmentIndicator.bgColor; + document + .querySelectorAll('#toolbar-bar .toolbar-item a:not(.is-active)') + .forEach((el) => { + el.style.color = settings.environmentIndicator.fgColor; + return el; + }); + } + + // Set environment color for gin_toolbar vertical toolbar. + if ($body.hasClass('gin--vertical-toolbar')) { + document.querySelector( + '.toolbar-menu-administration', + ).style.borderLeftColor = settings.environmentIndicator.bgColor; + document.querySelector( + '.toolbar-menu-administration', + ).style.borderLeftWidth = borderWidth; + document + .querySelectorAll( + '.toolbar-tray-horizontal .toolbar-menu li.menu-item', + ) + .forEach((el) => { + el.style.marginLeft = + 'calc(var(--environment-indicator-border-width) * -0.5)'; + return el; + }); + } + + // Set environment color for gin_toolbar horizontal toolbar. + if ($body.hasClass('gin--horizontal-toolbar')) { + document.querySelector( + '#toolbar-item-administration-tray', + ).style.borderTopColor = settings.environmentIndicator.bgColor; + document.querySelector( + '#toolbar-item-administration-tray', + ).style.borderTopWidth = borderWidth; + } + + // Set environment color on the icon of the gin_toolbar + if ( + $body.hasClass('gin--horizontal-toolbar') || + $body.hasClass('gin--vertical-toolbar') + ) { + $('head', context).append( + `<style>.toolbar .toolbar-bar #toolbar-item-administration-tray a.toolbar-icon-admin-toolbar-tools-help.toolbar-icon-default::before{ background-color: ${settings.environmentIndicator.bgColor} }</style>`, + ); + } + } + }, + }; })(jQuery, Drupal, once); diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index 9e08e570..71e6e48d 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -1,3 +1,7 @@ +:root { + --environment-indicator-border-width: 6px; +} + .toolbar .toolbar-bar .toolbar-tab>a.toolbar-icon.is-active::before { filter: invert(0%); } diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module index 2c1980e8..67bb24d8 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module @@ -23,7 +23,6 @@ function environment_indicator_toolbar_toolbar() { ], ]; $title = $environmentIndicator->getTitle(); - $activeEnvironment = $config->get('environment_indicator.indicator'); $name = $activeEnvironment->get('name'); $items['environment_indicator'] += [ '#type' => 'toolbar_item', diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index c2d74a55..9c41c8fe 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -67,31 +67,29 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('environment_indicator.settings'); $toolbar_integration_module_enabled = $this->moduleHandler->moduleExists('environment_indicator_toolbar') ?? FALSE; - $toolbar_integration_setting_enabled = $config->get('toolbar_integration')[0] ?? FALSE; - + $toolbar_integration_setting_enabled = !empty($config->get('toolbar_integration')['toolbar']); $form = parent::buildForm($form, $form_state); $form['toolbar_integration'] = [ '#type' => 'checkboxes', - '#title' => $this->t('Toolbar integration'), + '#title' => $this->t('Toolbar integration (Deprecated)'), '#options' => [ - 'toolbar' => $this->t('Toolbar'), + 'toolbar' => $this->t('Toolbar (Deprecated)'), ], - '#description' => $this->t('Select the toolbars that you want to integrate with.'), + '#description' => $this->t('This setting is deprecated and will be removed in a future release. Please use the Environment Indicator Toolbar module instead.'), '#default_value' => $config->get('toolbar_integration') ?: [], ]; // If the toolbar integration settings are empty, we remove the // toolbar integration settings from the form. - if (!$toolbar_integration_module_enabled || !$toolbar_integration_setting_enabled) { + if (!$toolbar_integration_setting_enabled) { unset($form['toolbar_integration']); - // Add a message to inform the user that the toolbar integration is not - // enabled. We can inform them of the environment_indicator_toolbar - // module. + } + // If the toolbar integration module is not enabled, we add a message to inform the user. + if (!$toolbar_integration_module_enabled) { $form['toolbar_integration_message'] = [ '#type' => 'item', '#markup' => $this->t('The <strong>Environment Indicator Toolbar</strong> module is not enabled. Please enable it to use toolbar integration.'), ]; } - $form['favicon'] = [ '#type' => 'checkbox', '#title' => $this->t('Show favicon'), @@ -166,14 +164,18 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config('environment_indicator.settings'); - $config->set('toolbar_integration', array_filter($form_state->getValue('toolbar_integration'))) + $config ->set('favicon', $form_state->getValue('favicon')) ->set('version_identifier', $form_state->getValue('version_identifier')) ->set('version_identifier_fallback', $form_state->getValue('version_identifier_fallback')) ->save(); - parent::submitForm($form, $form_state); + if (isset($form['toolbar_integration'])) { + $config->set('toolbar_integration', array_filter($form_state->getValue('toolbar_integration'))) + ->save(); + } + parent::submitForm($form, $form_state); } } diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index ff091315..d03cba43 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -184,7 +184,14 @@ class ToolbarHandler { if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { $title = $this->getTitle(); - + // This must be done for backward compatibility. Some versions of Drupal + // have the toolbar integration setting as an array of arrays like: + // [['toolbar' => 'toolbar']], while others have it as an array of strings + // like ['toolbar']. + $toolbars_setting = $this->config->get('toolbar_integration') ?? []; + if (!empty($toolbars_setting)) { + $toolbars_setting = [['toolbar' => 'toolbar']]; + } $items['environment_indicator'] += [ '#type' => 'toolbar_item', '#weight' => 125, @@ -208,7 +215,7 @@ class ToolbarHandler { 'name' => $this->activeEnvironment->get('name') ?: ' ', 'fgColor' => $this->activeEnvironment->get('fg_color'), 'bgColor' => $this->activeEnvironment->get('bg_color'), - 'toolbars' => $this->config->get('toolbar_integration'), + 'toolbars' => $toolbars_setting, ], ], ], @@ -364,7 +371,7 @@ class ToolbarHandler { * Use * \Drupal::service('environment_indicator.indicator')->getCacheTags() * instead. - * @see https://www.drupal.org/node/3526893 + * @see https://www.drupal.org/node/3526812 */ public function getCacheTags(): array { return $this->entityTypeManager->getDefinition('environment_indicator')->getListCacheTags(); @@ -380,7 +387,7 @@ class ToolbarHandler { * Use * \Drupal::service('environment_indicator.indicator')->getLinks() * instead. - * @see https://www.drupal.org/node/3526893 + * @see https://www.drupal.org/node/3526812 */ public function getLinks(): array { if (!$environment_entities = EnvironmentIndicator::loadMultiple()) { -- GitLab From 574dd90c5dc222e7b3fe62aa822e584bb684e9d7 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 18:04:32 -0700 Subject: [PATCH 19/56] PHPCS fixes --- environment_indicator.module | 11 ++++++----- .../environment_indicator_toolbar.module | 1 - src/Entity/EnvironmentIndicator.php | 1 - src/Form/EnvironmentIndicatorSettingsForm.php | 3 ++- src/ToolbarHandler.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index e2a22b69..8675e391 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -82,7 +82,7 @@ function environment_indicator_preprocess_html(&$variables) { // the environment_indicator_toolbar module. // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. - // @see https://www.drupal.org/node/3454735 + // @see https://www.drupal.org/i/3484735 $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; @@ -102,7 +102,7 @@ function environment_indicator_preprocess_html(&$variables) { function environment_indicator_page_top(array &$page_top) { // Check if environment_indicator_toolbar is enabled. $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; - if($toolbar_integration_module_enabled) { + if ($toolbar_integration_module_enabled) { return $page_top; } // @todo Remove this check in environment_indicator:5.0.0. @@ -110,7 +110,7 @@ function environment_indicator_page_top(array &$page_top) { // the environment_indicator_toolbar module. // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. - // @see https://www.drupal.org/node/3454735 + // @see https://www.drupal.org/i/3484735 $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); if ($toolbar_integration_setting_enabled) { return $page_top; @@ -284,7 +284,7 @@ function _environment_indicator_switcher_links(): array { * Then you can replicate this functionality by using the * core.extension.service to check if the module is enabled. * - * @see https://www.drupal.org/node/ spllingsljldskajflj + * @see https://www.drupal.org/project/environment_indicator/issues/3484735 */ function _environment_indicator_external_integration_is_enabled(string $integration): bool { return \Drupal::service('environment_indicator.toolbar_handler') @@ -298,7 +298,8 @@ function _environment_indicator_external_integration_is_enabled(string $integrat * The cache tags. * * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. - * Use \Drupal\environment_indicator\Service\EnvironmentIndicator::getCacheTags(). + * Use + * \Drupal\environment_indicator\Service\EnvironmentIndicator::getCacheTags(). * * @see https://www.drupal.org/node/3526893 */ diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module index 67bb24d8..5fd6045a 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.module +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.module @@ -89,4 +89,3 @@ function environment_indicator_toolbar_toolbar() { return $items; } - diff --git a/src/Entity/EnvironmentIndicator.php b/src/Entity/EnvironmentIndicator.php index 8b8c1f22..d384ff32 100644 --- a/src/Entity/EnvironmentIndicator.php +++ b/src/Entity/EnvironmentIndicator.php @@ -4,7 +4,6 @@ namespace Drupal\environment_indicator\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityInterface; -use Drupal\Core\Url; /** * Defines a Environment configuration entity. diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index 9c41c8fe..d9f0100f 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -83,7 +83,8 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { if (!$toolbar_integration_setting_enabled) { unset($form['toolbar_integration']); } - // If the toolbar integration module is not enabled, we add a message to inform the user. + // If the toolbar integration module is not enabled, we add a message to + // inform the user. if (!$toolbar_integration_module_enabled) { $form['toolbar_integration_message'] = [ '#type' => 'item', diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index d03cba43..ebea08b2 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -14,7 +14,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Drupal\environment_indicator\Service\EnvironmentIndicator; -@trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. See https://www.drupal.org/node/the-change-notice-nid', E_USER_DEPRECATED); +@trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. See https://www.drupal.org/node/3526893', E_USER_DEPRECATED); /** * Toolbar integration handler. @@ -22,7 +22,7 @@ use Drupal\environment_indicator\Service\EnvironmentIndicator; * @deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Use * \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. * - * @see https://www.drupal.org/node/the-change-notice-nid + * @see https://www.drupal.org/node/3526893 */ class ToolbarHandler { -- GitLab From dafede5b211b58a00b1c8d38d53076c34adc4929 Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Sat, 31 May 2025 01:25:46 +0000 Subject: [PATCH 20/56] Update Toolbar Service Deprecation notice --- src/ToolbarHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index ebea08b2..30d89c89 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -14,7 +14,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Drupal\environment_indicator\Service\EnvironmentIndicator; -@trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator_toolbar\Service\ToolbarHandler. See https://www.drupal.org/node/3526893', E_USER_DEPRECATED); +@trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator\Service\EnvironmentIndicator or the environment_indicator_toolbar module. See https://www.drupal.org/node/3526893', E_USER_DEPRECATED); /** * Toolbar integration handler. -- GitLab From 274ec705995aa10f8395c7f79f0025ee45e132df Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Sat, 31 May 2025 01:27:38 +0000 Subject: [PATCH 21/56] Load environmentindicator entity as EnvironmentSwitcher --- src/ToolbarHandler.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index 30d89c89..3081cce7 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -12,6 +12,7 @@ use Drupal\Core\Site\Settings; use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; +use Drupal\environment_indicator\Entity\EnvironmentIndicator as EnvironmentSwitcher; use Drupal\environment_indicator\Service\EnvironmentIndicator; @trigger_error('The ' . __NAMESPACE__ . '\ToolbarHandler is deprecated in environment_indicator:4.0.22 and is removed from environment_indicator:5.0.0. Instead, use \Drupal\environment_indicator\Service\EnvironmentIndicator or the environment_indicator_toolbar module. See https://www.drupal.org/node/3526893', E_USER_DEPRECATED); @@ -390,7 +391,7 @@ class ToolbarHandler { * @see https://www.drupal.org/node/3526812 */ public function getLinks(): array { - if (!$environment_entities = EnvironmentIndicator::loadMultiple()) { + if (!$environment_entities = EnvironmentSwitcher::loadMultiple()) { return []; } @@ -403,7 +404,7 @@ class ToolbarHandler { } $environment_entities = array_filter( $environment_entities, - function (EnvironmentIndicator $entity) { + function (EnvironmentSwitcher $entity) { return $entity->status() && !empty($entity->getUrl()) && $this->hasAccessEnvironment($entity->id()); -- GitLab From f84928ee307879c68c821cda068de20e09d452e3 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 18:50:00 -0700 Subject: [PATCH 22/56] Fix module handler --- src/Form/EnvironmentIndicatorSettingsForm.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index d9f0100f..3d2aa000 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -34,7 +34,8 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { public function __construct( ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, - protected readonly ModuleHandlerInterface $module_handler, + ModuleHandlerInterface $module_handler, + ) { parent::__construct( $config_factory, -- GitLab From 1f101a1db7269a5189674566b0c9187c89e234ff Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 19:37:36 -0700 Subject: [PATCH 23/56] Add config --- src/Form/EnvironmentIndicatorSettingsForm.php | 1 - tests/src/Functional/ToolbarIntegrationTest.php | 14 ++++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index 3d2aa000..66f639f2 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -35,7 +35,6 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, ModuleHandlerInterface $module_handler, - ) { parent::__construct( $config_factory, diff --git a/tests/src/Functional/ToolbarIntegrationTest.php b/tests/src/Functional/ToolbarIntegrationTest.php index 2c9b5a8a..3ce811d2 100644 --- a/tests/src/Functional/ToolbarIntegrationTest.php +++ b/tests/src/Functional/ToolbarIntegrationTest.php @@ -72,6 +72,11 @@ class ToolbarIntegrationTest extends BrowserTestBase { $config = $this->config('system.performance'); $config->set('css.preprocess', FALSE)->save(); + // Set the toolbar integration setting to 'toolbar' by default. + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) + ->save(); + // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator', 'access toolbar']); $this->unprivilegedUser = $this->drupalCreateUser(); @@ -143,8 +148,9 @@ class ToolbarIntegrationTest extends BrowserTestBase { * We also test that the style tag is present with CSS variables. */ public function testEnvironmentIndicatorToolbarIntegration(): void { - $config = $this->config('environment_indicator.indicator'); - $config->set('name', 'Test Environment') + + $indicator_config = $this->config('environment_indicator.indicator'); + $indicator_config->set('name', 'Test Environment') ->set('fg_color', '#000000') ->set('bg_color', '#ffffff') ->save(); @@ -156,8 +162,8 @@ class ToolbarIntegrationTest extends BrowserTestBase { $session->pageTextContains('Test Environment'); $session->elementNotExists('css', '#environment-indicator'); // Change configuration values. - $config = $this->config('environment_indicator.indicator'); - $config->set('name', 'Development Environment') + $indicator_config = $this->config('environment_indicator.indicator'); + $indicator_config->set('name', 'Development Environment') ->set('fg_color', '#efefef') ->set('bg_color', '#12285f') ->save(); -- GitLab From 813a446f53f524f55ed4c2683a680e2aad15725a Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 19:42:59 -0700 Subject: [PATCH 24/56] Update test --- tests/src/FunctionalJavascript/ToolbarIntegrationTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index 625d3e20..6ef3fd55 100644 --- a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -60,6 +60,11 @@ class ToolbarIntegrationTest extends WebDriverTestBase { $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); + // Set the toolbar integration setting to 'toolbar' by default. + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) + ->save(); + // Disable CSS preprocessing. $config = $this->config('system.performance'); $config->set('css.preprocess', FALSE)->save(); -- GitLab From 431a21422afbb5c9864736bc768512d51f4a3990 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 21:39:55 -0700 Subject: [PATCH 25/56] Remove update function and add install function --- environment_indicator.install | 17 -------------- .../environment_indicator_toolbar.install | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 modules/environment_indicator_toolbar/environment_indicator_toolbar.install diff --git a/environment_indicator.install b/environment_indicator.install index 409018a7..3da415da 100644 --- a/environment_indicator.install +++ b/environment_indicator.install @@ -25,20 +25,3 @@ function environment_indicator_update_8101() { $settings->save(); } - -/** - * Ensure environment_indicator_toolbar module is installed. - */ -function environment_indicator_update_110401999() { - // Check if toolbar integration is enabled. - $config = \Drupal::config('environment_indicator.settings'); - if ($config->get('toolbar_integration')['toolbar'] === '0') { - return; - } - // List of dependencies. - $modules = [ - 'environment_indicator_toolbar', - 'toolbar', - ]; - \Drupal::service('module_installer')->install($modules); -} diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.install b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install new file mode 100644 index 00000000..492a3afc --- /dev/null +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install @@ -0,0 +1,22 @@ +<?php + +/** + * @file + * Install, update and uninstall functions. + */ + +/** + * Ensure toolbar_integration setting is emppty if it exists. + * + * Otherwise, do not change the configuration. + * This is here to help upgrade from versions of the Environment Indicator + * module that have a removed function for toolbar integration. + */ +function environment_indicator_toolbar_install() { + // Check if toolbar integration is enabled. + $config = \Drupal::config('environment_indicator.settings'); + if ($config->get('toolbar_integration')) { + $config->set('toolbar_integration', []); + $config->save(); + } +} -- GitLab From 927381c9ed29001b20fb293cb900f53e7d5a5e3b Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 21:47:57 -0700 Subject: [PATCH 26/56] Update spelling --- .../environment_indicator_toolbar.install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.install b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install index 492a3afc..3168f832 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.install +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install @@ -6,7 +6,7 @@ */ /** - * Ensure toolbar_integration setting is emppty if it exists. + * Ensure toolbar_integration setting is empty if it exists. * * Otherwise, do not change the configuration. * This is here to help upgrade from versions of the Environment Indicator -- GitLab From 1b3d7bd5e54de62d8d3c06374fe647c5b1d5475a Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 30 May 2025 21:53:03 -0700 Subject: [PATCH 27/56] Update install config --- config/install/environment_indicator.settings.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/install/environment_indicator.settings.yml b/config/install/environment_indicator.settings.yml index 21e84910..fbb315c6 100644 --- a/config/install/environment_indicator.settings.yml +++ b/config/install/environment_indicator.settings.yml @@ -1,5 +1,4 @@ -toolbar_integration: - - toolbar +toolbar_integration: { } favicon: true version_identifier: 'environment_indicator_current_release' version_identifier_fallback: 'deployment_identifier' -- GitLab From b20e2aa73573f1f9739d25fb7d166259a8bfc4ee Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Sat, 31 May 2025 21:04:21 +0000 Subject: [PATCH 28/56] Apply 1 suggestion(s) to 1 file(s) --- config/install/environment_indicator.settings.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/install/environment_indicator.settings.yml b/config/install/environment_indicator.settings.yml index fbb315c6..21e84910 100644 --- a/config/install/environment_indicator.settings.yml +++ b/config/install/environment_indicator.settings.yml @@ -1,4 +1,5 @@ -toolbar_integration: { } +toolbar_integration: + - toolbar favicon: true version_identifier: 'environment_indicator_current_release' version_identifier_fallback: 'deployment_identifier' -- GitLab From 139131e09d0b7a716c6e06b71b90ea42700f918e Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 10:51:44 -0700 Subject: [PATCH 29/56] Some fixes --- css/environment_indicator.css | 8 ++++---- environment_indicator.module | 8 ++++---- js/environment_indicator.js | 12 +++++++++--- .../environment_indicator_toolbar.install | 10 ++++++---- src/Form/EnvironmentIndicatorSettingsForm.php | 2 +- src/ToolbarHandler.php | 2 +- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index ee67f362..cf68c5ef 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -84,16 +84,16 @@ } .toolbar-bar .toolbar-icon-environment::before { - background-image: url("../images/env-bebebe.svg"); + background-image: url("../modules/environment_indicator_toolbar/images/env-bebebe.svg"); } .no-svg .toolbar-bar .toolbar-icon-environment::before { - background-image: url("../images/env-bebebe.png"); + background-image: url("../modules/environment_indicator_toolbar/images/env-bebebe.png"); } .toolbar-bar .toolbar-icon-environment.is-active::before { - background-image: url("../images/env-ffffff.svg"); + background-image: url("../modules/environment_indicator_toolbar/images/env-ffffff.svg"); } .no-svg .toolbar-bar .toolbar-icon-environment.is-active::before { - background-image: url("../images/env-ffffff.png"); + background-image: url("../modules/environment_indicator_toolbar/images/env-ffffff.png"); } .toolbar .toolbar-tray-vertical .edit-environments { padding: 1em; diff --git a/environment_indicator.module b/environment_indicator.module index 8675e391..eb3b75bb 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -83,7 +83,7 @@ function environment_indicator_preprocess_html(&$variables) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 - $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')); if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; } @@ -103,7 +103,7 @@ function environment_indicator_page_top(array &$page_top) { // Check if environment_indicator_toolbar is enabled. $toolbar_integration_module_enabled = \Drupal::moduleHandler()->moduleExists('environment_indicator_toolbar') ?? FALSE; if ($toolbar_integration_module_enabled) { - return $page_top; + return; } // @todo Remove this check in environment_indicator:5.0.0. // This is a temporary solution to avoid breaking existing sites that use @@ -111,9 +111,9 @@ function environment_indicator_page_top(array &$page_top) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 - $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']); + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')); if ($toolbar_integration_setting_enabled) { - return $page_top; + return; } $active_environment = \Drupal::config('environment_indicator.indicator'); $title = $active_environment->get('name'); diff --git a/js/environment_indicator.js b/js/environment_indicator.js index 2c94cc72..ba38e02d 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -36,12 +36,18 @@ document.querySelector('#toolbar-bar').style.backgroundColor = settings.environmentIndicator.bgColor; document - .querySelectorAll('#toolbar-bar .toolbar-item a:not(.is-active)') + .querySelectorAll('#toolbar-bar .toolbar-tab a.toolbar-item') .forEach((el) => { + el.style.borderBottom = '0px'; el.style.color = settings.environmentIndicator.fgColor; - return el; }); - } + + document + .querySelectorAll('.toolbar .toolbar-bar .toolbar-tab > .toolbar-item') + .forEach((el) => { + el.style.backgroundColor = settings.environmentIndicator.bgColor; + }); + } // Set environment color for gin_toolbar vertical toolbar. if ($body.hasClass('gin--vertical-toolbar')) { diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.install b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install index 3168f832..f8a2329f 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.install +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.install @@ -14,9 +14,11 @@ */ function environment_indicator_toolbar_install() { // Check if toolbar integration is enabled. - $config = \Drupal::config('environment_indicator.settings'); - if ($config->get('toolbar_integration')) { - $config->set('toolbar_integration', []); - $config->save(); + $config_factory = \Drupal::configFactory(); + $settings = $config_factory->getEditable('environment_indicator.settings'); + + if ($settings->get('toolbar_integration')) { + $settings->set('toolbar_integration', []); + $settings->save(); } } diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index 66f639f2..ceb87e5d 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -67,7 +67,7 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('environment_indicator.settings'); $toolbar_integration_module_enabled = $this->moduleHandler->moduleExists('environment_indicator_toolbar') ?? FALSE; - $toolbar_integration_setting_enabled = !empty($config->get('toolbar_integration')['toolbar']); + $toolbar_integration_setting_enabled = !empty($config->get('toolbar_integration')); $form = parent::buildForm($form, $form_state); $form['toolbar_integration'] = [ '#type' => 'checkboxes', diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index 3081cce7..09cd4610 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -191,7 +191,7 @@ class ToolbarHandler { // like ['toolbar']. $toolbars_setting = $this->config->get('toolbar_integration') ?? []; if (!empty($toolbars_setting)) { - $toolbars_setting = [['toolbar' => 'toolbar']]; + $toolbars_setting = ['toolbar' => 'toolbar']; } $items['environment_indicator'] += [ '#type' => 'toolbar_item', -- GitLab From 9879b9619ee9c76ef1887027dca7ff945ee4b770 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 10:59:24 -0700 Subject: [PATCH 30/56] Update JS to match 2.0.21 --- js/environment_indicator.js | 10 ++++------ .../environment_indicator_toolbar/js/toolbar.js | 14 +++++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/js/environment_indicator.js b/js/environment_indicator.js index ba38e02d..fa9cb6a9 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -41,14 +41,14 @@ el.style.borderBottom = '0px'; el.style.color = settings.environmentIndicator.fgColor; }); - document - .querySelectorAll('.toolbar .toolbar-bar .toolbar-tab > .toolbar-item') + .querySelectorAll( + '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item' + ) .forEach((el) => { el.style.backgroundColor = settings.environmentIndicator.bgColor; }); - } - + } // Set environment color for gin_toolbar vertical toolbar. if ($body.hasClass('gin--vertical-toolbar')) { document.querySelector( @@ -67,7 +67,6 @@ return el; }); } - // Set environment color for gin_toolbar horizontal toolbar. if ($body.hasClass('gin--horizontal-toolbar')) { document.querySelector( @@ -77,7 +76,6 @@ '#toolbar-item-administration-tray', ).style.borderTopWidth = borderWidth; } - // Set environment color on the icon of the gin_toolbar if ( $body.hasClass('gin--horizontal-toolbar') || diff --git a/modules/environment_indicator_toolbar/js/toolbar.js b/modules/environment_indicator_toolbar/js/toolbar.js index 6085ea69..ab17baec 100644 --- a/modules/environment_indicator_toolbar/js/toolbar.js +++ b/modules/environment_indicator_toolbar/js/toolbar.js @@ -17,13 +17,19 @@ document.querySelector('#toolbar-bar').style.backgroundColor = settings.environmentIndicator.bgColor; document - .querySelectorAll('#toolbar-bar .toolbar-item a:not(.is-active)') + .querySelectorAll('#toolbar-bar .toolbar-tab a.toolbar-item') .forEach((el) => { + el.style.borderBottom = '0px'; el.style.color = settings.environmentIndicator.fgColor; - return el; + }); + document + .querySelectorAll( + '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item' + ) + .forEach((el) => { + el.style.backgroundColor = settings.environmentIndicator.bgColor; }); } - // Set environment color for gin_toolbar vertical toolbar. if ($body.hasClass('gin--vertical-toolbar')) { document.querySelector( @@ -42,7 +48,6 @@ return el; }); } - // Set environment color for gin_toolbar horizontal toolbar. if ($body.hasClass('gin--horizontal-toolbar')) { document.querySelector( @@ -52,7 +57,6 @@ '#toolbar-item-administration-tray', ).style.borderTopWidth = borderWidth; } - // Set environment color on the icon of the gin_toolbar if ( $body.hasClass('gin--horizontal-toolbar') || -- GitLab From 3b9d199ecbf14d3bc1273f06841523579a3e04d5 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 11:05:59 -0700 Subject: [PATCH 31/56] ESlint fixes --- js/environment_indicator.js | 2 +- modules/environment_indicator_toolbar/js/toolbar.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/environment_indicator.js b/js/environment_indicator.js index fa9cb6a9..fc650d6d 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -43,7 +43,7 @@ }); document .querySelectorAll( - '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item' + '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item', ) .forEach((el) => { el.style.backgroundColor = settings.environmentIndicator.bgColor; diff --git a/modules/environment_indicator_toolbar/js/toolbar.js b/modules/environment_indicator_toolbar/js/toolbar.js index ab17baec..8dde2e90 100644 --- a/modules/environment_indicator_toolbar/js/toolbar.js +++ b/modules/environment_indicator_toolbar/js/toolbar.js @@ -24,7 +24,7 @@ }); document .querySelectorAll( - '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item' + '.toolbar .toolbar-bar .toolbar-tab > .toolbar-item', ) .forEach((el) => { el.style.backgroundColor = settings.environmentIndicator.bgColor; -- GitLab From 6daa12599bf0f52ab400e970b300799b5636f9bd Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 11:16:19 -0700 Subject: [PATCH 32/56] Fix page_top indicator title. --- environment_indicator.module | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index eb3b75bb..8cec7f73 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -116,20 +116,21 @@ function environment_indicator_page_top(array &$page_top) { return; } $active_environment = \Drupal::config('environment_indicator.indicator'); - $title = $active_environment->get('name'); + $title = \Drupal::service('environment_indicator.indicator')->getTitle(); + $current_user_has_access = \Drupal::currentUser()->hasPermission('access environment indicator'); + $name = $active_environment->get('name'); $page_top['indicator'] = [ '#type' => 'environment_indicator', '#title' => $title, '#fg_color' => $active_environment->get('fg_color'), '#bg_color' => $active_environment->get('bg_color'), '#description' => \Drupal::state()->get('environment_indicator.current_release'), - '#access' => !empty($title) && \Drupal::currentUser() - ->hasPermission('access environment indicator'), + '#access' => !empty($name) && $current_user_has_access, '#attached' => [ 'library' => ['environment_indicator/drupal.environment_indicator'], 'drupalSettings' => [ 'environmentIndicator' => [ - 'name' => $title ? $title : ' ', + 'name' => $name, 'fgColor' => $active_environment->get('fg_color'), 'bgColor' => $active_environment->get('bg_color'), 'addFavicon' => \Drupal::config('environment_indicator.settings') -- GitLab From 51c9d5d6e2d04a23b96a6acb7d4d546f144f6e7a Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Sun, 1 Jun 2025 19:21:11 +0000 Subject: [PATCH 33/56] Don't need this now that the default config isn't changing. --- tests/src/Functional/ToolbarIntegrationTest.php | 5 ----- tests/src/FunctionalJavascript/ToolbarIntegrationTest.php | 5 ----- 2 files changed, 10 deletions(-) diff --git a/tests/src/Functional/ToolbarIntegrationTest.php b/tests/src/Functional/ToolbarIntegrationTest.php index 3ce811d2..8c4b42ef 100644 --- a/tests/src/Functional/ToolbarIntegrationTest.php +++ b/tests/src/Functional/ToolbarIntegrationTest.php @@ -72,11 +72,6 @@ class ToolbarIntegrationTest extends BrowserTestBase { $config = $this->config('system.performance'); $config->set('css.preprocess', FALSE)->save(); - // Set the toolbar integration setting to 'toolbar' by default. - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) - ->save(); - // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator', 'access toolbar']); $this->unprivilegedUser = $this->drupalCreateUser(); diff --git a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index 6ef3fd55..625d3e20 100644 --- a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -60,11 +60,6 @@ class ToolbarIntegrationTest extends WebDriverTestBase { $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); - // Set the toolbar integration setting to 'toolbar' by default. - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) - ->save(); - // Disable CSS preprocessing. $config = $this->config('system.performance'); $config->set('css.preprocess', FALSE)->save(); -- GitLab From 0ebd35811f87fb12804144c933baffe796e8b40e Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 13:00:07 -0700 Subject: [PATCH 34/56] Update help text test --- src/Form/EnvironmentIndicatorSettingsForm.php | 8 ++++++-- tests/src/Functional/ToolbarIntegrationTest.php | 9 ++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index ceb87e5d..40a449ba 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -71,16 +71,20 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { $form = parent::buildForm($form, $form_state); $form['toolbar_integration'] = [ '#type' => 'checkboxes', + '#disabled' => TRUE, '#title' => $this->t('Toolbar integration (Deprecated)'), '#options' => [ 'toolbar' => $this->t('Toolbar (Deprecated)'), ], - '#description' => $this->t('This setting is deprecated and will be removed in a future release. Please use the Environment Indicator Toolbar module instead.'), + '#description' => $this->t('This setting is deprecated and will be removed in a future release. Please enable the <strong>Environment Indicator - Toolbar Integration</strong> module to remove this setting early.'), '#default_value' => $config->get('toolbar_integration') ?: [], ]; // If the toolbar integration settings are empty, we remove the // toolbar integration settings from the form. if (!$toolbar_integration_setting_enabled) { + $form['toolbar_integration'] = [ + '#disabled' => TRUE, + ]; unset($form['toolbar_integration']); } // If the toolbar integration module is not enabled, we add a message to @@ -88,7 +92,7 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { if (!$toolbar_integration_module_enabled) { $form['toolbar_integration_message'] = [ '#type' => 'item', - '#markup' => $this->t('The <strong>Environment Indicator Toolbar</strong> module is not enabled. Please enable it to use toolbar integration.'), + '#markup' => $this->t('The <strong>Environment Indicator - Toolbar Integration</strong> (<code>environment_indicator_toolbar</code>) module is not enabled. Please enable it to enable toolbar integration.'), ]; } $form['favicon'] = [ diff --git a/tests/src/Functional/ToolbarIntegrationTest.php b/tests/src/Functional/ToolbarIntegrationTest.php index 8c4b42ef..2c9b5a8a 100644 --- a/tests/src/Functional/ToolbarIntegrationTest.php +++ b/tests/src/Functional/ToolbarIntegrationTest.php @@ -143,9 +143,8 @@ class ToolbarIntegrationTest extends BrowserTestBase { * We also test that the style tag is present with CSS variables. */ public function testEnvironmentIndicatorToolbarIntegration(): void { - - $indicator_config = $this->config('environment_indicator.indicator'); - $indicator_config->set('name', 'Test Environment') + $config = $this->config('environment_indicator.indicator'); + $config->set('name', 'Test Environment') ->set('fg_color', '#000000') ->set('bg_color', '#ffffff') ->save(); @@ -157,8 +156,8 @@ class ToolbarIntegrationTest extends BrowserTestBase { $session->pageTextContains('Test Environment'); $session->elementNotExists('css', '#environment-indicator'); // Change configuration values. - $indicator_config = $this->config('environment_indicator.indicator'); - $indicator_config->set('name', 'Development Environment') + $config = $this->config('environment_indicator.indicator'); + $config->set('name', 'Development Environment') ->set('fg_color', '#efefef') ->set('bg_color', '#12285f') ->save(); -- GitLab From 65e57195a5fa233393d0b4850bbac75c5fc838db Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 13:07:01 -0700 Subject: [PATCH 35/56] Revert form logic --- src/Form/EnvironmentIndicatorSettingsForm.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php index 40a449ba..9a06165c 100644 --- a/src/Form/EnvironmentIndicatorSettingsForm.php +++ b/src/Form/EnvironmentIndicatorSettingsForm.php @@ -71,7 +71,6 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { $form = parent::buildForm($form, $form_state); $form['toolbar_integration'] = [ '#type' => 'checkboxes', - '#disabled' => TRUE, '#title' => $this->t('Toolbar integration (Deprecated)'), '#options' => [ 'toolbar' => $this->t('Toolbar (Deprecated)'), @@ -82,9 +81,6 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase { // If the toolbar integration settings are empty, we remove the // toolbar integration settings from the form. if (!$toolbar_integration_setting_enabled) { - $form['toolbar_integration'] = [ - '#disabled' => TRUE, - ]; unset($form['toolbar_integration']); } // If the toolbar integration module is not enabled, we add a message to -- GitLab From 3945cfb7b5eabc45b5d47d0f0e9ab51e50738413 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 13:11:11 -0700 Subject: [PATCH 36/56] Update tests --- tests/src/Functional/EnvironmentIndicatorTest.php | 5 +++-- tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/src/Functional/EnvironmentIndicatorTest.php b/tests/src/Functional/EnvironmentIndicatorTest.php index eba9e408..27f6de73 100644 --- a/tests/src/Functional/EnvironmentIndicatorTest.php +++ b/tests/src/Functional/EnvironmentIndicatorTest.php @@ -73,9 +73,10 @@ class EnvironmentIndicatorTest extends BrowserTestBase { // Retrieve the dynamic module path. $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); - + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', []) + ->save(); $this->state = \Drupal::state(); - // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator']); $this->unprivilegedUser = $this->drupalCreateUser(); diff --git a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php index 6b0005c2..3c42972e 100644 --- a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php +++ b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php @@ -65,6 +65,9 @@ class EnvironmentIndicatorTest extends WebDriverTestBase { // Retrieve the dynamic module path. $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', []) + ->save(); // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator']); -- GitLab From 5e2c2323fcbef5f5e3cb35e74661c393db48d39e Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 13:23:33 -0700 Subject: [PATCH 37/56] Update js for old library --- environment_indicator.module | 4 ++-- js/environment_indicator.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index 8cec7f73..55025db4 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -83,7 +83,7 @@ function environment_indicator_preprocess_html(&$variables) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 - $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')); + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']) ?? FALSE; if ($toolbar_integration_module_enabled && $toolbar_integration_setting_enabled) { return; } @@ -111,7 +111,7 @@ function environment_indicator_page_top(array &$page_top) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 - $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')); + $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']) ?? FALSE; if ($toolbar_integration_setting_enabled) { return; } diff --git a/js/environment_indicator.js b/js/environment_indicator.js index fc650d6d..bb15713e 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -28,7 +28,7 @@ ) || '6px'; // Set environment color if not using gin_toolbar. if ( - settings.environmentIndicator.toolbars.toolbar === 'toolbar' && + settings.environmentIndicator.toolbars.toolbar && !$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar') ) { -- GitLab From 51a22caf86e674649981dc3c4dbbc088ccc041c4 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Sun, 1 Jun 2025 13:40:27 -0700 Subject: [PATCH 38/56] Update tests. --- src/ToolbarHandler.php | 10 +--------- tests/src/Functional/ToolbarIntegrationTest.php | 9 ++++++--- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index 09cd4610..3a7a6673 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -185,14 +185,6 @@ class ToolbarHandler { if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { $title = $this->getTitle(); - // This must be done for backward compatibility. Some versions of Drupal - // have the toolbar integration setting as an array of arrays like: - // [['toolbar' => 'toolbar']], while others have it as an array of strings - // like ['toolbar']. - $toolbars_setting = $this->config->get('toolbar_integration') ?? []; - if (!empty($toolbars_setting)) { - $toolbars_setting = ['toolbar' => 'toolbar']; - } $items['environment_indicator'] += [ '#type' => 'toolbar_item', '#weight' => 125, @@ -216,7 +208,7 @@ class ToolbarHandler { 'name' => $this->activeEnvironment->get('name') ?: ' ', 'fgColor' => $this->activeEnvironment->get('fg_color'), 'bgColor' => $this->activeEnvironment->get('bg_color'), - 'toolbars' => $toolbars_setting, + 'toolbars' => $this->config->get('toolbar_integration') ?? [], ], ], ], diff --git a/tests/src/Functional/ToolbarIntegrationTest.php b/tests/src/Functional/ToolbarIntegrationTest.php index 2c9b5a8a..5f28e790 100644 --- a/tests/src/Functional/ToolbarIntegrationTest.php +++ b/tests/src/Functional/ToolbarIntegrationTest.php @@ -119,14 +119,14 @@ class ToolbarIntegrationTest extends BrowserTestBase { * This also tests that the correct libraries are loaded. */ public function testEnvironmentIndicatorVisibilityWithToolBarSettingEnabled(): void { + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) + ->save(); $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Test Environment') ->set('fg_color', '#000000') ->set('bg_color', '#ffffff') ->save(); - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) - ->save(); // Clear drupal cache. $this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']); $this->drupalLogin($this->privilegedUser); @@ -143,6 +143,9 @@ class ToolbarIntegrationTest extends BrowserTestBase { * We also test that the style tag is present with CSS variables. */ public function testEnvironmentIndicatorToolbarIntegration(): void { + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) + ->save(); $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Test Environment') ->set('fg_color', '#000000') -- GitLab From 82fd66d20f52f7ff78e5b70d429e30c0d880eaf1 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 13:02:51 -0700 Subject: [PATCH 39/56] Updates --- environment_indicator.module | 2 +- js/environment_indicator.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index 55025db4..d26224e4 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -111,7 +111,7 @@ function environment_indicator_page_top(array &$page_top) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 - $toolbar_integration_setting_enabled = !empty(\Drupal::config('environment_indicator.settings')->get('toolbar_integration')['toolbar']) ?? FALSE; + $toolbar_integration_setting_enabled = _environment_indicator_external_integration_is_enabled('toolbar'); if ($toolbar_integration_setting_enabled) { return; } diff --git a/js/environment_indicator.js b/js/environment_indicator.js index bb15713e..0f34d211 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -28,7 +28,6 @@ ) || '6px'; // Set environment color if not using gin_toolbar. if ( - settings.environmentIndicator.toolbars.toolbar && !$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar') ) { -- GitLab From f8cbaba904f17f29166c97a9421789bfb694407b Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 13:12:26 -0700 Subject: [PATCH 40/56] Ignore line --- environment_indicator.module | 1 + 1 file changed, 1 insertion(+) diff --git a/environment_indicator.module b/environment_indicator.module index d26224e4..bb22ceef 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -111,6 +111,7 @@ function environment_indicator_page_top(array &$page_top) { // In the future, we will rely on the environment_indicator_toolbar module // to handle the toolbar integration, and this check will no longer be needed. // @see https://www.drupal.org/i/3484735 + // @phpstan-ignore-next-line $toolbar_integration_setting_enabled = _environment_indicator_external_integration_is_enabled('toolbar'); if ($toolbar_integration_setting_enabled) { return; -- GitLab From ccb3d6797a610e0ee964ae2002f8bb192bbeed85 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 13:22:13 -0700 Subject: [PATCH 41/56] revert to original settings --- tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php index 3c42972e..4bb71c4e 100644 --- a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php +++ b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php @@ -65,10 +65,6 @@ class EnvironmentIndicatorTest extends WebDriverTestBase { // Retrieve the dynamic module path. $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', []) - ->save(); - // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator']); $this->environmentIndicatorAdministrator = $this->drupalCreateUser([ -- GitLab From cbe23d267081fa2618788cb9c8a1d792a4f84cb4 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 13:59:30 -0700 Subject: [PATCH 42/56] Update logic for toolbar integration js --- environment_indicator.module | 8 ++++---- js/environment_indicator.js | 1 + src/ToolbarHandler.php | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index bb22ceef..0ee8986c 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -119,6 +119,8 @@ function environment_indicator_page_top(array &$page_top) { $active_environment = \Drupal::config('environment_indicator.indicator'); $title = \Drupal::service('environment_indicator.indicator')->getTitle(); $current_user_has_access = \Drupal::currentUser()->hasPermission('access environment indicator'); + $settings = \Drupal::config('environment_indicator.settings'); + $toolbar_integration = in_array('toolbar', $settings->get('toolbar_integration') ?? [], TRUE); $name = $active_environment->get('name'); $page_top['indicator'] = [ '#type' => 'environment_indicator', @@ -134,10 +136,8 @@ function environment_indicator_page_top(array &$page_top) { 'name' => $name, 'fgColor' => $active_environment->get('fg_color'), 'bgColor' => $active_environment->get('bg_color'), - 'addFavicon' => \Drupal::config('environment_indicator.settings') - ->get('favicon'), - 'toolbars' => \Drupal::config('environment_indicator.settings') - ->get('toolbar_integration'), + 'addFavicon' => $settings->get('favicon'), + 'toolbars' => $toolbar_integration, ], ], ], diff --git a/js/environment_indicator.js b/js/environment_indicator.js index 0f34d211..e138d2ff 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -28,6 +28,7 @@ ) || '6px'; // Set environment color if not using gin_toolbar. if ( + settings.environmentIndicator.toolbars === true && !$body.hasClass('gin--vertical-toolbar') && !$body.hasClass('gin--horizontal-toolbar') ) { diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index 3a7a6673..f67d4580 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -183,7 +183,8 @@ class ToolbarHandler { ]; if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { - + $settings = \Drupal::config('environment_indicator.settings'); + $toolbar_integration = in_array('toolbar', $settings->get('toolbar_integration') ?? [], TRUE); $title = $this->getTitle(); $items['environment_indicator'] += [ '#type' => 'toolbar_item', @@ -208,7 +209,7 @@ class ToolbarHandler { 'name' => $this->activeEnvironment->get('name') ?: ' ', 'fgColor' => $this->activeEnvironment->get('fg_color'), 'bgColor' => $this->activeEnvironment->get('bg_color'), - 'toolbars' => $this->config->get('toolbar_integration') ?? [], + 'toolbars' => $toolbar_integration, ], ], ], -- GitLab From 1212495dd1989bfdff3ddbeff58ca229bcb79221 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 14:24:24 -0700 Subject: [PATCH 43/56] Use dependency injection. --- src/ToolbarHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index f67d4580..1b399007 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -183,7 +183,7 @@ class ToolbarHandler { ]; if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { - $settings = \Drupal::config('environment_indicator.settings'); + $settings = $this->config('environment_indicator.settings'); $toolbar_integration = in_array('toolbar', $settings->get('toolbar_integration') ?? [], TRUE); $title = $this->getTitle(); $items['environment_indicator'] += [ -- GitLab From 401916ac4b318e810b06ea0dac1b3c66ddfdec48 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 14:29:24 -0700 Subject: [PATCH 44/56] Use dependency injection. --- src/ToolbarHandler.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php index 1b399007..b40fd5de 100644 --- a/src/ToolbarHandler.php +++ b/src/ToolbarHandler.php @@ -183,8 +183,7 @@ class ToolbarHandler { ]; if ($this->hasAccessActiveEnvironment() && $this->externalIntegration('toolbar')) { - $settings = $this->config('environment_indicator.settings'); - $toolbar_integration = in_array('toolbar', $settings->get('toolbar_integration') ?? [], TRUE); + $toolbar_integration = in_array('toolbar', $this->config->get('toolbar_integration') ?? [], TRUE); $title = $this->getTitle(); $items['environment_indicator'] += [ '#type' => 'toolbar_item', -- GitLab From 29d7b92422a6ec48c5df3e49f88c72d3ea38b5b9 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 16:04:04 -0700 Subject: [PATCH 45/56] Update tests --- tests/src/Functional/EnvironmentIndicatorTest.php | 3 ++- tests/src/FunctionalJavascript/ToolbarIntegrationTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/src/Functional/EnvironmentIndicatorTest.php b/tests/src/Functional/EnvironmentIndicatorTest.php index 27f6de73..5a704360 100644 --- a/tests/src/Functional/EnvironmentIndicatorTest.php +++ b/tests/src/Functional/EnvironmentIndicatorTest.php @@ -105,6 +105,7 @@ class EnvironmentIndicatorTest extends BrowserTestBase { $this->assertStringContainsString('background-color: red', $output); $this->assertStringContainsString('color: green', $output); $this->assertSession()->elementExists("css", "link[href*='{$this->modulePath}/css/environment_indicator.css']"); + $this->resetAll(); // Change configuration values. $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Test Environment') @@ -122,7 +123,7 @@ class EnvironmentIndicatorTest extends BrowserTestBase { $this->assertNotEmpty($output, 'Style attribute should not be empty.'); $this->assertStringContainsString('background-color: #000000', $output); $this->assertStringContainsString('color: #ffffff', $output); - + $this->resetAll(); // Change configuration values. $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Development Environment') diff --git a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index 625d3e20..73b5558f 100644 --- a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -20,9 +20,9 @@ class ToolbarIntegrationTest extends WebDriverTestBase { * {@inheritdoc} */ protected static $modules = [ + 'toolbar', 'environment_indicator', 'environment_indicator_ui', - 'toolbar', ]; /** -- GitLab From abf1c1a6fa233053967644c8f664d0df1f4371b9 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 16:10:12 -0700 Subject: [PATCH 46/56] Update tests --- tests/src/FunctionalJavascript/ToolbarIntegrationTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index 73b5558f..ee9c310f 100644 --- a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -20,11 +20,16 @@ class ToolbarIntegrationTest extends WebDriverTestBase { * {@inheritdoc} */ protected static $modules = [ - 'toolbar', 'environment_indicator', 'environment_indicator_ui', + 'toolbar', ]; + /** + * {@inheritdoc} + */ + protected $profile = 'standard'; + /** * {@inheritdoc} */ -- GitLab From 57cec5b2a2eaa8aad8a972eeeeed8ecc39e70e90 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 16:34:54 -0700 Subject: [PATCH 47/56] Add toolbar library dependency. --- environment_indicator.libraries.yml | 1 + .../environment_indicator_toolbar.libraries.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/environment_indicator.libraries.yml b/environment_indicator.libraries.yml index 546d1c90..a4715aac 100644 --- a/environment_indicator.libraries.yml +++ b/environment_indicator.libraries.yml @@ -7,6 +7,7 @@ drupal.environment_indicator: dependencies: - core/drupal - core/jquery + - toolbar/toolbar tinycon: js: js/tinycon.min.js: { minified: true } diff --git a/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml b/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml index 9e7ed02e..3fb6c4a8 100644 --- a/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml +++ b/modules/environment_indicator_toolbar/environment_indicator_toolbar.libraries.yml @@ -8,3 +8,4 @@ toolbar: - core/drupal - core/jquery - core/drupalSettings + - core/toolbar -- GitLab From dd2dfbdde395883e0177c196fd074fbbab410c0c Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 16:40:08 -0700 Subject: [PATCH 48/56] Update tests --- environment_indicator.libraries.yml | 2 +- tests/src/FunctionalJavascript/ToolbarIntegrationTest.php | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/environment_indicator.libraries.yml b/environment_indicator.libraries.yml index a4715aac..20702b50 100644 --- a/environment_indicator.libraries.yml +++ b/environment_indicator.libraries.yml @@ -7,7 +7,7 @@ drupal.environment_indicator: dependencies: - core/drupal - core/jquery - - toolbar/toolbar + tinycon: js: js/tinycon.min.js: { minified: true } diff --git a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index ee9c310f..625d3e20 100644 --- a/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -25,11 +25,6 @@ class ToolbarIntegrationTest extends WebDriverTestBase { 'toolbar', ]; - /** - * {@inheritdoc} - */ - protected $profile = 'standard'; - /** * {@inheritdoc} */ -- GitLab From e086b66ee474dbd84dc31036483bc89c81322e2a Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 19:07:55 -0700 Subject: [PATCH 49/56] Turn off toolbar integration. --- tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php index 4bb71c4e..6c7ce225 100644 --- a/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php +++ b/tests/src/FunctionalJavascript/EnvironmentIndicatorTest.php @@ -65,6 +65,10 @@ class EnvironmentIndicatorTest extends WebDriverTestBase { // Retrieve the dynamic module path. $moduleHandler = \Drupal::service('extension.list.module'); $this->modulePath = $moduleHandler->getPath('environment_indicator'); + // Turn off the toolbar integration for the environment indicator. + $settings = $this->config('environment_indicator.settings'); + $settings->set('toolbar_integration', []) + ->save(); // Create users. $this->privilegedUser = $this->drupalCreateUser(['access environment indicator']); $this->environmentIndicatorAdministrator = $this->drupalCreateUser([ -- GitLab From 80140f28fd5a47eeb5fc95b9f91bec9debba9213 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 19:28:02 -0700 Subject: [PATCH 50/56] Ensure JS does not fail. --- environment_indicator.module | 2 +- js/environment_indicator.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index 0ee8986c..d3e01dfb 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -137,7 +137,7 @@ function environment_indicator_page_top(array &$page_top) { 'fgColor' => $active_environment->get('fg_color'), 'bgColor' => $active_environment->get('bg_color'), 'addFavicon' => $settings->get('favicon'), - 'toolbars' => $toolbar_integration, + 'toolbars' => _environment_indicator_external_integration_is_enabled('toolbar'), ], ], ], diff --git a/js/environment_indicator.js b/js/environment_indicator.js index e138d2ff..2725ccbd 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -36,7 +36,7 @@ document.querySelector('#toolbar-bar').style.backgroundColor = settings.environmentIndicator.bgColor; document - .querySelectorAll('#toolbar-bar .toolbar-tab a.toolbar-item') + .querySelectorAll('#toolbar-bar .toolbar-item a:not(.is-active)') .forEach((el) => { el.style.borderBottom = '0px'; el.style.color = settings.environmentIndicator.fgColor; -- GitLab From 628f8778942296e7d307f03903b743906821d41e Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 19:28:27 -0700 Subject: [PATCH 51/56] Ensure JS does not fail. --- environment_indicator.module | 1 - 1 file changed, 1 deletion(-) diff --git a/environment_indicator.module b/environment_indicator.module index d3e01dfb..a8888236 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -120,7 +120,6 @@ function environment_indicator_page_top(array &$page_top) { $title = \Drupal::service('environment_indicator.indicator')->getTitle(); $current_user_has_access = \Drupal::currentUser()->hasPermission('access environment indicator'); $settings = \Drupal::config('environment_indicator.settings'); - $toolbar_integration = in_array('toolbar', $settings->get('toolbar_integration') ?? [], TRUE); $name = $active_environment->get('name'); $page_top['indicator'] = [ '#type' => 'environment_indicator', -- GitLab From 5326c5ba4c4dcba71f2731119c5723850df069a4 Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Mon, 2 Jun 2025 19:28:59 -0700 Subject: [PATCH 52/56] Ensure JS does not fail. --- environment_indicator.module | 1 + 1 file changed, 1 insertion(+) diff --git a/environment_indicator.module b/environment_indicator.module index a8888236..ef826ad7 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -136,6 +136,7 @@ function environment_indicator_page_top(array &$page_top) { 'fgColor' => $active_environment->get('fg_color'), 'bgColor' => $active_environment->get('bg_color'), 'addFavicon' => $settings->get('favicon'), + // @phpstan-ignore-next-line 'toolbars' => _environment_indicator_external_integration_is_enabled('toolbar'), ], ], -- GitLab From 5d741981c5045917f9082d9e6cf9e96ec2f0588d Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Tue, 3 Jun 2025 03:44:10 +0000 Subject: [PATCH 53/56] fixes --- environment_indicator.libraries.yml | 1 - tests/src/Functional/ToolbarIntegrationTest.php | 6 ------ 2 files changed, 7 deletions(-) diff --git a/environment_indicator.libraries.yml b/environment_indicator.libraries.yml index 20702b50..546d1c90 100644 --- a/environment_indicator.libraries.yml +++ b/environment_indicator.libraries.yml @@ -7,7 +7,6 @@ drupal.environment_indicator: dependencies: - core/drupal - core/jquery - tinycon: js: js/tinycon.min.js: { minified: true } diff --git a/tests/src/Functional/ToolbarIntegrationTest.php b/tests/src/Functional/ToolbarIntegrationTest.php index 5f28e790..2dc84ab6 100644 --- a/tests/src/Functional/ToolbarIntegrationTest.php +++ b/tests/src/Functional/ToolbarIntegrationTest.php @@ -119,9 +119,6 @@ class ToolbarIntegrationTest extends BrowserTestBase { * This also tests that the correct libraries are loaded. */ public function testEnvironmentIndicatorVisibilityWithToolBarSettingEnabled(): void { - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) - ->save(); $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Test Environment') ->set('fg_color', '#000000') @@ -143,9 +140,6 @@ class ToolbarIntegrationTest extends BrowserTestBase { * We also test that the style tag is present with CSS variables. */ public function testEnvironmentIndicatorToolbarIntegration(): void { - $settings = $this->config('environment_indicator.settings'); - $settings->set('toolbar_integration', ['toolbar' => 'toolbar']) - ->save(); $config = $this->config('environment_indicator.indicator'); $config->set('name', 'Test Environment') ->set('fg_color', '#000000') -- GitLab From 5fad34680adfa3067c44bfec4a9b22d7129f5e3e Mon Sep 17 00:00:00 2001 From: Chris Green <chrisgreen@arizona.edu> Date: Fri, 6 Jun 2025 08:59:56 -0700 Subject: [PATCH 54/56] Fix broken implementation of basic environment_indicator. --- environment_indicator.module | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/environment_indicator.module b/environment_indicator.module index a9283ba2..560d4a55 100644 --- a/environment_indicator.module +++ b/environment_indicator.module @@ -117,16 +117,16 @@ function environment_indicator_page_top(array &$page_top) { return; } $active_environment = \Drupal::config('environment_indicator.indicator'); - $title = \Drupal::service('environment_indicator.indicator')->getTitle(); + $environment_indicator = \Drupal::service('environment_indicator.indicator'); $current_user_has_access = \Drupal::currentUser()->hasPermission('access environment indicator'); $settings = \Drupal::config('environment_indicator.settings'); $name = $active_environment->get('name'); $page_top['indicator'] = [ '#type' => 'environment_indicator', - '#title' => $title, + '#title' => $name, '#fg_color' => $active_environment->get('fg_color'), '#bg_color' => $active_environment->get('bg_color'), - '#description' => \Drupal::state()->get('environment_indicator.current_release'), + '#description' => $environment_indicator->getCurrentRelease(), '#access' => !empty($name) && $current_user_has_access, '#attached' => [ 'library' => ['environment_indicator/drupal.environment_indicator'], -- GitLab From 082c3bf574540b792f021d4493a0bc2da8d659ae Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Mon, 9 Jun 2025 22:25:25 +0000 Subject: [PATCH 55/56] Update comment style. --- css/environment_indicator.css | 2 +- js/environment_indicator.js | 2 +- .../css/toolbar.css | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index cf68c5ef..183e6420 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -71,7 +71,7 @@ font-size: 1em; } -/* @todo: Remove everything below this comment in environment_indicator:5.0.0. */ +// @todo: Remove everything below this comment in environment_indicator:5.0.0. .toolbar .toolbar-bar .toolbar-tab > a.toolbar-icon.is-active::before { filter: invert(0%); } diff --git a/js/environment_indicator.js b/js/environment_indicator.js index 2725ccbd..6c37cc58 100644 --- a/js/environment_indicator.js +++ b/js/environment_indicator.js @@ -17,7 +17,7 @@ }, }; - /** @todo Remove this function in environment_indicator 5.0.0 */ + // @todo Remove this function in environment_indicator 5.0.0 Drupal.behaviors.environmentIndicatorToolbar = { attach(context, settings) { if (settings.environmentIndicator !== undefined) { diff --git a/modules/environment_indicator_toolbar/css/toolbar.css b/modules/environment_indicator_toolbar/css/toolbar.css index 71e6e48d..39ec6b99 100644 --- a/modules/environment_indicator_toolbar/css/toolbar.css +++ b/modules/environment_indicator_toolbar/css/toolbar.css @@ -54,20 +54,20 @@ .gin--vertical-toolbar .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { margin-inline-start: calc( - /* stylelint-disable custom-property-pattern */ - /* See https://www.drupal.org/i/3309113 */ - /* See https://www.drupal.org/i/3524015 */ + // stylelint-disable custom-property-pattern + // See https://www.drupal.org/i/3309113 + // See https://www.drupal.org/i/3524015 var(--gin-toolbar-width-collapsed, var(--ginToolbarWidthCollapsed)) - 4px - /* stylelint-enable custom-property-pattern */ + // stylelint-enable custom-property-pattern ); } .gin--vertical-toolbar[data-toolbar-menu="open"] .toolbar-menu-administration>.toolbar-menu>.menu-item .toolbar-menu { margin-inline-start: calc( - /* stylelint-disable custom-property-pattern */ - /* See https://www.drupal.org/i/3309113 */ - /* See https://www.drupal.org/i/3524015 */ + // stylelint-disable custom-property-pattern + // See https://www.drupal.org/i/3309113 + // See https://www.drupal.org/i/3524015 var(--gin-toolbar-width, var(--ginToolbarWidth)) - 4px - /* stylelint-enable custom-property-pattern */ + // stylelint-enable custom-property-pattern ); } -- GitLab From 080bf35b1e86e3e05b7c16d8e419b29db59bc018 Mon Sep 17 00:00:00 2001 From: Chris Green <42483-trackleft2@users.noreply.drupalcode.org> Date: Mon, 9 Jun 2025 22:30:19 +0000 Subject: [PATCH 56/56] Stylelint complained about // comments --- css/environment_indicator.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/css/environment_indicator.css b/css/environment_indicator.css index 183e6420..cf68c5ef 100644 --- a/css/environment_indicator.css +++ b/css/environment_indicator.css @@ -71,7 +71,7 @@ font-size: 1em; } -// @todo: Remove everything below this comment in environment_indicator:5.0.0. +/* @todo: Remove everything below this comment in environment_indicator:5.0.0. */ .toolbar .toolbar-bar .toolbar-tab > a.toolbar-icon.is-active::before { filter: invert(0%); } -- GitLab