function hook_views_post_render

Post-process any rendered data.

This can be valuable to be able to cache a view and still have some level of dynamic output. In an ideal world, the actual output will include HTML comment-based tokens, and then the post process can replace those tokens. This hook can be used by themes.

Example usage. If it is known that the view is a node view and that the primary field will be a nid, you can do something like this:


  <!--post-FIELD-NID-->

And then in the post-render, create an array with the text that should go there:


  strtr($output, array('<!--post-FIELD-1-->' => 'output for FIELD of nid 1');

All of the cached result data will be available in $view->result, as well, so all ids used in the query should be discoverable.

Parameters

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

string $output: A flat string with the rendered output of the view.

\Drupal\views\Plugin\views\cache\CachePluginBase $cache: The cache settings.

See also

\Drupal\views\ViewExecutable

Related topics

7 functions implement hook_views_post_render()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

MediaLibraryHooks::viewsPostRender in core/modules/media_library/src/Hook/MediaLibraryHooks.php
Implements hook_views_post_render().
media_library_views_post_render in core/modules/media_library/media_library.module
Implements hook_views_post_render().
test_basetheme_views_post_render in core/modules/system/tests/themes/test_basetheme/test_basetheme.theme
Implements hook_views_post_render().
test_subtheme_views_post_render in core/modules/system/tests/themes/test_subtheme/test_subtheme.theme
Implements hook_views_post_render().
ViewsTestConfigHooks::viewsPostRender in core/modules/views/tests/modules/views_test_config/src/Hook/ViewsTestConfigHooks.php
Implements hook_views_post_render().

... See full list

1 invocation of hook_views_post_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 854

Code

function hook_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
  // When using full pager, disable any time-based caching if there are fewer
  // than 10 results.
  if ($view->pager instanceof Drupal\views\Plugin\views\pager\Full && $cache instanceof Drupal\views\Plugin\views\cache\Time && count($view->result) < 10) {
    $cache->options['results_lifespan'] = 0;
    $cache->options['output_lifespan'] = 0;
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.