I have a node type view. I would like to add a taxonomy field, where it gets only the parent terms to the specific node.

I have:

Church 1
	John Doe
	Uncle Sam
Church 2
	Jane Doe
	Jane Smith
Church 3
	Goerge Washington
	John Doe
Church 4
	Mary Smith

Currently views "shows all" terms specific to that node, i.e. Church 1, John Doe, Church 3, Goeorge Washington. I need it to "show only parents" Church 1 and Church 3, linked to its respective term page.

If you can provide the snippet either, using views 6.2 natively, or a custom PHP code via Views Custom Field module?

I've been struggling for two days, using queries, and applying methods found in the following links?
http://drupal.org/node/1011946
http://drupal.org/node/216888
http://drupal.org/node/43290
http://drupal.org/node/866586
http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/functi...

If you can please post the code snippets here, in hopes it helps others find same or similar solutions?

This is my first time using this service, so I really don't know what to pay, say 50.00; I hope thats fine. I can send Paypal.

Comments

Sagar Ramgade’s picture

Hi,

Please enable your contact form, so people can contact you.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

cj-a-min’s picture

Ok I set the contact form. Thanks

I was able to use dbforge query builder (very awesome tool) to create the sql, and provide me with the following array:

  Array
  (
  [tid] => 1
  [name] => Church1
  )
  
  Array
  (
  [tid] => 7
  [name] => Church3
  )

I turned this array into this:

Church1 
Church3

with the following snippets:

$result = db_query('
SELECT term_node.tid
     , term_data.name
FROM
  term_node
LEFT OUTER JOIN node
ON term_node.vid = node.vid
LEFT OUTER JOIN term_hierarchy
ON term_node.tid = term_hierarchy.tid
LEFT OUTER JOIN term_data
ON term_node.tid = term_data.tid
WHERE
  term_node.nid = %s
  AND term_hierarchy.parent = 0
', $node->nid);

while ($row = db_fetch_array($result)) {
  echo $row['name'] . '<br>';
  //dprint_r($row);
}

This is where I am at now, on how to link the terms to their term page. My first guess is tu use links using l(...).

Also, I know the sql needs cleaning up, like do I even need to 'left join' the {node} table since the nid is already in the {term_node} table?

Anyways I'll still be working on this, would be nice to put it into a function in a custom module, then calling it from anywhere by only passing it the $nid?

:)

Sagar Ramgade’s picture

Hi,

Here's the code to output them as a link:

<?php
  $result = db_query('
SELECT term_node.tid
     , term_data.name
FROM
  term_node
LEFT OUTER JOIN term_hierarchy
ON term_node.tid = term_hierarchy.tid
LEFT OUTER JOIN term_data
ON term_node.tid = term_data.tid
WHERE
  term_node.nid = %d
  AND term_hierarchy.parent = %d
', $node->nid, 0);

while ($row = db_fetch_array($result)) {
 echo l($row['name'], 'taxonomy/term/' . $row['tid']) . "<br />";
}
?>

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

cj-a-min’s picture

Thanks very much the feedback.

After personally trying I came up with this, which is similar to yours.

$result = db_query('
SELECT term_node.tid
     , term_data.name
FROM
  term_node
LEFT OUTER JOIN node
ON term_node.vid = node.vid
LEFT OUTER JOIN term_hierarchy
ON term_node.tid = term_hierarchy.tid
LEFT OUTER JOIN term_data
ON term_node.tid = term_data.tid
WHERE
  term_node.nid = %s
  AND term_hierarchy.parent = 0
', $node->nid);

while ($row = db_fetch_array($result)) {

 // echo $row['name'] . '<br>';
  $items[]= l($row['name'], "taxonomy/term/" . $row['tid']);
 
} 
print theme('item_list',$items);

The other big question is, do I need to 'left join' the {node} table? Thanks

Sagar Ramgade’s picture

Hi,

No left join for node table is not required at all as we are getting nid from term node table.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

cj-a-min’s picture

Great Job - Great Teamwork - I'll contact you right now

cj-a-min’s picture

Here is the snippet in module form, so you can use it anywhere.

Put this into your custom module:

/**
 * Display all parent terms specific to a node.
 *
 * @param $vid
 *   The vocabulary ID to generate the list of parent terms for.
 *
 * @param $nid
 *   The node ID is used to specify the node you wish to retrieve
 *   the parent terms for.
 */
function mycustommodule_get_all_parent_terms_specific_to_node($nid, $vid){
  $result = db_query('
SELECT tn.tid
     , td.name
FROM
  {term_node} tn
LEFT OUTER JOIN {term_hierarchy} th
ON tn.tid = th.tid
LEFT OUTER JOIN {term_data} td
ON tn.tid = td.tid
WHERE
  tn.nid = %d
  AND td.vid = %d
  AND th.parent = %d
', $nid, $vid, 0);
  while ($row = db_fetch_array($result)) {
    $items[]= l($row['name'], "taxonomy/term/" . $row['tid']);
  }
  print theme('item_list',$items);
}

Then call it from where every you want like node.tpl.php, views, etc..


echo mycustommodule_get_all_parent_terms_specific_to_node($node->nid, 1);

Where $node->nid is your nid, and 1 is the vocabulary id you want. If your using views custom field module than substitute $node->nid, with $data->nid

Well I hope that helps people with similar issue, thanks to Sagar for his contribution!

CJ