You are here

toolbar_anti_flicker.module in Toolbar Anti-flicker 8.3

File

toolbar_anti_flicker.module
View source
<?php

use Drupal\Core\Url;
function toolbar_anti_flicker_page_attachments_alter(array &$page) {
  if (!\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    return;
  }
  if (version_compare(\DRUPAL::VERSION, 8.4, '<')) {
    $page['#attached']['library'][] = 'core/jquery.cookie';
  }
  else {
    $page['#attached']['library'][] = 'toolbar_anti_flicker/toolbar-anti-flicker';
  }
}

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 */
function toolbar_anti_flicker_preprocess_html(&$variables) {
  if (!\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    return;
  }
  $orientation = \Drupal::request()->cookies
    ->get('toolbar');
  if (version_compare(\DRUPAL::VERSION, 8.4, '<')) {
    $variables['attributes']['class'][] = 'toolbar-loading';
    $variables['attributes']['class'][] = 'toolbar-fixed';
  }
  else {

    // Removing extra class from D8.4 fixes.
    // It can be \Drupal\Core\Template\ArrayAccess or Array
    if (is_array($variables['attributes']['class'])) {
      if ($orientation == 'vertical') {
        unset($variables['attributes']['class']['toolbar-horizontal']);
      }
      else {
        unset($variables['attributes']['class']['toolbar-vertical']);
      }
    }
    else {
      if ($orientation == 'vertical') {
        $variables['attributes']
          ->removeClass('toolbar-horizontal');
      }
      else {
        $variables['attributes']
          ->removeClass('toolbar-vertical');
      }
    }
  }
  if (is_array($variables['attributes']['class'])) {
    if ($orientation == 'vertical') {
      $variables['attributes']['class'][] = 'toolbar-vertical';
    }
    else {
      $variables['attributes']['class'][] = 'toolbar-horizontal';
    }
  }
  else {
    if ($orientation == 'vertical') {
      $variables['attributes']
        ->addClass('toolbar-vertical');
    }
    else {
      $variables['attributes']
        ->addClass('toolbar-horizontal');
    }
  }

  // Remove left margin for closed Vertical mode.
  $activeTab = \Drupal::request()->cookies
    ->get('toolbarActiveTab');
  if (!empty($activeTab)) {
    $variables['attributes']['class'][] = 'toolbar-tray-open';
  }
  elseif (version_compare(\DRUPAL::VERSION, 8.4, '>')) {

    // It can be \Drupal\Core\Template\ArrayAccess or Array
    if (is_array($variables['attributes']['class'])) {
      unset($variables['attributes']['class']['toolbar-tray-open']);
    }
    else {
      $variables['attributes']
        ->removeClass('toolbar-tray-open');
    }
  }
}

/**
 * Implements hook_toolbar_alter().
 */
function toolbar_anti_flicker_toolbar_alter(&$items) {
  $wrapper_attributes =& $items['home']['#wrapper_attributes']['class'];

  // Show "Home" button by default and use CSS to toggle it.
  if (($key = array_search('hidden', $wrapper_attributes)) !== false) {
    unset($wrapper_attributes[$key]);
  }
}

/**
 * In-place a library.
 *
 * @param $target_library
 * @param $key
 * @param $replacement
 */
function toolbar_anti_flicker_replace_library(array &$target_library, $key, array $replacement) {
  $key_pos = array_search($key, array_keys($target_library));
  if ($key_pos !== false) {
    $key_pos++;
    $second_array = array_splice($target_library, $key_pos);
    $target_library = array_merge($target_library, $replacement, $second_array);
    unset($target_library[$key]);
  }
}

/**
 * Implements hook_library_info_alter().
 *
 * @TODO: Workaround for https://www.drupal.org/node/2642122
 */
function toolbar_anti_flicker_library_info_alter(array &$libraries, $module) {
  if ($module === 'toolbar' && isset($libraries['toolbar'])) {
    $module_path = '/' . drupal_get_path('module', 'toolbar_anti_flicker') . '/';

    // Version 8.4 introduced css fixes.
    if (version_compare(\DRUPAL::VERSION, 8.4, '<')) {
      unset($libraries['toolbar']['css']['component']['css/toolbar.module.css']);
      unset($libraries['toolbar']['css']['component']['css/toolbar.menu.css']);
      unset($libraries['toolbar']['css']['component']['css/toolbar.theme.css']);
      $libraries['toolbar']['css']['component'][$module_path . 'css/toolbar/toolbar.module.css'] = [];
      $libraries['toolbar']['css']['component'][$module_path . 'css/toolbar/toolbar.menu.css'] = [];
      $libraries['toolbar']['css']['component'][$module_path . 'css/toolbar/toolbar.theme.css'] = [];

      // @TODO: Register as lib in yml?
      // @TODO: better way to handle libs order.
      toolbar_anti_flicker_replace_library($libraries['toolbar']['js'], 'js/toolbar.js', [
        $module_path . 'js/toolbar/js/toolbar.js' => [],
      ]);
      toolbar_anti_flicker_replace_library($libraries['toolbar']['js'], 'js/views/BodyVisualView.js', [
        $module_path . 'js/toolbar/js/views/BodyVisualView.js' => [],
      ]);
      toolbar_anti_flicker_replace_library($libraries['toolbar']['js'], 'js/views/ToolbarVisualView.js', [
        $module_path . 'js/toolbar/js/views/ToolbarVisualView.js' => [],
      ]);
      toolbar_anti_flicker_replace_library($libraries['toolbar.escapeAdmin']['js'], 'js/escapeAdmin.js', [
        $module_path . 'js/toolbar/js/escapeAdmin.js' => [],
      ]);
    }
  }
}

// Add a placeholder to ensure the toolbar item always exist in HTML.
function toolbar_anti_flicker_toolbar() {
  $items['anti-flicker'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => t('&nbsp;'),
      '#url' => Url::fromRoute('<front>'),
    ],
    'tray' => [
      '#heading' => t('&nbsp;'),
      '#wrapper_attributes' => [
        'id' => 'toolbar-tray-anti-flicker',
      ],
      'content' => [
        '#theme' => 'item_list',
        '#items' => [
          \Drupal\Core\Link::fromTextAndUrl('&nbsp;', new Url('<front>', [], [])),
        ],
        '#attributes' => [
          'class' => [
            'toolbar-menu',
          ],
        ],
      ],
    ],
    '#weight' => 1000,
  ];
  return $items;
}