I've followed the Drupal 7 documentation for hook_query_alter() in order to limit the number of total search results when searching for nodes or users to an arbitrary number.
However, with the following code (and the example in the documentation) this function has no effect:
function mymodule_query_alter(QueryAlterableInterface &$query)
{
$query->range(0, 3);
}
The code is getting executed, because adding a line using the dpm() function works and information is outputted, so the hook is getting executed. However, the $query->range(0, 3) method appears to have no effect.
What am I doing wrong?
EDIT:
I am using the custom_search module for Drupal 7.
I can change the number of results displayed on each page with the following code in hook_query_alter():
$results_per_page = 20;
if ($query instanceof PagerDefault
&& ($tables = $query->getTables())
&& isset($tables['i']['table'])
&& $tables['i']['table'] === 'search_index')
{
$query->limit($results_per_page);
}
But what I'd like to do is limit the total number of results before being paginated; if there are 234 results, I'd like to limit the number to, say, 100.
I am guessing that this is not working because I'm using the custom_search module.
Thanks.
dpm
in hook, and it fired. But he didn't write what it shown, so we don't know for what it fired... That was my point. By the way, unconditional limiting queries is... Bold move. I wouldn't dare. – Mołot Jun 19 '15 at 16:33