I am attempting to display template $variables through else if statement - which is dependent upon taxonomy terms associated with the node -- for example - in the current situation -- two terms are available (eg., orange and apple)

The pre-processor function for the applicable template contains the following code:

//get taxonomy node terms
$node_terms = taxonomy_node_get_terms($node->nid);
$variables['node_terms'] = $node_terms;

The template file would be as follows:

if ($node_terms[1]->name == "orange") {
//do this
} else ($node_terms[2]->name == "apple") {
//do this
}

However, as of date -- print $node_terms; -- located in the template file -- only manages to print the word "Array" without displaying the contents of the array to the template.

I realize this is simplistic in explanation -- and that I am missing crucial code or steps in the process -- Any help in how to make this logic work would be greatly appreciated. Thanks in advance.

Note: the following code exist in the applicable pre-processor function and correctly creates taxonomy links to the template.

// remove hidden taxonomies
module_name_nodeapi($node,'view');
$terms = taxonomy_link('taxonomy terms',$node);
$variables['terms'] = theme_links($terms);

Comments

mean0dspt’s picture

based on this http://11heavens.com/putting-some-order-in-your-terms
and some other snippet I can't remember the source right now...

I used such a code to apply the same logic:

$tid1 = 450; // vocabulary term ID for whivh to display the block
$tid2 = 451; // vocabulary term ID for whivh to display the block
$tid3 = 452; // vocabulary term ID for whivh to display the block
$tid4 = 453; // vocabulary term ID for whivh to display the block
$vid = 6; // Vocablulary ID (We ignore all but one vocabularies for speed)
if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $r = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
    while ($term = db_fetch_object($r)) {
        if ($term->tid == $tid1)
            print ' work';
        if ($term->tid == $tid2)
            print ' easy';
	if ($term->tid == $tid3)
            print ' night';
	if ($term->tid == $tid4)
            print ' other';
    }
}

It has no else clause because vocabulary has only 4 terms

it's not a real function, it works directly inside node.tpl
there might be a better way of doing this

auctionteamster’s picture

Thanks for pointing me in the right direction mean0dspt - this was my first attempt with working with taxonomy code.

After beating about with different core taxonomy functions - I couldnt find one that would simply meet my needs -- a lot of great taxonomy functions -- but not one that I discovered that would simply solve my issue.

Among that process, I discovered the - taxonomy_node_get_terms_by_vocabulary() -- never could get it to work -- perhaps I misunderstood how to provide the correct parameters - still not sure why it ask for three (3) parameters - I'm baffled with this function. Nonetheless, it would have solved my issue if I could have gotten it to work. Essentially -- the function makes a call to the taxonomy tables - in particularly the term_node and term_data tables -- and returns a vocabulary. So essentially I borrowed the sql code from the function, slightly modifed it, and placed it in the preprocess funtion as follows:

function template_preprocess_xxxxxxxx(&$variables){

// get tid value FROM term_data table where vid (vocabulary) = 56
// and assign tid value to the $terms variable
$r = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node}
r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), 56, $node->nid);
while ($term = db_fetch_object($r)){
$variables['terms'] = $term->tid;
}
}

Then modified the xxxxxxxxx-tpl.php (template file) as follows:

if (($orange) && (($terms) == 276)):

echo $orange; endif;

if (($banana) && (($terms) == 277)):

echo $banana; endif;

if (($apple) && (($terms) == 278)):

echo $apple; endif;
mean0dspt’s picture

I'm glad it helped

jasenward’s picture

I have a similar wish list item and I believe that I have a solution for this that may be a bit more flexible. I was recently asked to dynamically turn off the display of the "title" block on some pages, but unlike all of the solutions I found on this site I didn't know anything about the nodes that were to have the title hidden. So, after several hours of searching and reading through the API documentation, I came up with the following solution. It's probably a bit low tech, but it works and hopefully this will help someone else to improve upon it.

Goal:
To be able to turn of the display of the Title on a page knowing only that the node in question has been tagged with a vocabulary term.

Requirements:
Taxonomy Module, Zen Template

Add The following Vocabulary using Taxonomy:
Vocabulary: Title Display with Terms: "Title Visible" and "Title Hidden"

Setup:
Copy the page.tpl.php, template.php from the Zen template to your subtemplate as per instructions on building a custom Zen template.

Files to Modify:
page.tpl.php
1. Find line 195 ( the one that reads ... if($title): ... immediately below the call to print the $breadcrumb variable )
2. Change the PHP logic to add a check of a new variable like so: ... if($title && $myTitleShowVariable ): ...

template.php
1. Find line 103 (the one the reads ... function XXXX_preprocess(&$vars, $hook){ )
2. Make sure that the line above this which comments out the function call is removed to allow this function to be called
3. Replace the sample code $vars['sample_variable']=t('Lorem ipsum'); with the following block of code

if(arg(0)=='node' && is_numeric(arg(1)) && !arg(2)){ 
$node=node_load(arg(1)); 
$vars['idgshow_title']=FALSE; 
foreach ($node->taxonomy as $vid=> $term){
$vn=taxonomy_vocabulary_load($term->vid);
if($term->name=='Title Visible'){
$vars['idgshow_title']=TRUE;
}
}
}

The same logic could apply to other variables of the template, but this makes it possible to simple tag the nodes with a term that can be used to control the display of the content. I installed Views and the Devel modules as well, but none of these quite did what I wanted. Hopefully someone will improve upon this.