File: /datos/www/fabricas.colombiatrade.com.co/public_html/modules/contrib/gin_toolbar/gin_toolbar.module
<?php
/**
* @file
* gin_toolbar.module
*/
/**
* Implements hook_preprocess_HOOK() for html.
*/
function gin_toolbar_preprocess_html(&$variables) {
// Are we relevant?
$gin_activated = _gin_toolbar_gin_is_active();
if ($gin_activated) {
// Get theme darkmode config.
$darkmode = _gin_toolbar_dark_mode_enabled();
$preset_accent_color = _gin_toolbar_preset_accent_color();
$toolbar = _gin_toolbar_toolbar();
$high_contrast_mode = _gin_toolbar_highcontrastmode_enabled();
// Check if darkmode is enabled.
if ($darkmode === TRUE) {
$variables['attributes']['class'][] = 'gin--dark-mode';
}
// Set accent color.
if ($preset_accent_color) {
$variables['attributes']['data-gin-accent'] = $preset_accent_color;
}
// High contast mode.
if ($high_contrast_mode === TRUE) {
$variables['attributes']['class'][] = 'gin--high-contrast-mode';
}
// Only add gin--classic-toolbar class if user has permission.
if (!\Drupal::currentUser()->hasPermission('access toolbar')) {
return;
}
// Check if classic_toolbar is active.
if ($toolbar === 1) {
$variables['attributes']['class'][] = 'gin--classic-toolbar';
}
// If modern horizontal Toolbar is active.
elseif ($toolbar === 'horizontal') {
$variables['attributes']['class'][] = 'gin--horizontal-toolbar';
}
// Sidebar toolbar.
else {
$variables['attributes']['class'][] = 'gin--vertical-toolbar';
}
}
}
/**
* Implements hook_preprocess_HOOK() for page_attachments.
*/
function gin_toolbar_page_attachments_alter(&$page) {
// Are we relevant?
$gin_activated = _gin_toolbar_gin_is_active();
if ($gin_activated) {
// Get theme configs.
$darkmode = _gin_toolbar_dark_mode_enabled();
$preset_accent_color = _gin_toolbar_preset_accent_color();
$accent_color = _gin_toolbar_accent_color();
$preset_focus_color = _gin_toolbar_get_admin_theme_setting('preset_focus_color');
$focus_color = _gin_toolbar_get_admin_theme_setting('focus_color');
$toolbar = _gin_toolbar_toolbar();
$highcontrastmode = _gin_toolbar_highcontrastmode_enabled();
// Define darkmode class.
$darkmode_class = 'gin--dark-mode';
$highcontrastmode_class = 'gin--high-contrast-mode';
// Attach the init script.
$page['#attached']['library'][] = 'gin/gin_init';
if ($toolbar === 1) {
// Attach the classic toolbar styles.
$page['#attached']['library'][] = 'gin/gin_classic_toolbar';
}
elseif ($toolbar === 'horizontal') {
// Attach the horizontal toolbar styles.
$page['#attached']['library'][] = 'gin/gin_horizontal_toolbar';
}
else {
// Attach toolbar styles.
$page['#attached']['library'][] = 'gin/gin_toolbar';
}
// Attach accent overrides CSS.
$page['#attached']['library'][] = 'gin/gin_accent';
// Add library for dialog.
$page['#attached']['library'][] = 'gin/gin_dialog';
$page['#attached']['library'][] = 'claro/claro.drupal.dialog';
// Expose settings to JS.
$page['#attached']['drupalSettings']['gin']['darkmode'] = $darkmode;
$page['#attached']['drupalSettings']['gin']['darkmode_class'] = $darkmode_class;
$page['#attached']['drupalSettings']['gin']['preset_accent_color'] = $preset_accent_color;
$page['#attached']['drupalSettings']['gin']['accent_color'] = $accent_color;
$page['#attached']['drupalSettings']['gin']['preset_focus_color'] = $preset_focus_color;
$page['#attached']['drupalSettings']['gin']['focus_color'] = $focus_color;
$page['#attached']['drupalSettings']['gin']['highcontrastmode'] = $highcontrastmode;
$page['#attached']['drupalSettings']['gin']['highcontrastmode_class'] = $highcontrastmode_class;
}
}
/**
* Toolbar alter().
*/
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';
}
/**
* Implements hook_preprocess_menu().
*/
function gin_toolbar_preprocess_menu(&$variables) {
if (isset($variables['theme_hook_original']) && $variables['theme_hook_original'] == 'menu__toolbar__admin') {
$moduleHandler = \Drupal::service('module_handler');
// Check if the admin_toolbar module is installed.
foreach ($variables['items'] as $key => $item) {
$gin_id = str_replace('.', '-', $key);
$variables['items'][$key]['gin_id'] = $gin_id;
if ($moduleHandler->moduleExists('admin_toolbar')) {
$variables['items'][$key]['gin_admin_toolbar_module'] = TRUE;
}
}
}
}
/**
* Implements hook_preprocess_menu__toolbar().
*/
function gin_toolbar_preprocess_menu__toolbar(&$variables) {
// Get theme configs.
$icon_path = _gin_toolbar_get_admin_theme_setting('icon_path');
$icon_default = _gin_toolbar_get_admin_theme_setting('icon_default');
$variables['default_icon'] = $icon_default;
if (!$icon_default) {
$variables['icon_path'] = $icon_path;
}
}
/**
* Implements hook_ckeditor_css_alter().
*/
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';
}
/**
* Set Gin_login CSS on top of all other CSS files.
*/
function gin_toolbar_css_alter(&$css, $assets) {
// UPDATE THIS PATH TO YOUR MODULE'S CSS PATH.
$path = drupal_get_path('theme', 'gin') . '/dist/css/gin_dialog.css';
if (isset($css[$path])) {
// Use anything greater than 100 to have it load after the theme
// as CSS_AGGREGATE_THEME is set to 100.
// Let's be on the safe side and assign a high number to it.
$css[$path]['group'] = 101;
}
}
/**
* Helper function for check if Gin is active.
*/
function _gin_toolbar_gin_is_active() {
$logged_in = \Drupal::currentUser()->isAuthenticated();
$theme_handler = \Drupal::service('theme_handler')->listInfo();
// Check if set as frontend theme.
$frontend_theme_name = \Drupal::config('system.theme')->get('default');
// Check if base themes are set.
if (isset($theme_handler[$frontend_theme_name]->base_themes)) {
$frontend_base_themes = $theme_handler[$frontend_theme_name]->base_themes;
}
// Add theme name to base theme array.
$frontend_base_themes[$frontend_theme_name] = $frontend_theme_name;
// Check if set as admin theme.
$admin_theme_name = \Drupal::config('system.theme')->get('admin');
// Admin theme will have no value if is set to use the default theme.
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;
}
// Check if Gin/Claro is activated in the frontend.
if ($logged_in) {
$gin_activated = array_key_exists('gin', $admin_base_themes);
$claro_activated = array_key_exists('claro', $admin_base_themes) && !array_key_exists('gin', $admin_base_themes);
}
else {
$gin_activated = array_key_exists('gin', $frontend_base_themes);
$claro_activated = array_key_exists('claro', $frontend_base_themes) && !array_key_exists('gin', $frontend_base_themes);
}
// Is Gin or Claro in the active chain?
$theme_activated = $gin_activated || $claro_activated;
return $theme_activated;
}
/**
* Gets the admin theme setting.
*
* @param string $setting
* Setting name.
*
* @return mixed
* Return NULL if setting doesn't exist.
*/
function _gin_toolbar_get_admin_theme_setting($setting) {
$admin_theme = \Drupal::configFactory()->get('system.theme')->get('admin');
return theme_get_setting($setting, $admin_theme);
}
/**
* Check user has enabled darkmode helper function.
*/
function _gin_toolbar_user__get_user_id() {
$uid = Drupal::currentUser()->id();
if (empty($uid)) {
return NULL;
}
else {
return $uid;
}
}
/**
* Check user has enabled darkmode helper function.
*/
function _gin_toolbar_user__overrides_enabled() {
if (_gin_toolbar_get_admin_theme_setting('show_user_theme_settings') !== TRUE) {
return FALSE;
}
$uid = _gin_toolbar_user__get_user_id();
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
$enableUserSetting = $userData->get('gin', $uid, 'enable_user_settings');
$enableUserSetting = $enableUserSetting === 1 || $enableUserSetting === '1' ? TRUE : $enableUserSetting;
$enableUserSetting = $enableUserSetting === 0 || $enableUserSetting === '0' ? FALSE : $enableUserSetting;
if ($uid && $enableUserSetting === TRUE) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Enable darkmode helper function.
*/
function _gin_toolbar_dark_mode_enabled() {
// Get user config.
if (_gin_toolbar_user__overrides_enabled()) {
$darkmode = _gin_toolbar_user__get_darkmode_enabled();
}
// Get general config.
else {
$darkmode = _gin_toolbar_get_admin_theme_setting('enable_darkmode');
}
// Convert older versions to boolean.
$darkmode = $darkmode === 0 || $darkmode === '0' ? FALSE : $darkmode;
$darkmode = $darkmode === 1 || $darkmode === '1' ? TRUE : $darkmode;
return $darkmode;
}
/**
* Check user has enabled darkmode helper function.
*/
function _gin_toolbar_user__get_darkmode_enabled() {
$uid = _gin_toolbar_user__get_user_id();
if ($uid) {
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
return $userData->get('gin', $uid, 'enable_darkmode');
}
else {
return FALSE;
}
}
/**
* Accent color helper function.
*/
function _gin_toolbar_preset_accent_color() {
if (_gin_toolbar_user__overrides_enabled()) {
return _gin_toolbar_user__get_preset_accentcolor();
}
else {
return _gin_toolbar_get_admin_theme_setting('preset_accent_color');
}
}
/**
* User Accent color helper function.
*/
function _gin_toolbar_user__get_preset_accentcolor() {
$uid = _gin_toolbar_user__get_user_id();
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
if ($userData->get('gin', $uid, 'preset_accent_color') !== NULL) {
return $userData->get('gin', $uid, 'preset_accent_color');
}
else {
return NULL;
}
}
/**
* Accent color helper function.
*/
function _gin_toolbar_accent_color() {
if (_gin_toolbar_user__overrides_enabled() && _gin_toolbar_user__accentcolor()) {
return _gin_toolbar_user__accentcolor();
}
else {
return _gin_toolbar_get_admin_theme_setting('accent_color');
}
}
/**
* User Accent color helper function.
*/
function _gin_toolbar_user__accentcolor() {
$uid = _gin_toolbar_user__get_user_id();
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
if ($uid && $userData->get('gin', $uid, 'accent_color') !== NULL) {
return $userData->get('gin', $uid, 'accent_color');
}
else {
return NULL;
}
}
/**
* Accent color presets helper function.
*/
function _gin_toolbar_get_accent_color_presets() {
return [
'blue' => t('Claro Blue (Default)'),
'teal' => t('Teal'),
'dark_purple' => t('Dark Purple'),
'purple' => t('Purple'),
'green' => t('Green'),
'red' => t('Red'),
'orange' => t('Orange'),
'yellow' => t('Yellow'),
'pink' => t('Pink'),
'custom' => t('Custom'),
];
}
/**
* Accent color preset label helper function.
*/
function _gin_toolbar_get_accent_color_preset_label($key) {
$options = _gin_toolbar_get_accent_color_presets();
if (!empty($options[$key])) {
return $options[$key];
}
return '';
}
/**
* Classic Toolbar helper function.
*/
function _gin_toolbar_toolbar() {
// Get user config.
if (_gin_toolbar_user__overrides_enabled()) {
$uid = _gin_toolbar_user__get_user_id();
$toolbar = _gin_toolbar_user__toolbar();
}
// Get general config.
else {
$toolbar = _gin_toolbar_get_admin_theme_setting('classic_toolbar');
}
// Convert older versions to boolean.
$toolbar = $toolbar === TRUE || $toolbar === 'true' || $toolbar === '1' || $toolbar === 1 ? 1 : $toolbar;
return $toolbar;
}
/**
* User Classic Toolbar helper function.
*/
function _gin_toolbar_user__toolbar() {
$uid = _gin_toolbar_user__get_user_id();
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
if ($userData->get('gin', $uid, 'classic_toolbar') !== NULL) {
return $userData->get('gin', $uid, 'classic_toolbar');
}
else {
return NULL;
}
}
/**
* Enable high contrast mode helper function.
*/
function _gin_toolbar_highcontrastmode_enabled() {
// Get user config.
if (_gin_toolbar_user__overrides_enabled()) {
$uid = _gin_toolbar_user__get_user_id();
$hcm = _gin_toolbar_user__get_highcontrastmode_enabled();
// Convert older versions to boolean.
$hcm = $hcm === 0 || $hcm === '0' ? FALSE : $hcm;
$hcm = $hcm === 1 || $hcm === '1' ? TRUE : $hcm;
}
// Get general config.
else {
$hcm = theme_get_setting('high_contrast_mode', 'gin');
}
return $hcm;
}
/**
* Check user has enabled high contrast mode helper function.
*/
function _gin_toolbar_user__get_highcontrastmode_enabled() {
$uid = _gin_toolbar_user__get_user_id();
if ($uid) {
/** @var \Drupal\user\UserDataInterface $userData */
$userData = Drupal::service('user.data');
return $userData->get('gin', $uid, 'high_contrast_mode');
}
else {
return FALSE;
}
}