function olivero_preprocess_block

Same name in other branches
  1. 9 core/themes/olivero/olivero.theme \olivero_preprocess_block()
  2. 11.x core/themes/olivero/olivero.theme \olivero_preprocess_block()

Implements hook_preprocess_HOOK() for block.html.twig.

File

core/themes/olivero/olivero.theme, line 120

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';
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.