Skip to content

Instantly share code, notes, and snippets.

@tmbritton
Last active December 25, 2015 19:29
Show Gist options
  • Save tmbritton/7028325 to your computer and use it in GitHub Desktop.
Save tmbritton/7028325 to your computer and use it in GitHub Desktop.
Add classes to <body> tag based on metatag keywords.
/**
* Implements hook_preprocess_html().
* Add classes to <body> tag based on metatag keywords.
*
* Previous version used hook_node_view() to make keywords accessible to
* this function to avoid a node_load(). However, this hook is not available
* on mobile due to Panels so this node_load() is necessary to work for
* both themes.
*/
function hook_preprocess_html(&$vars) {
if ($vars['page']['#type'] == 'page') {
$node = node_load(arg(1));
$keywordstring = $node->metatags['keywords']['value'];
$tags = explode(',', $keywordstring);
foreach ($tags as $tag) {
$vars['classes_array'][] = drupal_html_class(trim($tag));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment