How to sort View results programmatically in Drupal?

Posted by Jessica Taylor | Updated on

In this answer, I have shared how to sort View results programmatically in Drupal? The views module provides some hooks for ‘external’ manipulations, just like Drupal core.

You can implement hook_views_pre_render(&$view) within a custom module and manipulate the result array available in $view->result:

Alternatively, you could process the view ‘manually’, by copying the behavior of the views_get_view_result() function, but instead of returning the result, you manipulate it and continue to render the view:

Reference:
https://stackoverflow.com/questions/2471361/how-to-sort-view-results-programmatically

Note: This is a lot of code duplication and thus error prone – I do not recommend this and would rather go with the hook implementation above, trying to find a way to get access to your '$important_var' from within that.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers