I'm using Drupal 8 Views in combination with a search index from Search API. When using Global: Result summary in the header or footer of the View, the total number of results shown in the summary is not the same than the total of results of the Search index.

Similar problem has been reported on Drupal 7, with a solution using hook_views_pre_execute

/**
* Implements hook_views_pre_execute().
*/
function MY_MODULE_views_pre_execute(&$view) {
  // Set the View to compute total rows.
  if ($view->name === 'MACHINE_NAME_OF_YOUR_VIEW') {
    $view->get_total_rows = TRUE;
}
}

Unfortunately, I'm unable to use this code snippet in Drupal 8... Maybe a syntax problem?

/**
* Implements hook_views_pre_execute().
*/
function searchimprove_views_pre_execute(ViewExecutable $view) {    
if ($view->name === 'rechercher') {    
    $view->get_total_rows = TRUE;
}
}

// 'searchimprove' is the name of my module
// 'rechercher' is the machine name of my view.
  • Any idea about how to achieve this code with the good syntax in Drupal 8? – Emilie.Viau Dec 9 at 12:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.