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;
}

I have a view that contains images, I need to slightly alter the alt tag for each image by amending some text from the url onto the end. I seem to have pretty much figured it out yet when I check the view it has not altered anything.

Here is the code in my custom module, I have tried using a HOOK_views_post_execute and HOOK_views_pre_render, but still isn't changing the alt tags in the view

<?php
   
function image_alt_tag_alter_views_pre_render(&$view) {

      if (
$view->name == "we_print_for_menu") {
       
$path = arg(0) . "/" . arg(1);
       
$alias = drupal_get_path_alias($path);


    
// loops through index in array to change each alt tag
        // I have echo'd out from this loop and it is changing successfully
        // It just won't render in the view
         
for ($i = 0; $i < count($view->result); ++$i) {
               
$view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] = $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] . $alias;
              }
         
// not sure if I need this
         
return $view;
?>

I have printed out the results after this loop and everything has updated correctly. Yet it is not updating in the rendered view!!

I can't figure this out, $view->attachment_before and $view->attachment_after are both working within the hook.. In the hook instructions it just states that you need to edit $view->result, which I have done, within the function it is printing out with the correctly amended value, yet it isn't taking affect in the view.