0

In D8 I would like to sum a field in a view within a custom module called nvgh. I choose to use a hook for a start. I'm not sure this will be the good hook anyway.

https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_post_execute/8.6.x

function nvgh_views_post_execute(ViewExecutable $view) {

  if ($view->total_rows > 12) {
    \Drupal::messenger()
      ->addStatus(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  }
}

I directly get an error:

The website encountered an unexpected error. Please try again later. TypeError: Argument 1 passed to nvgh_views_post_execute() must be an instance of ViewExecutable, instance of Drupal\views\ViewExecutable given in nvgh_views_post_execute() (line 96 of modules\custom\nvgh\nvgh.module).

  • a) Is this a bug in Drupal 8?
  • b) Which hook is common to use for pseudo code like

    for each row in view result

    • count a field
    • display a message

closed as off-topic by Clive Aug 17 '18 at 11:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – Clive
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is a general php language issue so off topic here – Clive Aug 17 '18 at 11:45
1

You have to define the statement of ViewExecutable like use \Drupal\views\ViewExecutable

use \Drupal\views\ViewExecutable;

function nvgh_views_post_execute(ViewExecutable $view) {
  if ($view->total_rows > 12) {
    \Drupal::messenger()
      ->addStatus(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  }
} 

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