Hi:

I'm have a content type which is using both CCK fields and taxonomy terms. I'm trying to show both the fields and taxonomy terms in the node view. I tried to use the template to do this but I didn't see a way to do this readily. I ended up deleting the CCK template and creating a node template for the content type. I can list out the taxonomy terms this way but I don't see how to break them down into the respective vocabularies. I did dig around the drupal tables and saw the VID and terms listed in various tables but it seems like it be a major SQL ordeal to get the data that way. Appreciate any help or direction on doing this.

thanks!

Marty
marty.rynearson@gmail.com

Comments

mrynearson’s picture

well got no replies, it seems like something that might be common. Perhaps I should be more specific.

I have a cck form, half with custom fields and 5 or so taxonomy fields. Some are multiple choice and not all are required. What I want to do is show all the values in the node view, with some HTML for headers and labels.

Displaying the fields is straight forward but displaying the taxonomy terms are what I'm finding difficult. I'm not a php guru but i usually hack my way thru it.

So I found the following snippet online which lets me display all the terms:

      // Rather than simply print $terms, we want to control where
      // the taxonomy links lead; therefore, we'll compile the list
      // manually.
     $term_links = array();
    foreach ($node->taxonomy as $term) {
     $term_links[] = l($term->name, "$term->name");
    }

 	
      // "implode" makes the terms comma-separated
    print implode(', <br> ', $term_links);

So this displays all the terms with comma's but no way to seperate into the appropriate vocabulary. I did determine I can weight the Vocabulary so I know the basic order of the terms but since there are multiple choice hard to know where terms for one vocab begins and the other ends. If I had only one term per vocab then I could assume $term_links[0] is the term for the first vocab, $term_links[1] ids for the 2nd vocab etc.

My question did for any php/sql/drupal guru is what kind of query would yield the results I'm looking for . I see the vid and nid in various tables but I don't see an easy way to grab the terms for each vocab and present them.

Thanks for any help

Marty

kweisblatt’s picture

Did you find a solution to this? I am looking to do the same thing. I have cck fields address, city, state, zip, but I want these to be categorized, so I need to create categories. I don't want users to have to fill in info twice, so I will create categories and drop the cck fields. BUT, problem is, I want the new tax fields to show.....

~~~~~~~~~~~~~~~~
Kris
Current project: www.cribfax.com

jeff h’s picture

Hi Marty,

Sorry I haven't seen your post till now, but you may find http://drupal.org/node/53089#comment-111458 useful.

I modified the code from the above post slightly so that I could put it as a function in my template.php file:

/**
 * Return a list of taxonomy terms with each vocab one its own line.
*/
function node_terms_list($node) {

	$vocabularies = taxonomy_get_vocabularies();
	foreach($vocabularies as $vocabulary) {
		if ($vocabularies) {
			$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);
			if ($terms) {
				$output .= '<p class="terms"><strong>' . $vocabulary->name . ':</strong> ';
				foreach ($terms as $term) {
					$output .= l($term->name, 'taxonomy/term/'.$term->tid) . ', ';
				}
				$output = trim ($output,", ").'</p>';
			}
		}
	}

	return $output;
}

Don't include the first or last lines of the above when pasting into your template.php file.

I call this function from inside my node-content-cckname.tpl.php using print node_terms_list($node);

Jeff

eikes’s picture

thx!

juropel’s picture

Hi, I'm trying to achieve something similar to what is described in the original post in Drupal 6 using the Nitobe theme and a CCK custom content type. I want to display Taxonomy terms when viewing a node as if they are fields in the node.

I've pasted your function into the Nitobe template.php file, then called the function as you demonstrated from my custom node (CCK) template. However, the terms are not printing out.

Calling this Nitobe function does output the terms, however:

function nitobe_separate_terms($terms, $prefix = NULL, $separator = NULL) {
  $prefix    = ($prefix == NULL) ? t('Tags: ') : $prefix;
  $separator = ($separator == NULL) ? t(', ') : $separator;
  $output    = $prefix . preg_replace('!a></li>\s<li!',
                                      "a>{$separator}</li>\n<li", $terms);
  return $output;
}

But when I try to change the separator to cause a break between each term, I seem to break the code. (The browser won't display the page.)

Please advise. Thank you!

mpompili’s picture

Nice function to make it work on drupal 6.19 you have to change it to this one:

<?php
/**
 * Return a list of taxonomy terms with each vocab one its own line.
*/
function node_terms_list($node) {

    $vocabularies = taxonomy_get_vocabularies();
    foreach($vocabularies as $vocabulary) {
        if ($vocabularies) {
            //Send the $node and not the nid
            $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
            if ($terms) {
                $output .= '<p class="terms"><strong>' . $vocabulary->name . ':</strong> ';
                foreach ($terms as $term) {
                    $output .= l($term->name, 'taxonomy/term/'.$term->tid) . ', ';
                }
                $output = trim ($output,", ").'</p>';
            }
        }
    }

    return $output;
}
?>

The way to call the function is obviosly the same:

<?php
 print node_terms_list($node); 
?>
jaxxed@drupal.org’s picture

Maybe creating a cck vocabulary field is a better idea, instead of using the taxonomy association directly. Then you can template the fields easily.

I did a similar thing when playing with profiles pre-cck,pre nodprofile.module, and now I'm thining of writing a cck field for vocabularies. Advantages would be:
i ) being able to choose output better on a per node-type basis (multiple values, checkboxes/radios)
ii) field ordering and output templating.

I'm not that strong with cck but I am thinking of giving it a shot next week.

EDITED:

I just realized of course that this will remove the straight forward category association which can be quite handy (but maybe that's usefull too.)

James