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) {
      // What code should I write here?
  • You can also use function MYMODULE_views_pre_render(\Drupal\views\ViewExecutable $view) { .... Next install Devel and the Devel submodule Kint. Then you can ksm($result) to print out vars. (Don't click the plus icon in the output, click only titles.) – leymannx Mar 27 '18 at 20:17
  • Check other hook_views_pre_render() sample snippets on api.drupal.org/api/drupal/core!modules!views!views.api.php/… – leymannx Mar 27 '18 at 20:18
  • Thanks guys for your help. Prior to put this question I have tried to find a solution. I have read drupal 8 views api, and some snippets examples, but all of them (I have found) change title, image style or similar. Neither of this examples are for change the values. On the other hand, I don't think this is a duplicate of the question that Neograph734 are indicating. I have read this question prior to post my question and as I understood it is for change the title. – briast Mar 28 '18 at 19:42
  • @briast, your question is how to alter $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. – Neograph734 Mar 29 '18 at 19:28
  • I have updated my question to be more precise. I hope it was clear enough. Thanks for your help. – briast Mar 30 '18 at 7:49

It depends on what your result stores. It can be like entity with relation or just field:

1) $result->_entity->set('field_Name', 'xxxxxxxxxxx');

2) $value->node__field_Name = 'xxxxxxxxxxx';

| improve this answer | |
  • What about global field ? – Pankaj Yogi Dec 17 '18 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.