Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
Created February 25, 2017 06:57
Show Gist options
  • Save DejanBelic/8889c60930a99d85935c6f8b535fa6d6 to your computer and use it in GitHub Desktop.
Save DejanBelic/8889c60930a99d85935c6f8b535fa6d6 to your computer and use it in GitHub Desktop.
Add node title as body class in drupal 8.
function hook_preprocess_html(&$variables) {
// Add node title as body class.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$node = $node->getTranslation('en');
$title = $node->label();
$title = str_replace(' ', '-', $title);
$title = str_replace('_', '-', $title);
$variables['title_en'] = strtolower($title);
}
@ipwa
Copy link

ipwa commented Nov 21, 2020

Why not send it straight to the body class, replace last line with:
$variables['attributes']['class'][] = strtolower($title);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment