Share icon
nodeid

Hey, again with new solution with new article, in this article we are going to see how we add node ID class with node type to drupal 8 as like drupal 7 format

{node-type}-node-{node-id}

In drupal 7 above format class displayed on node to differentiate with other nodes, but in drupal 8 by default it was not present so we can achieve this easily using hook_preprocess_html in drupal 8

function yourthemename_preprocess_html(&$variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if (is_object($node)) {
    $type = strtolower($node->type->entity->label());
    $variables['attributes']['class'][] =  $type .'-node-' . $node->id();
  }
}

above code will add class to node body tag, you need to just write this node in your yourtheme.theme file in your theme directory.

It is very useful if migrate drupal 7 to drupal 8 you can use the same css in some cases without wasting any time

hopes you find this article helpful

thanks

Add new comment

Restricted HTML