0

i have this code to include node-ID attributes to the body tag on the page. My question is ... how can i modify this code to get additional dynamic attributes for the BODY class? The most important for me are classes for the current vocabulary and taxonomy term, possibly a panel page.. I has been searching for few days and found only solution for node-ID.

Thank you many times for help.

 /**
 * Implements hook_preprocess_html().
 */
function enar_preprocess_html(&$variables) {
  $node = \Drupal::request()->attributes->get('node');
  if ($node) {
    $variables['attributes']['class'][] = 'page-node-' . $node->id();
    if (empty($node->get('body')->value)) {
        $variables['attributes']['class'][] = 'no-padding-main-content';
    }
  }



   // Don't display the site name twice on the front page (and potentially others)
  if (isset($variables['head_title_array']['title']) && isset($variables['head_title_array']['name']) && ($variables['head_title_array']['title'] == $variables['head_title_array']['name'])) {
    $variables['head_title'] = $variables['head_title_array']['name'];
  }


  // Preloader configuration
  $preloader = theme_get_setting('preloader');
  $variables['attributes']['class'][] = $preloader;
  $variables['preloader'] = $preloader;
  // Go to top configuration
  $goto = theme_get_setting('gototop');
  $variables['goto'] = $goto;
}

1 Answer 1

0

I think, i found the right solution below:

  if ($node) {
    $variables['attributes']['class'][] = 'page-node';
}


  if ($tid) {
    $variables['attributes']['class'][] = 'term-page';
}  

  if ($tid) {
    $variables['attributes']['class'][] = 'term-page-' . $tid->id();
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.