July 2011

Body class fra Taxonomy term

http://drupal.org/node/1072806

nedenstående smides i template.php
NB! theme_preprocess_html udskiftes.

WC3 validering: 
PHP
Kode (XHTML, PHP, CSS etc.): 
<?php
/**
* Add a css class to the body based on the taxonomy values applied to a page.
*/
function taxonomy_node_get_terms($node, $key = 'tid') {
    if(
arg(0)=='node' && is_numeric(arg(1))) {
        static
$terms;
        if (!isset(
$terms[$node->vid][$key])) {
           
$query = db_select('taxonomy_index', 'r');
           
$t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
           
$v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
           
$query->fields( $t_alias );
           
$query->condition("r.nid", $node->nid);
           
$result = $query->execute();
           
$terms[$node->vid][$key] = array();
            foreach (
$result as $term) {
               
$terms[$node->vid][$key][$term->$key] = $term;
            }
        }
        return
$terms[$node->vid][$key];
    }
}

function
theme_preprocess_html(&$variables) {
   
$node = node_load(arg(1));
   
$results = taxonomy_node_get_terms($node);
    if(
is_array($results)) {
        foreach (
$results as $item) {
          
$variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
        }
    }
}
?>

Drupal 7

Drupal 7