function PreprocessHooks::preprocessHtml
Same name and namespace in other branches
- 11.x core/themes/default_admin/src/Hook/PreprocessHooks.php \Drupal\default_admin\Hook\PreprocessHooks::preprocessHtml()
Implements hook_preprocess_HOOK() for HTML.
Attributes
#[Hook('preprocess_html')]
File
-
core/
themes/ default_admin/ src/ Hook/ PreprocessHooks.php, line 605
Class
- PreprocessHooks
- Provides preprocess implementations.
Namespace
Drupal\default_admin\HookCode
public function preprocessHtml(array &$variables) : void {
// Check if IMCE is active.
if (isset($variables['attributes']['class']) && in_array('imce-page', $variables['attributes']['class'], TRUE)) {
return;
}
// Get theme settings.
$settings = Settings::getInstance();
// Old way to set accent color.
$variables['html_attributes']['data-accent'] = $settings->get('preset_accent_color');
// New way to set accent color.
$accent_colors = Helper::accentColors();
// The theme settings are not available while Drupal is being installed,
// because they are stored in configuration. Fall back to an empty string so
// that the preset can be used as an array offset.
$preset = $settings->get('preset_accent_color') ?? '';
$accent_color = '';
if ($preset === 'custom' && $settings->get('accent_color')) {
$accent_color = $settings->get('accent_color');
}
elseif (isset($accent_colors[$preset]['hex'])) {
$accent_color = $accent_colors[$preset]['hex'];
}
if ($accent_color) {
$variables['html_attributes']['style'] = '--accent-base: ' . $accent_color . ';';
}
// Set focus color.
$variables['html_attributes']['data-admin-focus'] = $settings->get('preset_focus_color');
// High contrast mode.
if ($settings->get('high_contrast_mode')) {
$variables['html_attributes']['class'][] = 'high-contrast-mode';
}
// Set layout density.
$variables['html_attributes']['data-layout-density'] = $settings->get('layout_density');
// Edit form? Use the new admin Edit form layout.
if (Helper::isContentForm()) {
$variables['attributes']['class'][] = 'edit-form';
}
// Checking permissions requires a database connection, which does not
// exist yet while Drupal is being installed. Installer pages have neither
// a toolbar nor a navigation, so there is nothing to add.
if (InstallerKernel::installationAttempted()) {
return;
}
// Only add toolbar/navigation class if user has permission.
if (!$this->currentUser
->hasPermission('access toolbar') && !$this->currentUser
->hasPermission('access navigation')) {
return;
}
// Check if Navigation module is active.
if ($this->moduleHandler
->moduleExists('navigation')) {
$variables['attributes']['class'][] = 'admin--navigation';
}
else {
// Set toolbar class.
$variables['attributes']['class'][] = 'admin--toolbar';
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.