I am working on my theme in Drupal 8 and I want to add a class to my header div. I tried adding this to the region.vars.php file.
if ($region === 'header') {
$variables[header]['class'][] = 'navbar-right';
}
It's not working, though. This is the complete region.vars.php file.
<?php
/**
* @file
* region.vars.php
*/
use Drupal\Core\Template\Attribute;
/**
* Implements hook_preprocess_region().
*/
function bootstrap_preprocess_region(&$variables) {
$region = $variables['elements']['#region'];
$variables['region'] = $region;
$variables['content'] = $variables['elements']['#children'];
$theme = \Drupal::theme()->getActiveTheme()->getName();
// Content region.
if ($region === 'content') {
// @todo is this actually used properly?
$variables['theme_hook_suggestions'][] = 'region__no_wrapper';
}
// Help region.
elseif ($region === 'help' && !empty($variables['content'])) {
$content = $variables['content'];
$variables['content'] = array(
'icon' => array(
'#markup' => _bootstrap_icon('question-sign'),
),
'content' => array(
'#markup' => $content,
),
);
$variables['attributes']['class'][] = 'alert';
$variables['attributes']['class'][] = 'alert-info';
$variables['attributes']['class'][] = 'messages';
$variables['attributes']['class'][] = 'info';
}
// Support for "well" classes in regions.
static $wells;
if (!isset($wells)) {
foreach (system_region_list($theme) as $name => $title) {
$wells[$name] = bootstrap_setting('region_well-' . $name);
}
}
if (!empty($wells[$region])) {
$variables['attributes']['class'][] = $wells[$region];
}
if ($region === 'header') {
$variables[header]['class'][] = 'navbar-right';
}
}
I got most of this code from the Drupal 8 bootstrap theme. Everything else is working.
$variables[header]['class'][] = 'navbar-right';
should probably be$variables['attributes']['class'][] = 'navbar-right';
In the first code snippet.