You are here

function views_aggregator_count in Views Aggregator Plus 8

Same name and namespace in other branches
  1. 7 views_aggregator_functions.inc \views_aggregator_count()

Aggregates a field group as a count of the number of group members.

Parameters

array $groups: An array of groups of rows, each group indexed by group value.

object $field_handler: The handler for the view column to count groups members in.

string $group_regexp: An optional regexp to count, if omitted all non-empty group values count.

string $column_regexp: An optional regexp to count, if omitted all non-empty group values count.

Return value

array An array of values, one for each group and one for the column.

2 string references to 'views_aggregator_count'
views.view.va_test_style_table.yml in tests/modules/views_aggregator_test_config/test_views/views.view.va_test_style_table.yml
tests/modules/views_aggregator_test_config/test_views/views.view.va_test_style_table.yml
ViewsAggregatorResultsTest::testColumnResultFunctions in tests/src/Functional/Plugin/ViewsAggregatorResultsTest.php
Test the column functions.

File

./views_aggregator_functions.inc, line 237
views_aggregator_functions.inc

Code

function views_aggregator_count(array $groups, $field_handler, $group_regexp = NULL, $column_regexp = NULL) {
  $values = [];
  $count_column = 0;
  $regexp = isset($group_regexp) ? $group_regexp : $column_regexp;
  if (preg_match('/[a-zA-Z0-9_]+/', $regexp)) {

    // Interpret omitted brace chars in the regexp as a verbatim text match.
    $regexp = "/{$regexp}/";
  }
  foreach ($groups as $group => $rows) {
    $count = 0;
    foreach ($rows as $num => $row) {
      $cell = views_aggregator_get_cell($field_handler, $num, TRUE);
      if (isset($cell) && $cell != '' && (empty($regexp) || preg_match($regexp, $cell))) {
        $count++;
      }
    }
    $values[$group] = $count;
    $count_column += $count;
  }
  $values['column'] = $count_column;
  return $values;
}