4

When you click on a search result, you navigate to that node or user. I would like to show the search results in a block when a user navigates to one of the search results, so the other results are always quickly available. Any idea if this is possible?

I was thinking... Make search result variable ($search_results) global, and show this in a php block. But maybe there are better ways to solve this issue?

5 Answers 5

0

Override search-result.tpl.php with below code

<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
  <h3 class="title"<?php print $title_attributes; ?>>
    <a href="<?php print $url . '?search_keyword=' . arg(2); ?>"><?php print $title; ?></a>
  </h3>
  <?php print render($title_suffix); ?>
  <div class="search-snippet-info">
    <?php if ($snippet): ?>
      <p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p>
    <?php endif; ?>
    <?php if ($info): ?>
      <p class="search-info"><?php print $info; ?></p>
    <?php endif; ?>
  </div>
</li>

Then create a view that displays search results using views module and add a contextual filter saying search term, which is returned using PHP Code.

You can read the value in views contextual filter using $_GET['search_keyword'] and return the same.

Once view block is built, enable it in the pages you want !

2
  • Yes, I see, the basic idea looks great. But how do you create a view that displays the search results in Drupal 7? Commented Oct 22, 2012 at 14:43
  • The simplest answer I know to that question is to use Search API + Search API Database instead of core search. Commented Apr 2, 2013 at 16:24
0

At this moment I don't know any contrib module that fit this use-case but I know a module that may interest you in some way.

The module is the Search API Page Block. Here goes a summary of the module description:

This module exposes Search API Pages as blocks and uses the node's title as keywords. This allows you to use your existing search results page to direct users to related content.

Currently, this module is only useful when placing search result blocks on node pages, since it currently only uses $node->title as the search keywords. Future development can lead to other uses, including using Taxonomy terms, Context module, or custom fields to set the keywords.

To achieve your goal I think you will need to implement a cutom module that stores the result of the user's search and uses the result list as the content of a block implemented by your custom module.

Maybe you can try another approach by using the views module with exposed filters then use the Views filters populate to merge them in a single text field and using a block to show the view result. This approach will let you easily store the keyword in a variable using the hook_form_alter in conjunction with the variable_set and so on.

Here goes a question about the Views Filter Populate that may be useful:

How to combine 2 exposed filters (String and Taxonomy field)

0

In addition to Anil's answer:

Use Page Manager (part of Chaos Tools) to collect the arguments from the URL and Panels to render the page with the search results visible.

0

Piggy backing on Anil's search result template with an alternative to the views search block, it's not too bad to implement a block displaying search results in a custom module:

function MYMODULE_block_info() {
    $blocks['search-results'] = array(
        'info' => t('Search Results'),
        'cache' => DRUPAL_NO_CACHE
    );

    return $blocks;
}

function MYMODULE_block_view($delta = '') {
    $block = array();

    switch ($delta) {
        case 'search-results':
            if (array_key_exists('search_keyword', $_GET)) {
                $element = PagerDefault::$maxElement;
                $block['subject'] = NULL; // search data renders with a header
                $block['content']['results'] = search_data(check_plain($_GET['search_keyword']), 'node');
                $block['content']['pager'] = array('#theme' => 'pager', '#element' => $element);
            }
            break;
    }

    return $block;
}

Granted the results didn't seem to know which pager element to render so I declared it explicitly.

0

search_api_page_block

The Search API Page module already exposes the search forms as block. This takes that functionality one step further. This module exposes Search API Pages as blocks and uses the node's title as keywords. This allows you to use your existing search results page to direct users to related content.

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