Skip to content

Instantly share code, notes, and snippets.

@jeremycaldwell
Created October 11, 2016 18:59
Show Gist options
  • Save jeremycaldwell/15f1c5016d81724fe4bc652272499377 to your computer and use it in GitHub Desktop.
Save jeremycaldwell/15f1c5016d81724fe4bc652272499377 to your computer and use it in GitHub Desktop.
Add custom class to body based on field value.
function THEMENAME_preprocess_html(&$variables) {
// Get the value of the field and add it as a class on <body>.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->field_layout) {
$layout_style = $node->field_layout->value;
$vars['attributes']['class'][] = 'layout-' . str_replace('_', '-', $layout_style);
} else {
$variables['attributes']['class'][] = 'layout-CUSTOM-CLASS';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment