I would like to display my search results as teaser list but can't find a way to do that at the moment.

There is a template file search-result.tpl.php (http://api.drupal.org/api/file/modules/search/search-result.tpl.php/6), but available variables do not include $node object or at least $nid, so I could load that node to display what I want.

Is there a way to do it?

Comments

gpk’s picture

Well http://api.drupal.org/api/function/node_search/6 certainly does return complete node objects - so in principle it's just a matter of hooking in somewhere between that and the search results template ...

gpk
----
www.alexoria.co.uk

gpk’s picture

Ahhh, maybe the point is that http://api.drupal.org/api/function/template_preprocess_search_result/6 doesn't give you a $node ... but is it present, in your template, in $result['node']?

gpk
----
www.alexoria.co.uk

point-1’s picture

Yes, it's present there, but I don't know how to override template_preprocess_search_result() because I don't want to override the core.

gpk’s picture

Can you just use $result['node']->teaser in your template? (You might need to a bit of processing on it to apply input formats etc.) Or use something like node_view($result['node'], TRUE, FALSE, FALSE) - see http://api.drupal.org/api/function/node_view/6.

gpk
----
www.alexoria.co.uk

point-1’s picture

Great, I can use $result['node'] in tpl.php. Thanks!

gpk’s picture

Alex.C.Freeman’s picture

Point,

Which tpl file are you referring to?

-Alex

gpk’s picture

http://api.drupal.org/api/file/modules/search/search-result.tpl.php/6.

You may want to copy it to your theme's directory and modify it there.

gpk
----
www.alexoria.co.uk

shevot’s picture

I used <?php print $result['node']->teaser ?> This has proven to be the most help for me thus far. Now at least I have the teasers showing up on the search page, but I am still having a few more problems if you can help please.

  • When using Firefox my search results look strange with the white background in the tables not showing up properly for example: http://www.ohavatar.com/search/node/girly but this error is only with most queries as you can see here: http://www.ohavatar.com/search/node/love not all searches have this problem.
  • Also as in the above example: http://www.ohavatar.com/search/node/love the top result is shown properly but subsequent results have the titles centered as apposed to left aligned.
  • Finally, when entering search keywords, some of the results do not display all of the available results: http://www.ohavatar.com/search/node/pink as you can see in this example only one result is displayed, but there should be at least 10 results for this keyword because when I do not display teasers there are at least that many.

    I am very much a novice at this and I have been trying to resolve this for a few months as you can see from my previous posts. From other user posts I can also see that this is very common inquiry for others like me who need the images from their teasers to show, art gallery and ecommerce sites especially. But with all of these advanced php coders and Drupal experts there does not seem to be anyone who knows how to do this. Any help would be truly appreciated.
    Thank you

  • gpk’s picture

    Well for starters you need to sort out your CSS. It looks like you are manually adding style.css to your page.tpl.php, but it is also output automatically by Drupal in compressed form. Hence all your CSS style rules are duplicated.

    So turn of CSS compression for now, and then you'll need to examine your page source to check which stylesheets are being multiply loaded. Only turn CSS compression on when everything is working.

    junro’s picture

    Hello, I can't make it work :(

    Do you just have to remplace in the search-result.tpl.php:

    <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>
    

    to

    print $result['node']->teaser

    I'm using the core serach module.

    Thanks :)

    mkinnan’s picture

    I am having a bit of trouble getting this to work. If I understand correctly, my search-result.tpl.php should look like:

    <?php print $result['node']->teaser ?>
    

    But what goes into function MYTHEME_preprocess_search_result(&$variables)?

    mkinnan’s picture

    ... is that modifying the search-result.tpl.php works for the core searches (/search/node). However, I am using Apache Solr and searches performed at (/search/apachesolr_search) are not affected by the changes in search-result.tpl.php with this method.

    twiik’s picture

    and I encountered the same problem when I copied the search-result.tpl.php to my theme directory and modified it there. However, changes made to the original file in the searce module directory works.

    So for now I just backed up the original search-result.tpl.php and have my modified version in the search module directory.

    EDIT: Ahh, I see what you meant now. $result['node']->teaser isn't available when using Solr. :/

    EDIT2:

    Here's how I fixed it when using Apache Solr.

    (search-result.tpl.php)

    <?php 
        $nid = $result['node']->getField('nid'); 
        $node = node_load($nid['value']);
        print node_view($node, $teaser = TRUE, $page = FALSE, $links = TRUE);
    ?>

    Øyvind Strømsvik
    Tech lead | Frontkom AS
    https://frontkom.com/

    mkinnan’s picture

    After a couple days of no success, I finally gave up. Thank you very much for finding the solution!!

    jlucard’s picture

    Almost perfect but.. in order to make it work I had to change "$nid = $result['node']->getField('nid');" to "$nid = $result['node']->nid;". I am using D6.16, MySQL 5.1.36 and Apache 2.2.11. Thanks

    Anonymous’s picture

    To summarize all of the above, for Drupal 6.x:

    - Use this for regular search:

        $nid = $result['node']->getField('nid');
        $node = node_load($nid['value']);
        print node_view($node, $teaser = TRUE, $page = FALSE, $links = TRUE);
    

    - Use this if using Apache SOLR search:

        $nid = $result['node']->getField('nid');
        $node = node_load($nid['value']);
        print node_view($node, $teaser = TRUE, $page = FALSE, $links = TRUE);
    

    Both go in search-result.tpl.php in your theme folder.

    mkinnan’s picture

    Are both snippets supposed to be the same? Just wanted to check.

    tomcatuk’s picture

    ....the same code posted twice for two different applications. I'm personally trying to display teasers as search results and finding it extremly challenging. I'm a bit surprised this isn't an option within core by default.

    Using Lucene search (although as I understand it, in terms of rendering results I'm essentially using core search). Drupal 6.

    KingJulian’s picture

    The first snippet should have been this:

    <?php
        print node_view($result['node'], $teaser = TRUE, $page = FALSE, $links = TRUE);
    ?>
    
    technikh’s picture

    This worked great. Thanks for the snippet.
    I want to highlight the searched terms in the teaser using search_excerpt (http://api.drupal.org/api/drupal/modules--search--search.module/function...)
    how can I get the searched terms in search-result.tpl.php?

    Cheers,
    TechNikh

    In 30 seconds set up Automated Visual testing of your website. Zero coding. https://drupal.org/project/drulenium
    Ever dreamed of styling your view, We have a solution for you. https://drupal.org/project/views_stylizer

    krisrobinson’s picture

    Worked like a charm. THANK YOU!

    krisrobinson’s picture

    I had an error after upgrading to core 6.22 and solr 6.1.5, here's the modified code I used to fix my error (Undefined class getField) :

    Working code:

        $nid = $result['node']->nid;
        $node = node_load($nid, NULL, TRUE);
        print node_view($node, $teaser = TRUE, $page = FALSE, $links = TRUE);
      
    rinux’s picture

    This is for Drupal 7.
    Apache SOLR search:

    Add to search-result.tpl.php that you have copied in your theme folder:

      $nid = $result['node']->nid;
      print render(node_view(node_load($nid),'teaser'));
    
    agogo’s picture

    ...this should be the way to handle search result display:
    http://drupal.org/node/1166114

    But it seems to be bugged. Atm.