How to get total number of rows on a block of a view On hook_block_view_alter() I need to get a total number of rows that this block displays.

For example, I have a view with the following block:



On hook_block_view_alter() I do the appropriate checks in order to weed out unnecessary blocks and get the one shown in the figure above.

function mymodule_block_view_alter(array &amp;$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if (!preg_match('~^views_block:taxonomy_tag_list~', $block-&gt;getPluginId())) {
    return;
  }

  $view = $block-&gt;getViewExecutable();
  // Gets the number of rows
  $totalRows = $view-&gt;get_total_rows;
}


But for some reason, $totalRows contains NULL.

Here is the object of the current view:


Why can't I get the total number of rows? Maybe I'm using the wrong hook?
Use a views_view_ hook, i.e. views_view_unformatted,  views_view_list  etc... I tried this with a PHP count for my views block and it worked great. 



My function is:

function MYTHEME_preprocess_views_view_unformatted(&amp;$vars) {
  $num_rows = $vars["rows"];
  $count = count($num_rows);
  echo '&lt;h2&gt;This view has ' . $count . ' rows&lt;/h2&gt;&lt;br&gt;';
}


This yielded the number of rows in the view. 

Printed, I see: