Skip to content

Instantly share code, notes, and snippets.

@woutersf
Last active December 22, 2016 06:22
Show Gist options
  • Save woutersf/54ad812b272edd21940aa2e19b1d78ab to your computer and use it in GitHub Desktop.
Save woutersf/54ad812b272edd21940aa2e19b1d78ab to your computer and use it in GitHub Desktop.
D8 Add class in preprocess conditionally (replaces menu_get_items)
<?php
//For nodes.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($node->getType() == 'article') { // Test the node type.
$is_video = $node->get('field_is_video')->value; // Get the field value of on of the node fields.
$class_suffix = $is_video ? 'video' : 'no-video';
$vars['attributes']['class'][] = Html::getClass($node->getType() . '--' . $class_suffix); //Add Class to the variables.
}
}
//For terms.
if ($term = \Drupal::routeMatch()->getParameter('taxonomy_term')) {
if ($term->getVocabularyId() == 'region') {
$vars['term'] = $term;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment