You are here

function environment_indicator_page_top in Environment Indicator 8.3

Same name and namespace in other branches
  1. 4.x environment_indicator.module \environment_indicator_page_top()

Implements hook_page_top().

File

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

Code

function environment_indicator_page_top(array &$page_top) {
  if (_environment_indicator_external_integration_is_enabled('toolbar')) {
    return;
  }
  $active_environment = \Drupal::config('environment_indicator.indicator');
  $title = $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'),
    '#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'),
        ],
      ],
    ],
  ];

  // Only add the environment indicator switcher if there are environments to
  // switch to.
  if ($items = _environment_indicator_switcher_links()) {
    $page_top['indicator']['switcher'] = [
      '#theme' => 'links',
      '#links' => $items,
      '#attributes' => [
        'class' => [
          'environment-switcher-container',
        ],
        'style' => 'border-top: 1px solid ' . \Drupal::config('environment_indicator.indicator')
          ->get('fg_color'),
      ],
    ];
    $page_top['indicator']['#attributes'] = [
      'style' => 'cursor: pointer',
      'title' => t('Show the environment switcher.'),
    ];
    $page_top['indicator'] += [
      '#cache' => [
        'tags' => Cache::mergeTags([
          'config:environment_indicator.settings',
        ], _environment_indicator_switcher_cache_tags()),
      ],
    ];
  }
}