I’m trying to take the results of a view – using the function views_get_view_result() – and sort the array in a way I couldn’t do from within the Views interface. So far so good. I’ve got a $rows variable with all of the stuff I need.
Now… How do I put it back? 🙂 Before I needed this sort, I used views_embed_view(), but I can’t do that anymore.
Grateful for any help on this, feels like I’m so close to cracking it!
$important_var = important_function();
$result = views_get_view_result($view, $display, $args);
$result = sorting_function($result, $important_var);
//TODO: Put the result back into the view
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:The hook is invoked in the middle of the view generation process, after all result data has been assembled, but before the actual output gets rendered, so changes to the result array will be reflected in the views final output.
EDIT: 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: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.