Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use light/dark favicons #497

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions common_design.theme
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function common_design_form_system_theme_settings_alter(&$form, FormStateInterfa
];
}

// Add a setting to use light/dark favicons.
$form['common_design']['use_light_dark_favicon'] = [
'#type' => 'checkbox',
'#title' => t('Use light/dark favicon'),
'#default_value' => theme_get_setting('use_light_dark_favicon') ?? FALSE,
];

// Load the node view modes.
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
$view_modes = $storage->loadByProperties([
Expand Down Expand Up @@ -1001,3 +1008,51 @@ function common_design_preprocess_datetime_wrapper(&$variables) {
$variables['title_attributes']['class'][] = 'visually-hidden';
}
}

/**
* Implements hook_page_attachments_alter().
*/
function common_design_page_attachments_alter(array &$page) {
$active_theme = \Drupal::theme()->getActiveTheme();

if (!theme_get_setting('use_light_dark_favicon')) {
return;
}

// Attach favicon from active theme.
if (file_exists($active_theme->getPath() . '/img/favicons/blue/favicon.ico')) {
$page['#attached']['html_head_link'][][] = [
'rel' => 'icon',
'href' => '/' . $active_theme->getPath() . '/img/favicons/blue/favicon.ico',
'type' => 'image/x-icon',
'media' => '(prefers-color-scheme: light)',
];
$page['#attached']['html_head_link'][][] = [
'rel' => 'icon',
'href' => '/' . $active_theme->getPath() . '/img/favicons/white/favicon.ico',
'type' => 'image/x-icon',
'media' => '(prefers-color-scheme: dark)',
];
}
else {
// Attach favicon from parent theme.
foreach ($active_theme->getBaseThemeExtensions() as $base_theme) {
if (file_exists($base_theme->getPath() . '/img/favicons/blue/favicon.ico')) {
$page['#attached']['html_head_link'][][] = [
'rel' => 'icon',
'href' => '/' . $base_theme->getPath() . '/img/favicons/blue/favicon.ico',
'type' => 'image/x-icon',
'media' => '(prefers-color-scheme: light)',
];
$page['#attached']['html_head_link'][][] = [
'rel' => 'icon',
'href' => '/' . $base_theme->getPath() . '/img/favicons/white/favicon.ico',
'type' => 'image/x-icon',
'media' => '(prefers-color-scheme: dark)',
];

return;
}
}
}
}
Binary file added img/favicons/blue/favicon.ico
Binary file not shown.
Binary file added img/favicons/white/favicon.ico
Binary file not shown.
Loading