1

I have successfully used hook_views_post_execute() to remove results I do not want from $views->result.

Now, however, I need to paginate the remaining results.

The code in my hook_views_post_execute() requires the full result set to work. If I edit the view in the admin interface and set it to 10 items per page with full pagination, the $views->result in hook_views_post_execute() will only include 10 items, so my code doesn't function properly.

It looks like I need to use hook_views_pre_render() to set up the pagination, but I'm not sure how.

Can someone point me in the right direction?

1
  • 1
    I would suggest using hook_views_query_alter() if possible to alter the $query to remove items. All features would then work as expected. Jul 6, 2017 at 19:19

1 Answer 1

1

I eventually realized that hook_views_query_alter() was the appropriate hook, but it wasn't working because this view utilizes Search API. Turns out hook_search_api_views_query_alter() is a hook specifically for altering the query when using Search API.

I used this hook to add a condition to the view query before it is executed. This allowed me to turn pagination back on in the admin panel and have it work as anticipated.

function mymodule_search_api_views_query_alter(view &$view, SearchApiViewsQuery &$query) {
  if ($view->name === 'my_view_name') {
    // Do some stuff to get the nids I'd like to allow in my result set.
    $nids = array(1, 2, 3, 4, 5);

    // Add the condition to the existing query.
    $view->query->condition('nid', $nids, 'IN');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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