You are here

public function FlagCountManager::getFlagFlaggingCount in Flag 8.4

Gets the count of flaggings for the given flag.

For example, if you have an 'endorse' flag, this method will tell you how many endorsements have been made, rather than how many things have been endorsed.

Parameters

\Drupal\flag\FlagInterface $flag: The flag.

Return value

int The number of flaggings for the flag.

Overrides FlagCountManagerInterface::getFlagFlaggingCount

File

src/FlagCountManager.php, line 104

Class

FlagCountManager
Class FlagCountManager.

Namespace

Drupal\flag

Code

public function getFlagFlaggingCount(FlagInterface $flag) {
  $flag_id = $flag
    ->id();
  $entity_type = $flag
    ->getFlaggableEntityTypeId();

  // We check to see if the flag count is already in the cache,
  // if it's not, run the query.
  if (!isset($this->flagCounts[$flag_id][$entity_type])) {
    $query = $this->connection
      ->select('flagging', 'f')
      ->condition('flag_id', $flag_id)
      ->condition('entity_type', $entity_type);

    // Using an expression is faster than using countQuery().
    $query
      ->addExpression('COUNT(*)');
    $this->flagCounts[$flag_id][$entity_type] = $query
      ->execute()
      ->fetchField();
  }
  return $this->flagCounts[$flag_id][$entity_type];
}