6

How can I determine from within a module or theme if the current page is an Apache solr search results page?

  • 2
    Which way are you trying to identify it? From within code? – John Fiala Mar 2 '11 at 21:01
  • Are you referring to a Google search result (from an external site)? Or are you referring to a Drupal search result (from an internal search)? And what is the context (block, module, template, etc) ? – Jason Smith Mar 2 '11 at 21:09
6

If you're trying to determine from a module then you can check if the page is the search results page via the path arguments using the arg() function.

if (arg(0) == 'search') {
  TRUE
}
else {
  FALSE
}
  • This is what I have used before. It worked pretty well. – Randy Burgess Mar 3 '11 at 0:08
  • 1
    You might have to test for an arg(1) as well to ensure it is actually a 'results' page and not the search landing page – wiifm Mar 3 '11 at 7:02
  • this only works if you are using the standard paths. – Jeremy French Mar 3 '11 at 8:02
  • What Jeremy said, for starters... but you should be able to check for whatever paths you're using :-) Now, you might also want to check to see if there's a third arg (arg(2)) present, which means you're on a search result page... – geerlingguy Mar 3 '11 at 18:12
2

From a theming point of view you could simply use

<?php if ($search_results) : ?>
    <!-- Do this on search results pages -->
<?php endif; ?>

$search_results should only return true if you are on a search results page.

If you're looking into styling the results themselves you might want to look into search-result.tpl.php

0

If you are looking to find if solr has been searched you can use the apachesolr_has_searched() function.

This won't indicate if you are on a search results page, as once you start using solr views the definition of a search results page is fuzzy, but it can tell you if a solr search has been performed.

Just for reference apachesolr_static_response_cache() will give you solrs' response, and apachesolr_current_query() will give you information on the query.

0

To verify if a page is a search page, you should verify the first path element of the current URL is search, and the second path element is the name of a module that implements hook_search(). (I used a temporary variable, and added extra parentheses just to be make the code more readable.)

$bool = ((arg(0) == 'search') &&
  ($module == arg(1)) && module_invoke($module, 'search', 'name')
);
if ($bool) {
  // The page is a search page returned from apachesolr.module.
}

In the specific case, considering that you want to verify if a page is a result page returned from apachesolr.module, the code can be simplified as follows:

if ((arg(0) == 'search') && (arg(1) == 'apachesolr')) {
  // The page is a search page returned from apachesolr.module.
}

The reason the generic code can be simplified is that you know the name of the module, and that module surely implements hook_search().
The generic code verify the value returned by hook_search() when the last parameter is equal to "name" because there are modules that implement hook_search() only partially. At least that what I discovered when I was debugging the code of OpenSearch Feed.

-1

In case you are talking about a theme or module you could check the current URL/path if it begins with "search".

  • Funny how my answer is downvoted without an explaining comment, while other answers with the same solution are upvoted... – EricSchaefer Mar 3 '11 at 10:13
  • 4
    I didn't downvote, but it's probably because of the lack of a code sample or something more substantive :-/ – geerlingguy Mar 3 '11 at 18:13

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.