View source
<?php
use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Html;
function toolbar_themes_get_theme_definitions() {
if ($cache = \Drupal::cache()
->get('toolbar_themes:definitions')) {
$definitions = $cache->data;
}
else {
$theme_handler = \Drupal::service('theme_handler');
$module_handler = \Drupal::service('module_handler');
$discovery = new YamlDiscovery('toolbar_themes', $module_handler
->getModuleDirectories() + $theme_handler
->getThemeDirectories());
$definitions = $discovery
->findAll();
if (!empty($definitions)) {
\Drupal::cache()
->set('toolbar_themes:definitions', $definitions);
}
}
return $definitions;
}
function toolbar_themes_get_library_names() {
$libraries = [];
$definitions = toolbar_themes_get_theme_definitions();
foreach ($definitions as $provider => $provider_themes) {
foreach ($provider_themes as $theme_key => $theme_values) {
$libraries[$theme_key] = $theme_values['libraries'];
}
}
return $libraries;
}
function toolbar_themes_get_theme_definition($theme) {
$theme_definition = [];
$definitions = toolbar_themes_get_theme_definitions();
foreach ($definitions as $provider => $provider_themes) {
foreach ($provider_themes as $theme_key => $theme_values) {
if ($theme == $theme_key) {
$theme_values['provider'] = $provider;
$theme_definition = $theme_values;
}
}
}
return $theme_definition;
}
function toolbar_themes_library_info_alter(&$libraries, $extension) {
if ($extension === 'toolbar') {
unset($libraries['toolbar']['css']);
unset($libraries['toolbar.menu']['css']);
}
if ($extension === 'admin_toolbar') {
if (isset($libraries['toolbar.tree'])) {
unset($libraries['toolbar.tree']);
}
}
}
function toolbar_themes_toolbar_alter(&$items) {
if (\Drupal::config('toolbar_themes.settings')
->get('tabs') === 0) {
foreach ($items as $id => $item) {
if ($id !== 'administration') {
unset($items[$id]);
}
}
}
}
function toolbar_themes_theme_registry_alter(&$theme_registry) {
$config = \Drupal::config('toolbar_themes.settings');
$default_theme = $config
->get('default_theme');
$path = drupal_get_path('module', 'toolbar_themes');
$theme_registry['toolbar']['template'] = 'toolbar-themes--toolbar';
$theme_registry['toolbar']['path'] = $path . '/templates';
$theme_registry['toolbar']['preprocess functions'][] = 'toolbar_themes_preprocess_toolbar_themes__toolbar';
$theme_registry['menu__toolbar']['template'] = 'toolbar-themes--menu';
$theme_registry['menu__toolbar']['path'] = $path . '/templates';
$theme_registry['menu__toolbar']['preprocess functions'][] = 'toolbar_themes_preprocess_toolbar_themes__menu';
$theme_registry['menu__toolbar__admin'] = $theme_registry['menu__toolbar'];
$theme_handler = \Drupal::service('theme_handler');
$base_theme = [];
$toolbar_implementation = '';
$menu_implementation = '';
$default_theme = toolbar_themes_get_theme_definition($default_theme);
if (isset($default_theme['base_theme'])) {
$base_theme = toolbar_themes_get_theme_definition($default_theme['base_theme']);
}
if (isset($default_theme['template_toolbar'])) {
$toolbar_implementation = $default_theme;
}
elseif (!empty($base_theme)) {
if (isset($base_theme['template_toolbar'])) {
$toolbar_implementation = $base_theme;
}
}
if (!empty($toolbar_implementation)) {
$provider_toolbar = $toolbar_implementation['provider'];
if ($theme_handler
->themeExists($provider_toolbar)) {
$provider_path = drupal_get_path('theme', $provider_toolbar);
}
else {
$provider_path = drupal_get_path('module', $provider_toolbar);
}
if (isset($toolbar_implementation['path']) && !empty($toolbar_implementation['path'])) {
$template_toolbar_parts = explode('/', $toolbar_implementation['template_toolbar']);
$template_toolbar = array_pop($template_toolbar_parts);
$template_toolbar_path = implode('/', $template_toolbar_parts);
$preprocess_toolbar = $provider_toolbar . '_preprocess_' . strtr($template_toolbar, '-', '_');
$theme_registry['toolbar']['template'] = $template_toolbar;
$theme_registry['toolbar']['path'] = $provider_path . '/' . $toolbar_implementation['path'] . '/' . $template_toolbar_path;
$theme_registry['toolbar']['preprocess functions'][] = $preprocess_toolbar;
}
}
if (isset($default_theme['template_toolbar_menu'])) {
$menu_implementation = $default_theme;
}
elseif (!empty($base_theme)) {
if (isset($base_theme['template_toolbar_menu'])) {
$menu_implementation = $base_theme;
}
}
if (!empty($menu_implementation)) {
$provider_menu = $menu_implementation['provider'];
if ($theme_handler
->themeExists($provider_menu)) {
$provider_path = drupal_get_path('theme', $provider_menu);
}
else {
$provider_path = drupal_get_path('module', $provider_menu);
}
if (isset($menu_implementation['path']) && !empty($menu_implementation['path'])) {
$template_menu_parts = explode('/', $menu_implementation['template_toolbar_menu']);
$template_menu = array_pop($template_menu_parts);
$template_menu_path = implode('/', $template_menu_parts);
$preprocess_menu = $provider_menu . '_preprocess_' . strtr($template_menu, '-', '_');
$theme_registry['menu__toolbar']['template'] = $template_menu;
$theme_registry['menu__toolbar']['path'] = $provider_path . '/' . $menu_implementation['path'] . '/' . $template_menu_path;
$theme_registry['menu__toolbar']['preprocess functions'][] = $preprocess_menu;
$theme_registry['menu__toolbar__admin'] = $theme_registry['menu__toolbar'];
}
}
}
function toolbar_themes_preprocess_html(&$variables) {
$config = \Drupal::config('toolbar_themes.settings');
$variables['attributes']['class'][] = 'toolbar-themes';
$variables['attributes']['class'][] = $config
->get('tabs') === 1 ? 'toolbar-has-tabs' : 'toolbar-no-tabs';
$variables['attributes']['class'][] = $config
->get('icons') === 1 ? 'toolbar-has-icons' : 'toolbar-no-icons';
$admin_theme = \Drupal::config('system.theme')
->get('admin');
$variables['attributes']['class'][] = 'toolbar-themes-admin-theme--' . Html::cleanCssIdentifier($admin_theme);
if (\Drupal::moduleHandler()
->moduleExists('toolbar_anti_flicker') === TRUE) {
$variables['attributes']['class'][] = 'toolbar-anti-flicker';
}
}
function toolbar_themes_preprocess_toolbar_themes__toolbar(&$variables) {
$config = \Drupal::config('toolbar_themes.settings');
$path = base_path() . drupal_get_path('module', 'toolbar_themes');
$variables['tray.content_attributes'] = new Attribute();
$variables['attributes']['class'][] = 'toolbar-themes-toolbar';
$variables['show_icons'] = $config
->get('icons');
$variables['show_tabs'] = $config
->get('tabs');
if ($variables['show_tabs'] === 1) {
if (isset($variables['tabs'])) {
foreach ($variables['tabs'] as $item_key => $item_values) {
if ($item_key === 'home') {
$variables['tabs'][$item_key]['attributes']
->addClass('hidden');
}
if (isset($item_values['link']['#markup']) && $item_values['link']['#markup'] === '<a href="/" class="toolbar-item"></a>') {
$variables['tabs'][$item_key]['attributes']
->addClass('hidden');
}
}
}
}
$default_theme = $config
->get('default_theme');
$library_names = toolbar_themes_get_library_names();
foreach ($library_names as $library_key => $libraries) {
if ($default_theme === $library_key) {
foreach ($libraries as $type => $library_name) {
if ($type !== 'icons') {
$variables['#attached']['library'][] = $library_name;
}
if ($variables['show_icons'] === 1) {
$variables['#attached']['library'][] = $library_name;
}
}
}
}
$drupalSettings = [
'icons' => $variables['show_icons'],
'tabs' => $variables['show_tabs'],
'path' => $path,
];
$variables['#attached']['drupalSettings']['toolbar'] = $drupalSettings;
$variables['#attached']['library'][] = 'toolbar_themes/base';
$variables['base_theme'] = [];
$variables['default_theme'] = toolbar_themes_get_theme_definition($default_theme);
if (isset($variables['default_theme']['base_theme'])) {
$variables['base_theme'] = toolbar_themes_get_theme_definition($variables['default_theme']['base_theme']);
}
}
function toolbar_themes_preprocess_toolbar_themes__base_toolbar(&$variables) {
$config = \Drupal::config('toolbar_themes.settings');
$path = base_path() . drupal_get_path('module', 'toolbar_themes');
$variables['show_icons'] = $config
->get('icons');
if ($variables['show_icons'] === 1) {
$icons_path = $variables['base_theme']['path'] . '/' . $variables['base_theme']['icons'];
if (!empty($icons_path)) {
$grunticon_loader = [
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'src' => $path . '/' . $icons_path . '/grunticon.loader.js',
],
];
$variables['#attached']['html_head'][] = [
$grunticon_loader,
'grunticon_loader',
];
$grunticon_fallback = [
'#type' => 'html_tag',
'#tag' => 'link',
'#noscript' => TRUE,
'#attributes' => [
'href' => $path . '/' . $icons_path . '/icons.fallback.css',
'rel' => 'stylesheet',
],
];
$variables['#attached']['html_head'][] = [
$grunticon_fallback,
'grunticon_fallback',
];
$variables['#attached']['library'][] = 'toolbar_themes/grunticon_load';
}
}
$variables['show_tabs'] = $config
->get('tabs');
if ($variables['show_tabs'] === 1) {
if (isset($variables['tabs'])) {
foreach ($variables['tabs'] as $item_key => $item_values) {
if ($variables['show_icons'] === 1) {
$variables['tabs'][$item_key]['icon_attributes'] = new Attribute();
$item_classes = $item_values['link']['#attributes']['class'];
$variables['tabs'][$item_key]['icon_attributes']
->addClass($item_classes)
->removeClass('toolbar-item', 'trigger');
}
}
}
}
}
function toolbar_themes_preprocess_toolbar_themes__menu(&$variables) {
$show_icons = \Drupal::config('toolbar_themes.settings')
->get('icons');
foreach ($variables['items'] as $item_key => $item_values) {
$variables['items'][$item_key]['show_icon'] = 0;
$attr = $item_values['url']
->getOption('attributes') ?: [];
$item_classes = isset($attr['class']) ? $attr['class'] : '';
$item_values['url']
->setOption('attributes', [
'class' => [
'toolbar-menu__link',
],
]);
if ($show_icons === 1) {
$variables['items'][$item_key]['icon_attributes'] = new Attribute();
$variables['items'][$item_key]['icon_attributes']
->addClass($item_classes);
$variables['items'][$item_key]['show_icon'] = 1;
}
}
}