View source
<?php
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\gin_toolbar\Render\Element\GinToolbar;
use Drupal\gin\GinSettings;
function gin_toolbar_preprocess_html(&$variables) {
if (!_gin_toolbar_gin_is_active()) {
return;
}
$settings = \Drupal::classResolver(GinSettings::class);
$toolbar = $settings
->get('classic_toolbar');
if ($settings
->get('enable_darkmode')) {
$variables['attributes']['class'][] = 'gin--dark-mode';
}
$variables['attributes']['data-gin-accent'] = $settings
->get('preset_accent_color');
if ($settings
->get('high_contrast_mode')) {
$variables['attributes']['class'][] = 'gin--high-contrast-mode';
}
if (!\Drupal::currentUser()
->hasPermission('access toolbar')) {
return;
}
$variables['attributes']['class'][] = 'gin--' . $toolbar . '-toolbar';
}
function gin_toolbar_page_attachments_alter(&$page) {
if (!_gin_toolbar_gin_is_active()) {
return;
}
$settings = \Drupal::classResolver(GinSettings::class);
$toolbar = $settings
->get('classic_toolbar');
$page['#attached']['library'][] = 'gin/gin_init';
if ($toolbar === 'classic') {
$page['#attached']['library'][] = 'gin/gin_classic_toolbar';
}
elseif ($toolbar === 'horizontal') {
$page['#attached']['library'][] = 'gin/gin_horizontal_toolbar';
}
else {
$page['#attached']['library'][] = 'gin/gin_toolbar';
}
$page['#attached']['library'][] = 'gin/gin_accent';
$page['#attached']['library'][] = 'gin/gin_dialog';
$page['#attached']['library'][] = 'claro/claro.drupal.dialog';
$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';
}
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';
}
function gin_toolbar_preprocess_menu(&$variables) {
if (isset($variables['theme_hook_original']) && $variables['theme_hook_original'] == 'menu__toolbar__admin') {
foreach ($variables['items'] as $key => $item) {
$gin_id = str_replace('.', '-', $key);
$variables['items'][$key]['gin_id'] = $gin_id;
}
$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);
}
}
}
}
function gin_toolbar_preprocess_menu__toolbar(&$variables) {
$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;
}
$variables['toolbar_variant'] = $settings
->get('classic_toolbar');
}
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';
}
function gin_toolbar_css_alter(&$css, $assets) {
$path = drupal_get_path('theme', 'gin') . '/dist/css/gin_dialog.css';
if (isset($css[$path])) {
$css[$path]['group'] = 101;
}
}
function gin_toolbar_toolbar_alter(&$items) {
$items['user']['#weight'] = 1000;
$items['administration']['tray']['toolbar_administration']['#pre_render'] = [
[
GinToolbar::class,
'preRenderTray',
],
];
}
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;
$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;
}
function gin_toolbar_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
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;
}
}
function _gin_toolbar_gin_is_active() {
if (!\Drupal::currentUser()
->hasPermission('access toolbar')) {
return FALSE;
}
$logged_in = \Drupal::currentUser()
->isAuthenticated();
$theme_handler = \Drupal::service('theme_handler')
->listInfo();
$frontend_theme_name = \Drupal::config('system.theme')
->get('default');
if (isset($theme_handler[$frontend_theme_name]->base_themes)) {
$frontend_base_themes = $theme_handler[$frontend_theme_name]->base_themes;
}
$frontend_base_themes[$frontend_theme_name] = $frontend_theme_name;
$admin_theme_name = \Drupal::config('system.theme')
->get('admin');
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;
}
if ($logged_in) {
$gin_activated = array_key_exists('gin', $admin_base_themes);
}
else {
$gin_activated = array_key_exists('gin', $frontend_base_themes);
}
$theme_activated = $gin_activated;
return $theme_activated;
}
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);
}