You are here

gin_toolbar.module in Gin Toolbar 8

File

gin_toolbar.module
View source
<?php

/**
 * @file
 * gin_toolbar.module
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\gin_toolbar\Render\Element\GinToolbar;
use Drupal\gin\GinSettings;

/**
 * Implements hook_preprocess_HOOK() for html.
 */
function gin_toolbar_preprocess_html(&$variables) {

  // Are we relevant?
  if (!_gin_toolbar_gin_is_active()) {
    return;
  }

  // Get theme settings.

  /** @var \Drupal\gin\GinSettings $settings */
  $settings = \Drupal::classResolver(GinSettings::class);
  $toolbar = $settings
    ->get('classic_toolbar');

  // Check if darkmode is enabled.
  if ($settings
    ->get('enable_darkmode')) {
    $variables['attributes']['class'][] = 'gin--dark-mode';
  }

  // Set accent color.
  $variables['attributes']['data-gin-accent'] = $settings
    ->get('preset_accent_color');

  // High contrast mode.
  if ($settings
    ->get('high_contrast_mode')) {
    $variables['attributes']['class'][] = 'gin--high-contrast-mode';
  }

  // Only add gin--classic-toolbar class if user has permission.
  if (!\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    return;
  }

  // Set toolbar.
  $variables['attributes']['class'][] = 'gin--' . $toolbar . '-toolbar';
}

/**
 * Implements hook_preprocess_HOOK() for page_attachments.
 */
function gin_toolbar_page_attachments_alter(&$page) {

  // Are we relevant?
  if (!_gin_toolbar_gin_is_active()) {
    return;
  }

  // Get theme settings.

  /** @var \Drupal\gin\GinSettings $settings */
  $settings = \Drupal::classResolver(GinSettings::class);
  $toolbar = $settings
    ->get('classic_toolbar');

  // Attach the init script.
  $page['#attached']['library'][] = 'gin/gin_init';
  if ($toolbar === 'classic') {

    // Attach the classic toolbar styles.
    $page['#attached']['library'][] = 'gin/gin_classic_toolbar';
  }
  elseif ($toolbar === 'horizontal') {

    // Attach the horizontal toolbar styles.
    $page['#attached']['library'][] = 'gin/gin_horizontal_toolbar';
  }
  else {

    // Attach toolbar styles.
    $page['#attached']['library'][] = 'gin/gin_toolbar';
  }

  // Attach accent overrides CSS.
  $page['#attached']['library'][] = 'gin/gin_accent';

  // Add library for dialog.
  $page['#attached']['library'][] = 'gin/gin_dialog';
  $page['#attached']['library'][] = 'claro/claro.drupal.dialog';

  // Expose settings to JS.
  $page['#attached']['drupalSettings']['gin']['darkmode'] = $settings
    ->get('enable_darkmode');
  $page['#attached']['drupalSettings']['gin']['darkmode_class'] = 'gin--dark-mode';
  $page['#attached']['drupalSettings']['gin']['preset_accent_color'] = $settings
    ->get('preset_accent_color');
  $page['#attached']['drupalSettings']['gin']['accent_color'] = $settings
    ->get('accent_color');
  $page['#attached']['drupalSettings']['gin']['preset_focus_color'] = $settings
    ->getDefault('preset_focus_color');
  $page['#attached']['drupalSettings']['gin']['focus_color'] = $settings
    ->getDefault('focus_color');
  $page['#attached']['drupalSettings']['gin']['highcontrastmode'] = $settings
    ->get('high_contrast_mode');
  $page['#attached']['drupalSettings']['gin']['highcontrastmode_class'] = 'gin--high-contrast-mode';
}

/**
 * Toolbar alter().
 */
function gin_toolbar_theme_registry_alter(&$theme_registry) {
  $theme_registry['toolbar']['path'] = drupal_get_path('module', 'gin_toolbar') . '/templates';
  $theme_registry['menu__toolbar']['path'] = drupal_get_path('module', 'gin_toolbar') . '/templates';
}

/**
 * Implements hook_preprocess_menu().
 */
function gin_toolbar_preprocess_menu(&$variables) {
  if (isset($variables['theme_hook_original']) && $variables['theme_hook_original'] == 'menu__toolbar__admin') {

    // Check if the admin_toolbar module is installed.
    foreach ($variables['items'] as $key => $item) {
      $gin_id = str_replace('.', '-', $key);
      $variables['items'][$key]['gin_id'] = $gin_id;
    }

    // Move config & help menu items to end.
    $to_move = [
      'system.admin_config',
      'help.main',
    ];
    foreach ($to_move as $id) {
      $index = array_search($id, array_keys($variables['items']));
      if (is_numeric($index)) {
        $variables['items'] += array_splice($variables['items'], $index, 1);
      }
    }
  }
}

/**
 * Implements hook_preprocess_menu__toolbar().
 */
function gin_toolbar_preprocess_menu__toolbar(&$variables) {

  // Get theme configs.

  /** @var \Drupal\gin\GinSettings $settings */
  $settings = \Drupal::classResolver(GinSettings::class);
  $logo_path = $settings
    ->getDefault('icon_path');
  $logo_default = $settings
    ->getDefault('icon_default');
  $variables['icon_default'] = $logo_default;
  if (!$logo_default) {
    $variables['icon_path'] = $logo_path;
  }

  // Expose Toolbar variant.
  $variables['toolbar_variant'] = $settings
    ->get('classic_toolbar');
}

/**
 * Implements hook_ckeditor_css_alter().
 */
function gin_toolbar_ckeditor_css_alter(array &$css) {
  $css[] = drupal_get_path('theme', 'gin') . '/dist/css/gin_accent.css';
  $css[] = drupal_get_path('theme', 'gin') . '/dist/css/gin_ckeditor.css';
}

/**
 * Set Gin_login CSS on top of all other CSS files.
 */
function gin_toolbar_css_alter(&$css, $assets) {

  // UPDATE THIS PATH TO YOUR MODULE'S CSS PATH.
  $path = drupal_get_path('theme', 'gin') . '/dist/css/gin_dialog.css';
  if (isset($css[$path])) {

    // Use anything greater than 100 to have it load after the theme
    // as CSS_AGGREGATE_THEME is set to 100.
    // Let's be on the safe side and assign a high number to it.
    $css[$path]['group'] = 101;
  }
}

/**
 * Implements hook_toolbar_alter().
 */
function gin_toolbar_toolbar_alter(&$items) {

  // Move the User tab to the end; devel uses 999 so up it.
  $items['user']['#weight'] = 1000;
  $items['administration']['tray']['toolbar_administration']['#pre_render'] = [
    [
      GinToolbar::class,
      'preRenderTray',
    ],
  ];
}

/**
 * Adds toolbar-specific attributes to the menu link tree.
 *
 * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
 *   The menu link tree to manipulate.
 *
 * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
 *   The manipulated menu link tree.
 */
function gin_toolbar_tools_menu_navigation_links(array $tree) {
  foreach ($tree as $element) {
    if ($element->subtree) {
      gin_toolbar_tools_menu_navigation_links($element->subtree);
    }
    $link = $element->link;

    // Get the non-localized title to make the icon class.
    $definition = $link
      ->getPluginDefinition();
    $element->options['attributes']['class'][] = 'toolbar-icon';
    $string = strtolower(str_replace([
      '.',
      ' ',
      '_',
    ], [
      '-',
      '-',
      '-',
    ], $definition['id']));
    $element->options['attributes']['class'][] = Html::cleanCssIdentifier('toolbar-icon-' . $string);
    $element->options['attributes']['title'] = $link
      ->getDescription();
  }
  return $tree;
}

/**
 * Implements hook_help().
 */
function gin_toolbar_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the gin_toolbar module.
    case 'help.page.gin_toolbar':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module changes the layout of the administration menu, and is actively compatible with <a href="@href" target="_blank">Gin Admin</a>.', [
        '@href' => 'https://www.drupal.org/project/gin',
      ]) . '</p>';
      return $output;
  }
}

/**
 * Helper function for check if Gin is active.
 */
function _gin_toolbar_gin_is_active() {

  // Check if permissions are given.
  if (!\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    return FALSE;
  }
  $logged_in = \Drupal::currentUser()
    ->isAuthenticated();
  $theme_handler = \Drupal::service('theme_handler')
    ->listInfo();

  // Check if set as frontend theme.
  $frontend_theme_name = \Drupal::config('system.theme')
    ->get('default');

  // Check if base themes are set.
  if (isset($theme_handler[$frontend_theme_name]->base_themes)) {
    $frontend_base_themes = $theme_handler[$frontend_theme_name]->base_themes;
  }

  // Add theme name to base theme array.
  $frontend_base_themes[$frontend_theme_name] = $frontend_theme_name;

  // Check if set as admin theme.
  $admin_theme_name = \Drupal::config('system.theme')
    ->get('admin');

  // Admin theme will have no value if is set to use the default theme.
  if ($admin_theme_name && isset($theme_handler[$admin_theme_name]->base_themes)) {
    $admin_base_themes = $theme_handler[$admin_theme_name]->base_themes;
    $admin_base_themes[$admin_theme_name] = $admin_theme_name;
  }
  else {
    $admin_base_themes = $frontend_base_themes;
  }

  // Check if Gin is activated in the frontend.
  if ($logged_in) {
    $gin_activated = array_key_exists('gin', $admin_base_themes);
  }
  else {
    $gin_activated = array_key_exists('gin', $frontend_base_themes);
  }

  // Is Gin in the active chain?
  $theme_activated = $gin_activated;
  return $theme_activated;
}

/**
 * Gets the admin theme setting.
 *
 * @param string $setting
 *   Setting name.
 *
 * @return mixed
 *   Return NULL if setting doesn't exist.
 */
function _gin_toolbar_get_admin_theme_setting($setting) {
  $admin_theme = \Drupal::configFactory()
    ->get('system.theme')
    ->get('admin');
  if (empty($admin_theme)) {
    $admin_theme = \Drupal::configFactory()
      ->get('system.theme')
      ->get('default');
  }
  return theme_get_setting($setting, $admin_theme);
}

Functions

Namesort descending Description
gin_toolbar_ckeditor_css_alter Implements hook_ckeditor_css_alter().
gin_toolbar_css_alter Set Gin_login CSS on top of all other CSS files.
gin_toolbar_help Implements hook_help().
gin_toolbar_page_attachments_alter Implements hook_preprocess_HOOK() for page_attachments.
gin_toolbar_preprocess_html Implements hook_preprocess_HOOK() for html.
gin_toolbar_preprocess_menu Implements hook_preprocess_menu().
gin_toolbar_preprocess_menu__toolbar Implements hook_preprocess_menu__toolbar().
gin_toolbar_theme_registry_alter Toolbar alter().
gin_toolbar_toolbar_alter Implements hook_toolbar_alter().
gin_toolbar_tools_menu_navigation_links Adds toolbar-specific attributes to the menu link tree.
_gin_toolbar_get_admin_theme_setting Gets the admin theme setting.
_gin_toolbar_gin_is_active Helper function for check if Gin is active.