6.x-3.x docs.php hook_views_pre_render(&$view)
7.x-3.x views.api.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.


object $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().


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


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.


vzblk’s picture

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.

retiredpro’s picture

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?

alvar0hurtad0’s picture

a lot of thanks for your comments, it was very helpfull for me.

imkartik’s picture

I was able to decrypt my data using hook. Just wanted to share if anyone needs it any time in future.

Miguel.Andrade’s picture

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)
prairiebean’s picture

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.

MantasK’s picture

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;
joeuk31’s picture

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

    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 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.

edwardchiapet’s picture

I, too, am having this issue.

I have assigned a new value to the view field and it's showing the updated value when I dpm, but it is still not reflecting in the view.

ashrafmhd’s picture

Create the markup using _field_data in the result array and copy it into the rendered view. That should work.

ranelpadon’s picture

if your code doesn't work when using hook_views_pre_render, try using hook_views_post_execute, just like in my case (this will not work when using the pre render hook):

function MY_MODULE_views_post_execute(&$view) {
  if ($view->name == 'job_distribution') {
    foreach ($view->result as $result) {
      // Replace all empty fields with the dash '-' character.
      if(empty($result->field_field_regular_job_count)) {
        $result->field_field_regular_job_count[0]['rendered']['#markup'] = '-';
phponwebsites’s picture

I have created a page with 2 pager. I have displayed both pager items in tab1 and tab2. The tab2 have been displayed by default. If I click pager of tab1, then it have displayed by default tab2.

So i want to change active class based on tab's pager. For that i want to pass custom argument with page number into url. If i will do this, then i can displayed correct tab.

ie, my url should be ?page=1&type=tab1, if i click pager of tab1.

navalogan’s picture

Hi, i have he same problem! did you find any solutions?

nikky171091’s picture

Hi #navalogan

Please read this thread!


rsingh27’s picture

I build a view of image , title , description displaying one after other (at a time 2 rows should be visible). If view count is less then 2, a placeholder image should render to occupy remaining one place.
Note : placeholder image should cover space of title,description,image of default view row

alex.designworks’s picture

Using unset($results[$key]->field_name) triggers warnings and notices.

Instead use
$view->result[$key]->field_name[0]['rendered']['#access'] = FALSE;