1

I want to re-order the results of a View using some arbitrary PHP ( the sort is not possible in SQL ). I believe I could use either _views_pre_render(&$view) or hook_views_post_execute(&$view) to alter the order of the results. However the $view->result array only contains the number of results that the pager is set to, not the whole result set.

Is there some other method I can hook into the result generation process and alter the order of results using PHP ?

  • I could have sworn there was a module out there for this but a quick search did not turn up anything. Maybe you'll have better luck. – sareed Feb 10 '16 at 14:22
3

Views will always only return the results for the current page of results.

That is a benefit of paging, since you aren't retrieving all the data if users may only want to see some results.

If you want to sort after the results come from the database then you have to turn paging off and return all results. Then you can sort all the results and display them as you like.

[EDIT]

You mention in your comment potentially using hook_views_query_alter() to alter the paging out of the query but (without having actually tried) I don't think that would work without removing the paging all together, because PagerDefault::execute() does its thing to the SQL after you are altering.

If you want to pursue that idea though, what might potentially work though is you doing Query alteration (tagging) by adding an alter tag in hook_views_query_alter() and then implementing hook_query_TAG_alter(), which would run on the preExecute(), which should be after the pager has acted.

For example:

/**
 * Implements hook_views_query_alter().
 */
function MODULE_NAME_views_query_alter(&$view, &$query) {
  if ($view->name == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME') {
    // Add a query alteration tag so that we can alter the completed query before execution.
    $query->addTag('CUSTOM_TAG_NAME');
  }
}

/**
 * Implements hook_query_TAG_alter().
 *
 * Alter queries with the CUSTOM_TAG_NAME tag.
 */
function MODULE_NAME_query_CUSTOM_TAG_NAME_alter(QueryAlterableInterface $query) {
  // Alter the $query object here.
}
  • Thanks Rooby. Can I then still get the pager functionality somehow ? Or I was thinking of using hook_views_query_alter() to remove the limit that the pager places on the query, then sorting the results in hook_views_pre_render(), and finally deleting all the results that are outside the scope of the current pager page. Thoughts appreciated. – elb Feb 10 '16 at 17:20
  • Interesting thought re query alter. You could try but I don't think it will work because the PagerDefault::execute() function adds the range to the query right before execution, which will be after you alter the query. I will add some untested thoughts to my answer. – rooby Feb 10 '16 at 22:12

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.