7

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?
    }    
  }
}
5
  • 1
    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, 2018 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, 2018 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, 2018 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. Mar 29, 2018 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, 2018 at 7:49

3 Answers 3

5

I was able to alter results just setting the view result property within hook_views_pre_render. My code looks something like:

$results = $view->result;
$filteredResults = [];

foreach ($results as $result) {
  if ($result->_entity->field_somefield->value === 'somevalue') {
    $filteredResults[] = $result;
  }
}

$view->result = $filteredResults;
$view->total_rows = count($filteredResults);
1
  • 3
    This solution would not help to rewrite the result. This is just filtering out some of the results. Oct 1, 2021 at 12:17
0

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';

1
  • 3
    What about global field ? Dec 17, 2018 at 14:01
0

View hooks like hook_views_pre_render() should be placed in an execution.inc file within the module directory: mymodule.views_execusion.inc

The execution order of View hooks is as follows:

  1. hook_views_pre_view
  2. hook_views_pre_build
  3. hook_views_post_build
  4. hook_views_pre_execute
  5. hook_views_post_execute
  6. hook_views_pre_render
  7. hook_views_post_render

Read what these hooks do here

The following is a good example for Drupal 8.x|9.x. Your goal is to create a new result set of entities and set it to $view->result. It is important to set the index property so the results will render properly.

Slightly altered from @rosemarystanley

/**
 * Implements hook_views_pre_render().
 */
function HOOK_views_pre_render(ViewExecutable $view) {

  $results = $view->result;
  $filteredResults = [];

  foreach ($results as $result) {
    if ($result->_entity->field_name->value === 'fieldValue') {
      $filteredResults[] = $result;
    }
  }

  // Important to set index values for view results rendering
  foreach ($filteredResults as $key => $row) {
    $row->index = $key;
  }

  $view->result = $filteredResults;
  $view->total_rows = count($filteredResults);
}
1
  • It's important to set index values when using views bulk operations.
    – Dimetry
    Jul 21 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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