Download & Extend

I'd like to create a bit of documentation for NAT

Project:Node Auto Term [NAT]
Version:6.x-1.1-beta3
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Hi.

I've recently created a little function for themers using NAT wich can output terms by vocabulary in three different ways:

  1. Plain text
  2. Term link
  3. NAT link

Links are created with URL alias if they are defined. Since it's my first "serious" code and I haven't seen any php snippet with the same functionality I'd like to share it. Is there any kind of project for NAT documentation?

Kind regards.

PS.- Here is a little example of use:

In your theme you put:

    <p><strong>Plain text:</strong> <?php print phptemplate_vterms($node, 8, FALSE, FALSE);?></p>
    <p><strong>Term link:</strong> <?php print phptemplate_vterms($node, 8, TRUE, FALSE);?></p>
    <p><strong>NAT link:</strong> <?php print phptemplate_vterms($node, 8, TRUE, TRUE);?></p>

...and you get

<?php
   
<p><strong>Plain text:</strong> <span class="term">Half life 2</span>, <span class="term">Half life 2 - Episode one</span>, <span class="term">Half life 2 - Episode two</span>, <span class="term">Portal</span>, <span class="term">Team fortress 2</span></p>
    <
p><strong>Term link:</strong> <a href="/categoria/juego/half-life-2">Half life 2</a>, <a href="/categoria/juego/half-life-2-episode-one">Half life 2 - Episode one</a>, <a href="/categoria/juego/half-life-2-episode-two">Half life 2 - Episode two</a>, <a href="/categoria/juego/portal">Portal</a>, <a href="/categoria/juego/team-fortress-2">Team fortress 2</a></p>
    <
p><strong>NAT link:</strong> <a href="/juego/portal">Half life 2</a>, <a href="/juego/half-life-2">Half life 2 - Episode one</a>, <a href="/juego/half-life-2-episode-one">Half life 2 - Episode two</a>, <a href="/juego/half-life-2-episode-two">Portal</a>, <a href="/juego/team-fortress-2">Team fortress 2</a></p>
?>

Comments

#1

Hi please share! How did you do this? Thanks.

#2

I've changed a bit the function and now it allows 4 different modes of terms.

Put this function in your template.php file:

(my natural language is spanish so it's possible I left some spanish comments)

<?php
/**************************************************************************
TERM PRINTING BY VOCABULARY
$node        Node object wich we are going to use
$vid            Vocabulary identification number
$output_mode    'raw' = plain text
                'tag' = text+special class (to allow better formatting with css)
                'link' = link depending on the next parameter, $nat_mode
$nat_mode        FALSE = links lead to their corresponding term (taxonomy page)
                 TRUE = links lead to their node creator through NAT
**************************************************************************/
function phptemplate_vterms($node, $vid, $output_mode = 'raw', $nat_mode = TRUE) {
   
/*global $base_url;*/
    /* We pass to $terms vector ONLY those terms wich are from our selected vocabulary */
   
$tids = array();
    foreach (
$node->taxonomy as $term) {
        if (
$term->vid == $vid) {
           
$url = taxonomy_term_path ($term);
           
$terms[$term->tid] = array(
               
'title' => $term->name,
               
'url' => $url,
               
'attributes' => array(
                   
'title' => $term->name,
                   
'rel' => 'tag'
                   
),
               
'nat' => '');
        }
    }
/** Now we got a vector of vectors where every entry has the needed data to build a normal link leading to the taxonomy page or just a plain text output. We need to get the NID of the parent NAT node for each term (if they exist) and modify their URL to point to the parent node instead of to the term directly **/
   
if ($nat_mode==TRUE) {                                        /* We only get the parent NIDs if we are in NAT mode (server resource saving) */
       
if (is_array($terms)) {
           
$tids = array_keys($terms);
           
$nats = array_keys(nat_get_nids($tids));            /* This functions output [$node->id]=>$node->title, I only catch the keys */
       
}
       
$new_nats = array();
        foreach (
$tids as $key => $tid) {
           
$new_nats[$tid] = $nats[$key];                        /* This way I got a more convenient format like [$tid]=>nid */
       
}
        foreach (
$new_nats as $key => $new_nat) {                /* Insertion of extra information (parent NIDs) in the terms vector */
               
$terms[$key]['nat'] = $new_nat;
        }
    }
   
$output = array();
   
/* If we are NOT in link mode the plain text mode is built */
   
if ($output_mode=='raw' && is_array($terms)) {
        foreach (
$terms as $term) {                /* Fast method, we are not going to use the TIDs, we only need to put the variables in an array */
       
array_push($output, $term['title']);    /* ... */
       
}
    }
    elseif (
$output_mode=='tag' && is_array($terms)) {
        foreach (
$terms as $term) {                                                        /* Again the fast method but now with extra tags for css theming */
           
array_push($output, '<span class="term">' . $term['title'] . '</span>');    /* ... */
       
}
    }
   
/* If we are in link mode, we make the output as a link */
   
elseif ($output_mode=='link'  && is_array($terms)) {
        foreach (
$terms as $key => $term) {
           
/*if ($nat_mode==FALSE) {}*/                                /* ...if we are NOT in NAT mode, URLs are not modified */
           
if ($nat_mode==TRUE) {
               
$terms[$key]['url'] = 'node/' . $terms[$key]['nat'];
            }
        }
        foreach (
$terms as $key => $term) {
           
$terms[$key]['url'] = drupal_get_path_alias($term['url']);    /* Making the URL with alias (in case the exist) (I got to check it also works without URL alias defined) */
           
$link = l($terms[$key]['title'], $terms[$key]['url'], $terms[$key]['attributes']);
           
array_push($output, $link);
        }
    }
   
$output = implode(', ', $output);
    return
$output;
}
?>

And that's it, you just need to modify your theme and call to this funcion any time you need it. For example:

Link for terms of vocabulary 5 wich leads to the standard taxonomy page...

...
<tr>
    <th>Genre:</th>
    <td><?php print phptemplate_vterms($node, 5, 'link', FALSE);?></td>
</tr>
...

Link for term of vocabulary 3 that leads to the node wich created that terms through NAT...

...
<tr>
    <th>Company:</th>
    <td><?php print phptemplate_vterms($node, 3, 'link', TRUE);?></td>
</tr>
...

Regads.

PS.- I'm not a programmer so the code can be improved a lot, SURE.

#3

Thanks for the snippet. I was hoping I could figure this out from your code but, do you know how to return just the NAT term. Just the name without a link or anything?

Thanks.

#4

Yes, simply put in your template:

<?php
print phptemplate_vterms($node, 3, 'raw', FALSE);
?>

So, in short, you have to choose between:

  • Raw text: 'raw'
  • Text with extra markup: 'tag'
  • Link: 'link'
    • Normal link: FALSE
    • NAT link: TRUE

#5

#6

I don't know much about this issues, but I think the problem comes from the block.

The function uses the $node variable wich -I think- is not available from blocks directly. You should "load" first the node (maybe using global or maybe with the drupal function to do it) and then you could call the function.

To check it you could try to print directly the node variable in your block with

<?php
print_r
($node);
?>
. If it doesn't output anything, that's the problem.

UPDATE

I think there is an error in your code:

'content' => print phptemplate_vterms($node, 2, 'raw', FALSE)

it should be

'content' => phptemplate_vterms($node, 2, 'raw', FALSE)

without "print".

#7

#8

Status:active» fixed

#9

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nobody click here