Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By simone960 on
In Drupal 7, there a class e.g "path-node-17" added to the body tag so that we can target a particular node easily but this is missing in Drupal 8, I wonder how to get this node id class output in the tag in Drupal 8.
Comments
From my theme, quick and dirty does the trick
theme name.theme :
html.html.twig :
Note that this works for all paths, if you just want the node id thing, you can most likely lever
\Drupal::request()->attributes->get('node');
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Awesome !
Awesome ! It works well ! I think this should be included in core. Thanks !
This solution not work for my
This solution not work for my theme (bootstrap subtheme), so I change
$path = \Drupal::request()->getPathInfo();
to
$path = \Drupal::service('path.current')->getPath();
Its work fine and i have "path-node-NID" body class.
Thanks
For bootstrap sub-theme it works fine.
Thanks serpforge
Thanks!
It works for me
Perfect
It works!
Got error
Thanks to all you who made efforts.
:)
(taxonomy) as css classes please?
Hi Jeff
Is there a way to expand this to also add tags (taxonomy) as css classes please?
Worked for me
Worked for me.
Worked perfectly. Thank you.
Worked perfectly. Thank you.
Thank You,
Shashwat Purav
This worked
Thanks! Saved me a lot of time!
Another method
in
themename.theme
:Works for me
This works for me.
Works perfectly!
Thanks so much...
Great, thank You!
Great, thank You!
Theme system became more complex of course.
seems better
Yes this piece of code is simple and works for me. Do not have to include additional library or class. Cool !!!
Works well, thank you
Works well, thank you
If Windows is the answer, it must have been a stupid question. -- Filip Van Raemdonck
Works perfectly, thanks!
Works perfectly, thanks!
Just the Node ID?
If you just want the node id in a body class you can do this in preprocess, this is probably the best way to get a simple unique class for the node page:
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Works!
Thanks, that works for me.
Not clear on adding
I added your code to mythemename.theme but I don't get any body classes, do I need to still add something to the html.html.twig body classes?
Using Classy as my base theme so not sure if there is a difference.
Ideally I'd like to get the taxonomy term added as a body class too.
I was looking for a way to
I was looking for a way to include term as body class as well and found this solution:
Place this in MY_THEME.theme:
Then, place this in html.html.twig:
and clear the cache.
Now the output of body classes should be something like this on term pages:
-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien
Worked out for me. Thanks!
Worked out for me. Thanks!
This doesn't include any
This doesn't include any taxonomy terms into the class, as your output example would suggest.
You're code is using the current_path (URL path) the same thing as the examples above.
For the term, use page title
The term is used in the page title, so it's right there, even in the template to be used:
Preprocess html:
Or just do it in twig:
page['#title'] ? 'page-title--' ~ page['#title']|clean_class
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Worked for me!
This did the trick for me, thanks!
Thanks alot! Exactly what i
Thanks alot! Exactly what i've searched for.
Many thanks, works great!
Many thanks, works great!
Exactly what I needed
Exactly what I needed
No success with Bootstrap theme
I've attempted all of these fixes using Drupal 8.1.7 (just upgraded to 8.1.8 with no change) and Bootstrap with my own subtheme. The best I can get is the content type page-node-type-CONTENTYPE, but nothing has added the Node ID or the path. Are there are possible solutions out there?
Bob
The bootstrap theme renders
The bootstrap theme renders classes for < body > tags (content types and path, as well as various others).
Only node ID is not available. However, if you care to follow Jeff example (a few comments above): https://www.drupal.org/node/2634364#comment-10871250 You will get the node ID as a css class appended to < body > tag.
Add that function to {MY_THEME_NAME}.theme file.
I run Drupal 8.1.8 with Bootstrap custom sub theme and it works fine.
-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien
...
Hm, no luck. The body tag renders like this after this change:
<body class="path-node page-node-type-participate has-glyphicons">
You have to show us your code
You have to show us your code, you've made a mistake somewhere.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Code
MYTHEME.theme
html.html.twig
Did you change "themename" to
Did you change "themename" to your theme name?
"themename_preprocess_html" needs to be renamed to replace "themename" with your actual themes name.
Also you need to clear the Drupal cache.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Oy
Oy...thank you.
For completeness, you can do
For completeness, you can do this from e.g. the .module file too in a hook MODULENAME_preprocess_html
I try it but ....
I try all the options in the comments but no one works for me, somebody could help me..
and..
Now there is a module for
Now there is a module for that, its called Body node ID class
--------------
zoubi.me
Personally, I prefer the
Personally, I prefer the following.
I hope it can help.
Visit my blog for more info: Make Drupal Easy.
This seems to be the cleanest
This seems to be the cleanest, thanks. Providing a bit more specificity was useful in my case: