In D8.6 I have problems with counting in a view. After the counting a message in the site should display the total. The message is sometimes show and sometimes not.

I used several different view hooks as some are not executed after caching. Therefore I used the list with all hooks for views

function nvgh_views_post_render(ViewExecutable $view) {

  $result = $view->result;
  $total = 0;

  foreach ($result as $row => $rowinfo) {
    // kint($rowinfo->nid);
      $node = \Drupal\node\Entity\Node::load($rowinfo->nid);
      $total = $total + $node->get('field_pe_punten')->value;
      // $pe_punten = $node->get('field_pe_punten')->value;

  drupal_set_message('Het totale aantal punten bedraagt voor dit overzicht: ' . $total, 'status', TRUE);


Which hook should I use in orde to see the message every time?

  • There is none. Don't use drupal_set_message(). Prepare it in preprocess, e.g. with hook_preprocess_view() and then display it in a twig template, so it can be cached together with the view – Berdir Aug 17 '18 at 22:54
  • Hi Berdir, thxs I do not understand the caching different in your approach and the approach of mine using a custom module. Can you explain this? – Justme Aug 18 '18 at 7:59
  • 1
    drupal_set_message() is stored in global context and can't be part of the cache. What I'm suggesting would be cached together with the view. You could also create a views are plugin and display it above/below the form. See blog.studio.gd/node/16 or the related question drupal.stackexchange.com/questions/203438/… – Berdir Aug 18 '18 at 12:41

Your Answer

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

Browse other questions tagged or ask your own question.