I have become in love with Display Suite. I am going to incorporate it into every site I build, from now on. It is amazing. Thanks for all your work on this.

Now, I have customized my Search Results, but I have this function in my template.php file, to group the results by content type:

function THEMENAME_preprocess_search_results(&$variables) {
  
    $variables['search_results'] = '';

    // get a list of node types
    $nodeTypes = node_get_types();

    // loop through results, group by type
    $resultTypes = array();
    foreach ($variables['results'] as $result)
    {
        $resultTypes[$result['node']->type][] = $result; 
    }
  

    // create fieldsets for each type
    foreach ($resultTypes as $resultType => $resultTypeResults)
    {
        $value = "";
        // loop through entries
        foreach ($resultTypeResults as $result)
        {
            $value .= theme('search_result', $result, $variables['type']);
        }
     
        // add fieldset
        $variables['search_results'] .= theme('fieldset',
            array(
                '#title' => $nodeTypes[$resultType]->name,
                '#collapsible' => TRUE,
                '#collapsed' => FALSE,
                '#value' => $value,
            )
        );
     
    }

    $variables['pager'] = theme('pager', NULL, 10, 0);
    // Provide alternate search results template.
    $variables['template_files'][] = 'search-results-'. $variables['type'];
}

It no longer works with ND Search. I imagine there must be a way to tweak this code to get it to work, since it just overrides the search results function, much like ND does. Any ideas? I would not abandon DS for this functionality, but it would be nice to have.

Thanks!

CommentFileSizeAuthor
#3 group-by-node-results.png72.52 KBswentel

Comments

swentel’s picture

Project: Node displays contributions » Display Suite
Version: 6.x-2.4 » 7.x-1.x-dev
Component: ND Search » Code
Category: support » feature

Look into nd_contrib/nd_search/nd_search.module. Depending on the search type (core, apachesolr ..) a function can be implemented to completely override the theming of the search results page. See node_search_page() or apachesolr_search_search_page() in that file. That means, any preprocess functions don't run anymore, that's the downside of it. So if you have custom modules in your project, you can actually do this yourself as well and even disable nd_search completely. Also, I think you should be able to take your preprocess function and do this:

//$value .= theme('search_result', $result, $variables['type']);
// Load the node
// Not sure if nid is here, it's been a while, but you get the idea right ?
// Maybe even the complete node is load here, so it might even be $node = $result['node'];
$node = $result['node'];
node_invoke_nodeapi($node, 'alter', TRUE, FALSE);
$value .= theme('node', $node, TRUE, FALSE);

I'm going to move this to Display Suite 7 version because I like the idea, so this will available in that version. This won't be backported normally since I stopped adding new features to the D6 branch right now, all focus is on D7. You think you have enough info with this ?

swentel’s picture

Title: How does search override exactly? » Group search results together by type

Better title for the new feature

swentel’s picture

Status: Active » Fixed
StatusFileSize
new72.52 KB

I've committed this to the D7 version of Display Suite. Again, I've looked for a possible backport, but that would break existing configurations of other people which I don't want too.

Attached is screenshot of how it works.

caponey’s picture

well, i tried and tried, but it was beyond me =)

i found out how to get search terms in views arguments though, so I can group them that way, and still use the Display Suite search results layout. Perfect.

thanks again for all you do swentel!

Status: Fixed » Closed (fixed)

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

puhig’s picture

Hi,

I would customized my Search Results group by node, my core searh is apache solr, Is possible get a similar view than post #3 ?

Thanks

waqarit’s picture

i have found a workable code to solve above issue without using DS.

<?php

function HOOK_preprocess_search_results(&$variables) {
  $variables['search_results'] = '';
  $resultTypes = array();
 
  // Divide results
  foreach ($variables['results'] as $result) {
    $resultTypes[$result['type']][] = $result;   
  }
 
  // Create fieldsets
  foreach ($resultTypes as $resultType => $resultTypeResults) {
    $value = '';
 
    foreach ($resultTypeResults as $result) {
      $value .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
    }
 
    $variables['search_results'] .= theme('fieldset', array('element' => array('#title' => $resultType, '#value' => $value)));
  }
}

?>

nareshbw’s picture

@waqariz - This code is ok . But here is one bug for it suppose total items are more then 10 so this field set will create for initial 10 items only . Other items will come on next page .

nareshbw’s picture

I used custom_apachesolr_query_alter($query) hook to overcome this problem .