0

After a painful day and a half I get this code, that prints out in my node.tpl.php the taxonomy terms associated to that particular node that the user is viewing.

It works just fine. As it is quite long I´ll just put the whole thing inside a module and into a variable, to just print that.

The thing is that it should get an easier, better way to do this! Any ideas?

<?php 
$vid = 11; //vocabulary id
$nid = $node->nid; //it looks for the current loaded node
$query = "SELECT tid, name
FROM (
SELECT td.tid AS tid, name
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
  ON td.tid = tn.tid
JOIN node AS n
  ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
  AND n.status = 1
  AND n.nid = ".$nid."
GROUP BY td.tid
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
      echo l($term->name, "taxonomy/term/$term->tid") . ', ';
}
 ?>

UPDATE:

This is devel output: (I´ve used (dpm($node);) because it let me print the result here without losing the indentation)

(object) array(
  'vid' => '5178',
  'uid' => '1',
  'title' => 'PROYECTO',
  'log' => '',
  'status' => '1',
  'comment' => '2',
  'promote' => '0',
  'sticky' => '0',
  'nid' => '155',
  'type' => 'jornadas',
  'language' => 'und',
  'created' => '1095048000',
  'changed' => '1360589684',
  'tnid' => '0',
  'translate' => '0',
  'revision_timestamp' => '1360589684',
  'revision_uid' => '1',
  'taxonomy_vocabulary_4' => array(),
  'taxonomy_vocabulary_6' => array(
    'und' => array(
      array(
        'tid' => '33',
        'taxonomy_term' => (object) array(
            'tid' => '33',
            'vid' => '6',
            'name' => 'Jornadas Gratuitas',
            'description' => '',
            'format' => NULL,
            'weight' => '0',
            'vocabulary_machine_name' => 'vocabulary_6',
          ),
      ),
    ),
  ),
  'taxonomy_vocabulary_7' => array(
    'und' => array(
      array(
        'tid' => '40',
        'taxonomy_term' => (object) array(
            'tid' => '40',
            'vid' => '7',
            'name' => 'Para PH',
            'description' => '',
            'format' => NULL,
            'weight' => '-10',
            'vocabulary_machine_name' => 'vocabulary_7',
          ),
      ),
    ),
  ),
  'taxonomy_vocabulary_11' => array(
    'und' => array(
      array(
        'tid' => '262',
        'taxonomy_term' => (object) array(
            'tid' => '262',
            'vid' => '11',
            'name' => 'colegios',
            'description' => '',
            'format' => NULL,
            'weight' => '0',
            'vocabulary_machine_name' => 'vocabulary_11',
          ),
      ),
      array(
        'tid' => '543',
        'taxonomy_term' => (object) array(
            'tid' => '543',
            'vid' => '11',
            'name' => 'derecho',
            'description' => '',
            'format' => NULL,
            'weight' => '0',
            'vocabulary_machine_name' => 'vocabulary_11',
          ),
      ),
    ),
  ),
  'body' => array(
    'und' => array(
      array(
        'value' => "
0

Few things here:

  1. Your SQL is open to injection
  2. You're running a query that has already been run, and for which data has already been returned by the core field system. Drupal already runs way too many queries in a page load, adding another unnecessary one should be avoided wherever possible.
  3. Your code should be in a template preprocess function (or module file as you've mentioned). But that's not because of the length of the code. It's because you should maintain a good separation of logic and display.
  4. Drupal has already prepared the display of the field for you, so it would be preferable to use that instead of building up from scratch again.

With those in mind, try simply adding:

<?php echo render($content['field_name_of_term_ref_field']); ?>

in place of your code.

To control how those field items come out (e.g. as links), navigate to admin/structure/types/manage/TYPE/display, and choose appropriate settings for the display widget.

11
  • 1
    Are you sure it's set to display on the "Manage Display" tab of your content type?
    – Clive
    Feb 12 '13 at 16:59
  • Sorry, By mistake I´ve deleted the comment! I´ll check that out now.
    – Rosamunda
    Feb 12 '13 at 17:01
  • Yes, they all should display, I´ve just checked the "Manage display" tab. And no, sadly that code doesn´t print out anything. This was the link being referenced by me in my accidentally deleted comment: stackoverflow.com/questions/14835303/…
    – Rosamunda
    Feb 12 '13 at 17:02
  • 1
    Ahhh ok. I remember seeing questions of yours before using D6 so I did wonder :) I can't think why the field isn't showing, but if you want to force it you can try print render(field_view_field('node', $node, 'taxonomy_vocabulary_11'));...
    – Clive
    Feb 12 '13 at 17:46
  • 1
    On the Devel tab for the node there's a sub-tab called 'Render'...you should be able to see whats being passed through in there. If you can dump that output to pastebin or something I can take a look
    – Clive
    Feb 12 '13 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.