8.3.x views.api.php hook_views_pre_render(ViewExecutable $view)
8.0.x views.api.php hook_views_pre_render(ViewExecutable $view)
8.1.x views.api.php hook_views_pre_render(ViewExecutable $view)
8.2.x views.api.php hook_views_pre_render(ViewExecutable $view)
8.4.x views.api.php hook_views_pre_render(ViewExecutable $view)

Act on the view immediately before rendering it.

At this point the query has been executed, and the preRender() phase has already happened for handlers, so all data should be available. This hook can be used by themes.

Output can be added to the view by setting $view->attachment_before and $view->attachment_after.

Parameters

\Drupal\views\ViewExecutable $view: The view object about to be processed.

See also

\Drupal\views\ViewExecutable

Related topics

4 functions implement 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.

test_basetheme_views_pre_render in core/modules/system/tests/themes/test_basetheme/test_basetheme.theme
Implements hook_views_pre_render().
test_subtheme_views_pre_render in core/modules/system/tests/themes/test_subtheme/test_subtheme.theme
Implements hook_views_pre_render().
views_test_data_views_pre_render in core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc
Implements hook_views_pre_render().
views_views_pre_render in core/modules/views/views.module
Implements hook_views_pre_render().
1 invocation of hook_views_pre_render()
ViewExecutable::render in core/modules/views/src/ViewExecutable.php
Renders this view for a certain display.

File

core/modules/views/views.api.php, line 817
Describes hooks and plugins provided by the Views module.

Code

function hook_views_pre_render(ViewExecutable $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

ivan.lewas’s picture

To change the image style of a view programatically, something like this should be enough:

use Drupal\responsive_image\Entity\ResponsiveImageStyle;

function MODULENAME_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if ($view->id() == "view_id" && $view->current_display == 'display_id') {
    $image_style_id = 'my_responsive_image_style';

    $style = ResponsiveImageStyle::load($image_style_id);
    if (!empty($style)) {
      $image_field = $view->field['field_news_image'];
      $image_field->options['type'] = 'responsive_image';
      $image_field->options['settings']['responsive_image_style'] = $image_style_id;
    }
  }
}

This code can be used to modify any other field (or setting) in a view display.

waqas93’s picture

Got null when
kint($view->field['field_name']);
So how can we change image styles if result is null ?

ressa’s picture

Insert this at the very start of your module:

print "view_id: " . $view->id();
print " - view_current_display: " . $view->current_display;
Andrew211’s picture

Simply manipulates the value of the title to "test" + the current timestamp.

function MODULENAME_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if($view->id() == 'products_test'){
    foreach($view->result as $value){
      kint($value->_entity->get('title')->value);
      $value->_entity->set('title', 'test - ' . time());
    }
  }
}
optimusprime619’s picture

was really running around in circles till i came across your example, using kint in pre_render however froze views' preview ajax... interestingly it did not have the same effect when used in post_render or preprocess_view_view hooks

waqas93’s picture

I have set teaser view using display suite that shows both image and video field, but requirement is to show video on priority and hide image field if video has data in it... i have usign display suite for one column layout, now i have to override the view result before it is rendered and hide the image field if video is not empty.. I am tired of trying it using modulename_views_pre_render hook and dpm($view) . i get nothing.. How to do such thing in d8 and how can i see the complete $view object, what it contains in it?

Vendramini’s picture

I need change a global field "nothing" but don't work like:

$value->_entity->set('nothing', 'test');

Thanks

Vendramini’s picture

I created a new global field using hook_views_data () and Implementing a Plugin Field.