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';
}
}
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 {
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');
}
}
$activeTab = \Drupal::request()->cookies
->get('toolbarActiveTab');
if (!empty($activeTab)) {
$variables['attributes']['class'][] = 'toolbar-tray-open';
}
elseif (version_compare(\DRUPAL::VERSION, 8.4, '>')) {
if (is_array($variables['attributes']['class'])) {
unset($variables['attributes']['class']['toolbar-tray-open']);
}
else {
$variables['attributes']
->removeClass('toolbar-tray-open');
}
}
}
function toolbar_anti_flicker_toolbar_alter(&$items) {
$wrapper_attributes =& $items['home']['#wrapper_attributes']['class'];
if (($key = array_search('hidden', $wrapper_attributes)) !== false) {
unset($wrapper_attributes[$key]);
}
}
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]);
}
}
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') . '/';
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'] = [];
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' => [],
]);
}
}
}
function toolbar_anti_flicker_toolbar() {
$items['anti-flicker'] = [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#title' => t(' '),
'#url' => Url::fromRoute('<front>'),
],
'tray' => [
'#heading' => t(' '),
'#wrapper_attributes' => [
'id' => 'toolbar-tray-anti-flicker',
],
'content' => [
'#theme' => 'item_list',
'#items' => [
\Drupal\Core\Link::fromTextAndUrl(' ', new Url('<front>', [], [])),
],
'#attributes' => [
'class' => [
'toolbar-menu',
],
],
],
],
'#weight' => 1000,
];
return $items;
}