I'm using hook_preprocess_page to add body classes to nodes. But I cannot find a way to add body classes to user pages. I looked at hook_preprocess_user_profile, but how to assign the body class?
Thanks!
I'm using hook_preprocess_page to add body classes to nodes. But I cannot find a way to add body classes to user pages. I looked at hook_preprocess_user_profile, but how to assign the body class?
Thanks!
Hi, I would solve this problem by installing the Term Body Class module and then editing the user profile by adding a taxonomy type field with a dictionary setting to be used as a Term Body Class.
"Term Body Class allows to add taxonomy term classes to the body element of content pages and user account pages."
I've done this for different content types and it works great, I haven't done it for user profiles, but the module description says it's possible.
@Wylbur
You can use hook_preprocess_page()
for this and it will add to the <body>
element.
Add the following:
$variables['classes'][] = 'wylbur';
You will have access to the user object if that helps, though you don't get quite the same variables you get in hook_preprocess_user_profile()
, though perhaps you just want a static class.
I moved this to "How-To and Troubleshooting" as it seems a better fit there.
To be effective, you want to customize body classes based on the page type. Adding a variable class adds it to ALL pages. To make this effective, you need to be able to add a conditional statement to assign classes:
function themename_preprocess_page(&$variables) {
$node = menu_get_object();
// Add node classes to each page.
if ($node) {
$variables['classes'][] = 'node-' . $node->nid;
$variables['classes'][] = 'node-' . $node->type;
}
}
The snippet above adds classes for node ID, and node type. This works great for node pages , but user pages are not nodes, so nothing is added when visiting a user page.
Instead of loading the node object at the start of the function, we can also load the user object. '$node = menu_get_object();' assumes we are loading a node, but we can also get it to load the user object, which gives us the user information for the profile page: '$user= menu_get_object('user');'
Now we can put this all together to load different body classes based on the pages we are visiting:
<?php
/**
* @file
* themename preprocess functions and theme function overrides.
*/
function themename_preprocess_page(&$variables) {
$node = menu_get_object();
$user= menu_get_object('user');
// Add node classes to each page.
if ($node) {
$variables['classes'][] = 'node-' . $node->nid;
$variables['classes'][] = 'node-' . $node->type;
}
// Add user classes to each page.
if ($user) {
$variables['classes'][] = 'user-' . $node->uid;
$variables['classes'][] = 'user-node';
}
}
Backdrop core also provides other objects: contact, filter_format, menu, menu_link, node, taxonomy_vocabulary, user. Another object that is not on the list is taxonomy_term.
Hope this helps others!
To be effective, you want to customize body classes based on the page type. Adding a variable class adds it to ALL pages. To make this effective, you need to be able to add a conditional statement to assign classes:
The snippet above adds classes for node ID, and node type. This works great for node pages , but user pages are not nodes, so nothing is added when visiting a user page.
Instead of loading the node object at the start of the function, we can also load the user object. '$node = menu_get_object();' assumes we are loading a node, but we can also get it to load the user object, which gives us the user information for the profile page: '$user= menu_get_object('user');'
Now we can put this all together to load different body classes based on the pages we are visiting:
Backdrop core also provides other objects: contact, filter_format, menu, menu_link, node, taxonomy_vocabulary, user. Another object that is not on the list is taxonomy_term.
Hope this helps others!