When I use contextual filters in a view, I can select an option to display a summary when the argument is not present. If I do it, it shows a list of node IDs instead of titles. For example, I get the following output, where the first number is the node ID, and the second number is the count of nodes.
- 1256 (12)
- 4323 (723)
- 5643 (34)
I want is to rewrite the output and replace the node ID with its title. ViewExecutable
has a $args
property I can check to know when the argument is not present.
How could I achieve this in a hook? In Drupal 7 I could use hook_views_post_execute()
. I modified $view->result
and changed it on my own.
I tried to do the same with Drupal 8 and the same hook, but hook_views_pre_render()
gets a ViewExecutable
object, and I don't know how to change it. I used the following code.
function MYMODULE_views_pre_render(ViewExecutable $view) {
if ($view->id() == 'myidoftheview') {
$results = $view->result;
foreach ($results as $result) {
\Drupal::logger('myview')->notice('<pre>'.print_r($result,TRUE).'</pre>');
// What code should I write here?
}
}
}
function MYMODULE_views_pre_render(\Drupal\views\ViewExecutable $view) { ...
. Next install Devel and the Devel submodule Kint. Then you canksm($result)
to print out vars. (Don't click the plus icon in the output, click only titles.)hook_views_pre_render()
sample snippets on api.drupal.org/api/drupal/core!modules!views!views.api.php/…$view
, which is exactly what the other question is about. Yes, the demonstrated alteration is indeed editing the title, but there is much more to edit. Please update your question to include what you want to edit in your results. I have to admin that I initially did not see you were already using the same hook.