2

First off, let me start by saying I am fairly new to Drupal, I come from a heavy Wordpress / Joomla background. On to my question:

I have looked for hours for a way to print a list of the taxonomy terms attached to a search result item. I need it to be as automated as possible (i.e. I need get the nid & vid from the node automatically if possible). I was able to get the nid using the following:

$nodeID = $variables['result']['node']->nid;

I do not need any fancy formatting, or hierarchy, I just need a comma separated list of the attached terms for each item on the search results page. If possible, I would also like to limit the depth to 5, but that is not a primary concern.

Here is what I have so far:

// Add List of Terms to Search Result Items
    $vid = 4;
    $terms = array();
      if (isset($node->taxonomy)) {
        foreach ($node->taxonomy as $voc) {
          if ($voc->vid == $vid) {
            $terms[] = $voc;
          }
        }
      } else {
        // check db in case node doesn't have terms
        $terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);    
      }
      return $terms;

    $terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
    // To store the new terms (if necessary...)
    if ($terms) {
        foreach ($terms as $term) {
            // Each term has the properties:
            // $term->tid
            // $term->vid
            // $term->name
            // $term->description
            // $term->weight
            // To just print the results use something like: 
            print $term->name . ',';
        }
    }

Any help you guys can provide would be immensely appreciated!

1

You almost have it. How I'd do it is like this. First, in (I assume you are doing this already) hook_preprocess_search_results:

function YOURMODULE_preprocess_search_results(&$variables) {

  // the vid you are restricting your results to...
  // it is really up to you to define the logic of how
  // to determine this, but you could use a variable...
  $vid=variable_get('YOURMODULE_search_result_vid', 4); 

  $taxonomy=$variables['result']['node']->taxonomy;

  if ($taxonomy) {

    $terms=array();
    foreach($taxonomy as $t) {
      if ($t->vid == $vid) {
        $terms[]=$t->name;
      }
    }

    if ($terms) {
      $variables['terms']=implode(',', $terms);
    }

  }

}

will, assuming no typos and brain fades on my part, produce a $terms variable you can access in search-result.tpl.php.

Now, for step 2, copy the default one out of .../modules/search to your theme. The default D6 one basically looks like this, with the large intro comments removed:

<dt class="title">
  <a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
  <?php if ($snippet) : ?>
    <p class="search-snippet"><?php print $snippet; ?></p>
  <?php endif; ?>
  <?php if ($info) : ?>
  <p class="search-info"><?php print $info; ?></p>
  <?php endif; ?>
</dd>

so in there, you can change it up basically like this:

<dt class="title">
  <a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
  <?php if ($snippet) : ?>
    <p class="search-snippet"><?php print $snippet; ?></p>
  <?php endif; ?>
  <?php if ($info) : ?>
  <p class="search-info"><?php print $info; ?></p>
  <?php endif; ?>
  <?php // THE NEW STUFF FOLLOWS... ?>
  <?php if ($terms): ?>
    <p class="search-terms"><?php print $terms; ?></p>
  <?php endif; ?>
  <?php // ...END OF THE NEW STUFF ?>
</dd>

and with some CSS styling of your new search-terms class, be on your way.

  • Thank you much for taking time to reply. Question though, when I use the code you have above, var_dump($terms); returns NULL. I have looked for a typos, and I am not seeing one so far. I also tried manually setting the vid to 4 instead of using the variable, same thing :( – Frejyasdaeg Mar 28 '13 at 18:25
  • Hmmm. I've cut and pasted the code right off this page and it works here on a D6 box. I'd throw in a drupal_set_message('<pre>' . print_r($taxonomy, TRUE) . '</pre>'); in the preprocess_search_results() hook to make sure it's grabbing what it should. – Jimajamma Mar 29 '13 at 12:25
  • Weird, are you putting the preprocess function in template.php or in the search module? All I get from $variables['result']['node']->taxonomy; is a NULL value. I have tried placing the function in both template.php and in the custom search module. – Frejyasdaeg Apr 3 '13 at 19:30
  • I put it in a custom module. Let me dig around and see if I have done anything further upstream that might be [ae]ffecting this... – Jimajamma Apr 3 '13 at 22:02
  • I don't see anything here that immediately jumps out. If you print out $variables['result']['node'] do taxonomy info/terms exist anywhere? – Jimajamma Apr 3 '13 at 22:28
0

If I'm understanding the requirement in the question, then another approach that might be worth exploring is to use Display Suite. The module can take over the display of search results. To replicate Drupal's default search you configure the Search Results view mode with title and snippet. You can then add additional fields, such as a term reference field.

If you have a lot of content types, the configuration process is a bit tedious as you find yourself configuring many near identical view modes. On the other hand, if you have a lot of content types, you are almost certainly going to gain, because some of those content types will benefit from having customised search results, i.e. for content types with a key image, you can display a thumbnail of the image in the search result. And, as per question, you can display taxonomy terms. Because it's part of Display Suite, you can also do some clever formatting without using templates, e.g. have a two columns search result or create code fields to combine two fields on your node into one etc.

See also documentation for configuring search display using Display suite on drupal.org

  • Hi, sorry for the delay, your suggestion would work great, but unfortunately there are other parts of the search results that need to be customized, and my research into Display Suite didn't look like Display Suite would be capable of handling it. The only real solution at this point is to build my own custom search module :( – Frejyasdaeg Apr 3 '13 at 19:28

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.