Question

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

Was it helpful?

Solution

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
}

OTHER TIPS

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

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top