Skip to content

Instantly share code, notes, and snippets.

@carwin
Created December 19, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carwin/4337491 to your computer and use it in GitHub Desktop.
Save carwin/4337491 to your computer and use it in GitHub Desktop.
Create a body class for a specific node type based on the value of a field on the node.
<?
function themename_preprocess_html(&$variables) {
// Add a body class to Articles based on the value of the 'field_article_type'
// field. e.g.: 'article-type-news' or 'article-type-blog'.
if(!empty($variables['page']['content']) && !empty($variables['page']['content']['system_main']['content']['nodes'])) {
$node = $variables['page']['content']['system_main']['content']['nodes'][arg(1)]['body']['#object'];
if(user_is_logged_in()) {
$node = $variables['page']['content']['system_main']['content']['nodes'][arg(1)]['#node'];
}
if($node->type == 'article' && !empty($node->field_article_type)) {
$type = field_get_items('node', $node, 'field_article_type');
$type_value = field_view_value('node', $node, 'field_article_type', $type[0]);
$variables['classes_array'][] = 'article-type-' . strtolower(render($type_value));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment