I want to show the node title and related taxonomy terms in a table and modifying the search-result.tpl.php to do this.

What I use to for taxonomy terms is that:

<td class="field-item">
    $str = '';
    foreach($result['node']->field_my_taxonomy_term['und'] as $obj) 
        $str .= l($obj['taxonomy_term']->name, 'taxonomy/term/' . $obj['tid']) . ',&nbsp;';
    print trim($str, ',&nbsp;');

This code works in one of my Drupal website and doesn't work on another one and gives this error:

Notice: include() (.../search-result.tpl.php  line 20) Undefined index: und.
Warning: include() (.../search-result.tpl.php  line 20) Invalid argument supplied for foreach().

How can I show the taxonomy terms related to the node in search-result.tpl.php?


It's either that your field has a set language (not 'und' which stands as 'undefined') or that you have no value for field_my_taxonomy_term.

Either way you should add this control before your foreach (I also rewrite it to fit Drupal best practices) :

<td class="field-item">
  <?php $str = ''; ?>
  <?php if (isset($result['node']->field_my_taxonomy_term[LANGUAGE_NONE]) && $result['node']->field_my_taxonomy_term[LANGUAGE_NONE]): ?>
    <?php foreach ($result['node']->field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?>
      <?php $str .= l($obj['taxonomy_term']->name, 'taxonomy/term/' . $obj['tid']) . ',&nbsp;'; ?>
    <?php endforeach ?>
    <?php print trim($str, ',&nbsp;'); ?>
  <?php endif ?>
| improve this answer | |
  • Thanks a lot @pbonnefoi . It's working correctly now. I had tried the [LANGUAGE_NONE] instead of ['und'] but it didn't work. Interesting... And also, thanks for the (Drupal best practices) link. – herci Nov 18 '14 at 21:12
  • 1
    Cool i'm glad that helped you. – pbonnefoi Nov 19 '14 at 8:47

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.