function olivero_preprocess_block in Drupal 9
Implements hook_preprocess_HOOK() for block.html.twig.
File
- core/
themes/ olivero/ olivero.theme, line 114 - Functions to support theming in the Olivero theme.
Code
function olivero_preprocess_block(&$variables) {
if (!empty($variables['elements']['#id'])) {
/** @var \Drupal\block\BlockInterface $block */
$block = \Drupal::entityTypeManager()
->getStorage('block')
->load($variables['elements']['#id']);
if ($block) {
$region = $block
->getRegion();
if ($variables['base_plugin_id'] === 'system_menu_block') {
$variables['content']['#attributes']['region'] = $region;
if ($region === 'sidebar') {
$variables['#attached']['library'][] = 'olivero/menu-sidebar';
}
}
if ($variables['base_plugin_id'] === 'search_form_block') {
if ($region === 'primary_menu') {
$variables['#attached']['library'][] = 'olivero/search-narrow';
$variables['content']['actions']['submit']['#theme_wrappers'] = [
'input__submit__header_search',
];
}
elseif ($region === 'secondary_menu') {
$variables['#attached']['library'][] = 'olivero/search-wide';
$variables['content']['actions']['submit']['#theme_wrappers'] = [
'input__submit__header_search',
];
}
}
}
}
if ($variables['plugin_id'] === 'system_branding_block') {
$site_branding_color = theme_get_setting('site_branding_bg_color');
if ($site_branding_color && $site_branding_color !== 'default') {
$variables['attributes']['class'][] = 'site-branding--bg-' . $site_branding_color;
}
}
// Add a primary-nav class to main menu navigation block.
if ($variables['plugin_id'] === 'system_menu_block:main') {
$variables['attributes']['class'][] = 'primary-nav';
}
}