Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / environment_indicator / environment_indicator.module
index 8c189c69291dc2fe8421c0d921bca84d1d02c712..3d57ddef47357c96f1ff7c054fbde20d413b93a4 100644 (file)
@@ -15,12 +15,13 @@ use Drupal\environment_indicator\Entity\EnvironmentIndicator;
  * Implements hook_help().
  */
 function environment_indicator_help($route_name, RouteMatchInterface $route_match) {
+  $permissions_url = Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-environment_indicator'])->toString();
+  $settings_url = Url::fromRoute('environment_indicator.settings')->toString();
   switch ($route_name) {
     case 'environment_indicator.settings':
       $output = '<p>' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.)') . '</p>';
-      $output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href="@permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', [
-          '@permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-environment_indicator'])
-            ->toString(),
+      $output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href=":permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', [
+          ':permissions' => $permissions_url,
         ]) . '</p>';
       $output .= '<p>'. t('The recommended way to add information about your release is to set the \'environment_indicator.current_release\' state. Use your git hooks to set the state using drush: <code>drush sset environment_indicator.current_release v1.2.44</code>') .'</p>';
       return $output;
@@ -32,8 +33,8 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc
 
     case 'help.page.environment_indicator':
       $output = '<p>' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.') . '</p>';
-      $output .= '<p>' . t('The Environment Indicator <a href="@settings">settings page</a> allows you to modify some elements of the indicator\'s behavior and appearance. Since the appearance of the indicator is dependent on your site theme, substantial customisations require modifications to your site\'s theme and CSS files.', ['@settings' => Url::fromRoute('environment_indicator.settings')]) . '</p>';
-      $output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href="@permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', ['@permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-environment_indicator'])]) . '</p>';
+      $output .= '<p>' . t('The Environment Indicator <a href=":settings">settings page</a> allows you to modify some elements of the indicator\'s behavior and appearance. Since the appearance of the indicator is dependent on your site theme, substantial customisations require modifications to your site\'s theme and CSS files.', [':settings' => $settings_url]) . '</p>';
+      $output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href=":permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', [':permissions' => $permissions_url]) . '</p>';
       $output .= '<p>' . t('Modify the "environment_indicator.indicator" configuration object to control how the environment indicator is presented:') . '</p>';
       $output .= '<dl>';
       $output .= '<dt><em>$config[\'environment_indicator.indicator\'][\'bg_color\']</em></dt><dd>' . t('A valid CSS color for the background of the indicator.') . '<br/>$config[\'environment_indicator.indicator\'][\'bg_color\'];<br /></dd></dt>';
@@ -56,16 +57,26 @@ function environment_indicator_page_top(array &$page_top) {
     return;
   }
   $active_environment = \Drupal::config('environment_indicator.indicator');
+  $title = $active_environment->get('name');
   $page_top['indicator'] = [
     '#type' => 'environment_indicator',
-    '#title' => $active_environment->get('name'),
+    '#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' => \Drupal::currentUser()
+    '#access' => !empty($title) && \Drupal::currentUser()
       ->hasPermission('access environment indicator'),
     '#attached' => [
       'library' => ['environment_indicator/drupal.environment_indicator'],
+      'drupalSettings' => [
+        'environmentIndicator' => [
+          'name' => $title ? $title : ' ',
+          'fgColor' => $active_environment->get('fg_color'),
+          'bgColor' => $active_environment->get('bg_color'),
+          'addFavicon' => \Drupal::config('environment_indicator.settings')
+            ->get('favicon'),
+        ],
+      ],
     ],
   ];
 
@@ -168,9 +179,35 @@ function _environment_indicator_parse_style($style) {
  */
 function environment_indicator_toolbar() {
   $active_environment = \Drupal::config('environment_indicator.indicator');
+  $title = $active_environment->get('name');
   $permission = \Drupal::currentUser()
     ->hasPermission('access environment indicator');
   $items['environment_indicator'] = [
+    '#attached' => [
+      'library' => ['environment_indicator/drupal.environment_indicator'],
+      'drupalSettings' => [
+        'environmentIndicator' => [
+          'name' => $title ? $title : ' ',
+          'fgColor' => $active_environment->get('fg_color'),
+          'bgColor' => $active_environment->get('bg_color'),
+          'addFavicon' => \Drupal::config('environment_indicator.settings')
+            ->get('favicon'),
+        ],
+      ],
+    ]
+  ];
+  if (!_environment_indicator_external_integration_is_enabled('toolbar')) {
+    return $items;
+  }
+  $release_info = \Drupal::state()->get('environment_indicator.current_release');
+  if (strlen($release_info)) {
+    $release_info = '(' . $release_info . ')';
+  }
+  $title = implode(' ', array_filter([$release_info, $title]));
+  if (!strlen($title)) {
+    return $items;
+  }
+  $items['environment_indicator'] += [
     // Include the toolbar_tab_wrapper to style the link like a toolbar tab.
     // Exclude the theme wrapper if custom styling is desired.
     '#type' => 'toolbar_item',
@@ -179,14 +216,6 @@ function environment_indicator_toolbar() {
       'tags' => Cache::mergeTags(['config:environment_indicator.settings'], _environment_indicator_switcher_cache_tags()),
     ],
     '#weight' => 125,
-  ];
-  if (!_environment_indicator_external_integration_is_enabled('toolbar')) {
-    return $items;
-  }
-  $title = $active_environment->get('name');
-  $release_info = \Drupal::state()->get('environment_indicator.current_release');
-  $title = empty($release_info) ? $title : $title . ' (' . $release_info . ')';
-  $items['environment_indicator'] += [
     'tab' => [
       '#type' => 'link',
       '#access' => $permission,
@@ -216,18 +245,6 @@ function environment_indicator_toolbar() {
         ],
       ],
     ],
-    '#attached' => [
-      'library' => ['environment_indicator/drupal.environment_indicator'],
-      'drupalSettings' => [
-        'environmentIndicator' => [
-          'name' => $active_environment->get('name'),
-          'fgColor' => $active_environment->get('fg_color'),
-          'bgColor' => $active_environment->get('bg_color'),
-          'addFavicon' => \Drupal::config('environment_indicator.settings')
-            ->get('favicon'),
-        ],
-      ],
-    ],
   ];
 
   if ($links = _environment_indicator_switcher_toolbar_links()) {