I'm using taxonomy term pages for a directory type application where I use the "taxonomy menu" module to list out links to taxonomy term pages that display teasers of content related to each term. That works just perfectly for my needs.

I'm having trouble with applying blocks to the term pages...
I would like to use blocks to apply theme/design elements to term pages and have them display based on their absolute parent term. I have 3 first level parent terms that split off into their own distinct multi-level branches as follows:

1stLevel
--2ndLevel
----3rdLevel
1stLevel
--2ndLevel
----3rdLevel
1stLevel
--2ndLevel
----3rdLevel

Since the term pages don't have the full path in the url, I can't use a wildcard and display for "Only the listed pages". The alternative is to use PHP and find the parent term but that seems a bit messy because if you are on the parent term, taxonomy_get_parents($tid); returns nothing and if you are in the 3rd level, the parent term is the 2nd level.

I've done a fair bit of "googling" over the past 3-4 days with nothing to show for it. I'm not choosey about how I solve it but maybe someone else can nundge me in the right direction.

Thanks,
Andrew

Comments

pixelsweatshop’s picture

forget the core block system and use http://drupal.org/project/context. You can use taxonomy as a reaction to display blocks that u need

awasson’s picture

Well, I put this on the backburner for a while and came back to it this morning with a fresh outlook.

NOTE: This isn't for regular content pages, this is only for the taxonomy list views where you have a list of terms under some hierarchy of terms and all of the content titles and teasers relating to that term are listed below. I am using the terms pages as a directory of sorts to list out and organize content.

My goal was to create some sort of code mechanism that would give me the absolute parent term for any leg on a taxonomy listing page . So even if the titles and teasers I'm looking at are five levels down from the main term, I want to be able to identify the first level parent term (in code) so that I can apply it to body classes or use it to display a block.

There is probably an easier method or a module somewhere but I couldn't find it so I coded the following. Hopefully it will help out someone else in the future.

    /**
     * Use taxonomy_get_parents_all($tid) 
     * to access all taxonomy object arrays in the leg
     * Find the 1st level parent object 
     * Then identify the 1st level parent term name. 
     */
    $tid = arg(2);
    $parents = taxonomy_get_parents_all($tid);
    $parent_tid = max(array_keys($parents));  

    $parent_term_name = $parents[$parent_tid]->name;

    // Now I can use a conditional to return true for a known term in a block 
    // or I can inject my own parent-taxonomy class into the body classes[] array  

awasson’s picture

One more update:
The following code will add a CSS class to the body classes collection so that all of the term list pages under a 1st level term can share common CSS attributes and behaviours.

function THE_THEME_preprocess_html(&$variables) {
    /**
     * Use taxonomy_get_parents_all($tid) to access taxonomy object
     * Find the 1st level parent object
     * Identify the 1st level parent term name
     * Inject it into the classes_array
     */
	$tid = arg(2);
	if($tid) {
		$parents = taxonomy_get_parents_all($tid);
		$parent_tid = max(array_keys($parents)); 
		$parent_term_name = $parents[$parent_tid]->name;
		$variables['classes_array'][] = "taxonomy-parent-".strtolower(drupal_clean_css_identifier($parent_term_name));
	}
}

}

emilium’s picture

Works for me (:

function theme_preprocess_html(&$variables) {
    if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
        $tid = arg(2);
        $parents = taxonomy_get_parents_all($tid);
        dpm($parents);
        array_shift($parents);
        foreach ($parents as $parent) {
            $variables['attributes_array']['class'][] = 'taxonomy-parent-'.$parent->tid;
        }
    }
}
jalapi’s picture

Hi guys,

Could any one shed some light on how I could print the parent term of a term that is assigned to a node to the body classes_array? I've been searching through forums all day and can't seem to find the solution. I have been able get the assigned term to print to the body class using the following code:

Added this to template.php in bootstrap:

function taxonomy_node_get_terms($node, $key = 'tid') {
    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);
        $myvid = 4;
		$query->condition("t.vid", $myvid);
        $result = $query->execute();
        $terms[$node->vid][$key] = array();
        foreach ($result as $term) {
            $terms[$node->vid][$key][$term->$key] = $term;
        }
    }
    return $terms[$node->vid][$key];
}

Added this to the html.vars in bootstrap:

function bootstrap_preprocess_html(&$variables) {
if(arg(0)=='node' && is_numeric(arg(1))) {
        $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));
            }
       }
   }
}

Any help would be truly appreciated!