Created
October 11, 2016 18:59
-
-
Save jeremycaldwell/15f1c5016d81724fe4bc652272499377 to your computer and use it in GitHub Desktop.
Add custom class to body based on field value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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