You are here

public function FlagCountManager::getFlagEntityCount in Flag 8.4

Gets the count of entities flagged by the given flag.

For example, with a 'report abuse' flag, this returns the number of entities that have been reported, not the total number of reports. In other words, an entity that has been reported multiple times will only be counted once.

Parameters

\Drupal\flag\FlagInterface $flag: The flag for which to retrieve a flag count.

Return value

int The number of entities that are flagged with the flag.

Overrides FlagCountManagerInterface::getFlagEntityCount

File

src/FlagCountManager.php, line 125

Class

FlagCountManager
Class FlagCountManager.

Namespace

Drupal\flag

Code

public function getFlagEntityCount(FlagInterface $flag) {
  $flag_id = $flag
    ->id();
  if (!isset($this->flagEntityCounts[$flag_id])) {
    $query = $this->connection
      ->select('flag_counts', 'fc')
      ->condition('flag_id', $flag_id);
    $query
      ->addExpression('COUNT(*)');
    $this->flagEntityCounts[$flag_id] = $query
      ->execute()
      ->fetchField();
  }
  return $this->flagEntityCounts[$flag_id];
}