You are here

public function FlagCountManager::getEntityFlagCounts in Flag 8.4

Gets flag counts for all flags on an entity.

Provides a count of all the flaggings for a single entity. Instead of a single response, this method returns an array of counts keyed by the flag ID:


array(
  my_flag => 42
  another_flag => 57
);

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

Return value

array An array giving the counts of all flaggings on the entity. The flag IDs are the keys and the counts for each flag the values. Note that flags that have no flaggings are not included in the array.

Overrides FlagCountManagerInterface::getEntityFlagCounts

File

src/FlagCountManager.php, line 82

Class

FlagCountManager
Class FlagCountManager.

Namespace

Drupal\flag

Code

public function getEntityFlagCounts(EntityInterface $entity) {
  $entity_type = $entity
    ->getEntityTypeId();
  $entity_id = $entity
    ->id();
  if (!isset($this->entityCounts[$entity_type][$entity_id])) {
    $this->entityCounts[$entity_type][$entity_id] = [];
    $query = $this->connection
      ->select('flag_counts', 'fc');
    $result = $query
      ->fields('fc', [
      'flag_id',
      'count',
    ])
      ->condition('fc.entity_type', $entity_type)
      ->condition('fc.entity_id', $entity_id)
      ->execute();
    foreach ($result as $row) {
      $this->entityCounts[$entity_type][$entity_id][$row->flag_id] = $row->count;
    }
  }
  return $this->entityCounts[$entity_type][$entity_id];
}