Issue #2793169 by Matroskeen, Manuel Garcia, anmolgoyal74, Pooja Ganjage, Abhijith S, Lendude, dww, quietone: hook_views_post_render provides inaccurate information

merge-requests/1589/head
Alex Pott 2021-12-27 13:02:35 +00:00
parent 8220c91597
commit acd7ac969e
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
1 changed files with 20 additions and 19 deletions

View File

@ -820,36 +820,37 @@ function hook_views_pre_render(ViewExecutable $view) {
} }
/** /**
* Post-process any rendered data. * Post-process any render data.
* *
* This can be valuable to be able to cache a view and still have some level of * The module or theme may add, modify or remove elements in $output after
* dynamic output. In an ideal world, the actual output will include HTML * rendering.
* 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 * If a module wishes to act on the rendered HTML of the view rather than the
* primary field will be a nid, you can do something like this: * structured content array, it may use this hook to add a #post_render
* callback:
* @code * @code
* <!--post-FIELD-NID--> * // The object must implement \Drupal\Core\Security\TrustedCallbackInterface.
* $output['#post_render'][] = '\Drupal\my_module\View::postRender';
* @endcode * @endcode
* And then in the post-render, create an array with the text that should *
* go there: * See \Drupal\Core\Render\RendererInterface::render() for #post_render
* @code * documentation.
* strtr($output, array('<!--post-FIELD-1-->' => 'output for FIELD of nid 1'); *
* @endcode * Alternatively, it could also implement hook_preprocess_HOOK() for
* All of the cached result data will be available in $view->result, as well, * the particular view template, if there is one.
* so all ids used in the query should be discoverable.
* *
* @param \Drupal\views\ViewExecutable $view * @param \Drupal\views\ViewExecutable $view
* The view object about to be processed. * The view object being processed.
* @param string $output * @param array $output
* A flat string with the rendered output of the view. * A structured content array representing the view output. The given array
* depends on the style plugin and can be either a render array or an array of
* render arrays.
* @param \Drupal\views\Plugin\views\cache\CachePluginBase $cache * @param \Drupal\views\Plugin\views\cache\CachePluginBase $cache
* The cache settings. * The cache settings.
* *
* @see \Drupal\views\ViewExecutable * @see \Drupal\views\ViewExecutable
*/ */
function hook_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) { function hook_views_post_render(ViewExecutable $view, array &$output, CachePluginBase $cache) {
// When using full pager, disable any time-based caching if there are fewer // When using full pager, disable any time-based caching if there are fewer
// than 10 results. // 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) { if ($view->pager instanceof Drupal\views\Plugin\views\pager\Full && $cache instanceof Drupal\views\Plugin\views\cache\Time && count($view->result) < 10) {