function hook_views_pre_render

You are here

7 views.api.php hook_views_pre_render(&$view)
6 docs.php hook_views_pre_render(&$view)

This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.

Adding output to the view can be accomplished by placing text on $view->attachment_before and $view->attachment_after. Altering the content can be achieved by editing the items of $view->result.

This hook can be utilized by themes.

Parameters

$view: The view object about to be processed.

Related topics

1 function implements hook_views_pre_render()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

views_test_views_pre_render in tests/views_test.module
Implements hook_views_pre_render().
1 invocation of hook_views_pre_render()
view::render in includes/view.inc
Render this view for a certain display.

File

./views.api.php, line 1012
Describe hooks provided by the Views module.

Code

function hook_views_pre_render(&$view) {
  // Scramble the order of the rows shown on this result page.
  // Note that this could be done earlier, but not later in the view execution
  // process.
  shuffle($view->result);
}

Comments

It doesn't change result if you use entity render plugin.
So if you want to change order or manipulate results for this plugin use hook_views_post_execute instead.

I'm able to manipulate the results with hook_views_post_execute. But if I have a pager enabled, I'm only able to manipulate the first page's results. I've tried some other hooks but I cant see to find where to get the results for me to sort before the pager is built. Any ideas?

You may wish to alter just one view...

You could check

$view->name; //checks for view machine name
$view->current_display; //checks machine name display

dpm($view,"complete view"); // to display all you can access (uses devel module for dpm function)

Maybe obvious to others but this function baffled me at first. If iterating through the view result, in order to actually reset the value of the result, you have to refer to it by the original array name. e.g. this won't work:

foreach($view->result as $r => $result) {
    $result[$r]->field = 1;
}

but this will:


foreach($view->result as $r => $result) {
    $view->result[$r]->field = 1;

Also, whatever the type of your field before, you're beholden to that. e.g. I tried to rewrite a TID field here to output markup, but bc TID is an integer, this made everything unhappy and had to leave it as a TID and template it out instead.

in your example $r is index of $view->result not of $result, so there is no way it could possibly work.

probably what you wanted to do was $result->field
then you have to do it this way:

foreach($view->result as $r => &$result) {
    $result->field = 1;
}