You are here

toolbar_anti_flicker.module in Toolbar Anti-flicker 8.2

File

toolbar_anti_flicker.module
View source
<?php

function toolbar_anti_flicker_page_attachments_alter(array &$page) {
  if (!\Drupal::currentUser()
    ->hasPermission('access toolbar')) {
    return;
  }
  $page['#attached']['library'][] = 'core/jquery.cookie';
}

/**
 * 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');
  $variables['attributes']['class'][] = 'toolbar-loading';
  $variables['attributes']['class'][] = 'toolbar-fixed';
  if ($orientation == 'vertical') {
    $variables['attributes']['class'][] = 'toolbar-vertical';
  }
  else {
    $variables['attributes']['class'][] = 'toolbar-horizontal';
  }

  // Remove left margin for closed Vertical mode.
  $activeTab = \Drupal::request()->cookies
    ->get('toolbarActiveTab');
  if (!empty($activeTab)) {
    $variables['attributes']['class'][] = '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') . '/';
    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' => [],
    ]);
  }
}