You are here

function environment_indicator_toolbar in Environment Indicator 4.x

Same name and namespace in other branches
  1. 8.3 environment_indicator.module \environment_indicator_toolbar()
  2. 8.2 environment_indicator.module \environment_indicator_toolbar()

Implements hook_toolbar().

File

./environment_indicator.module, line 185
Module implementation file.

Code

function environment_indicator_toolbar() {
  $active_environment = \Drupal::config('environment_indicator.indicator');
  $title = $active_environment
    ->get('name');
  $currentUser = Drupal::currentUser();
  $permission = $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',
    '#access' => $permission,
    '#cache' => [
      'tags' => Cache::mergeTags([
        'config:environment_indicator.settings',
      ], _environment_indicator_switcher_cache_tags()),
    ],
    '#weight' => 125,
    'tab' => [
      '#type' => 'link',
      '#access' => $permission,
      '#title' => $title,
      '#url' => Url::fromRoute('environment_indicator.settings'),
      '#attributes' => [
        'title' => t('Environments'),
        'class' => [
          'toolbar-icon',
          'toolbar-item',
          'toolbar-icon-environment',
        ],
      ],
    ],
    'tray' => [
      '#heading' => t('Environment switcher'),
      '#wrapper_attributes' => [],
      '#access' => $permission,
      'configure' => [
        '#type' => 'link',
        '#access' => $currentUser
          ->hasPermission('administer environment indicator settings'),
        '#title' => t('Configure'),
        '#url' => Url::fromRoute('environment_indicator.settings'),
        '#weight' => 100,
        '#attributes' => [
          'title' => t('Environment switcher configuration'),
          'class' => [
            'edit-environments',
          ],
        ],
      ],
    ],
  ];
  if ($links = _environment_indicator_switcher_toolbar_links()) {
    $items['environment_indicator']['tray']['links'] = $links;
  }
  return $items;
}