You are here

function flag_get_counts in Flag 7.3

Same name and namespace in other branches
  1. 5 flag.module \flag_get_counts()
  2. 6.2 flag.module \flag_get_counts()
  3. 6 flag.module \flag_get_counts()
  4. 7.2 flag.module \flag_get_counts()

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
);

When called during a flagging or unflagging (such as from a hook implementation or from Rules), the count this returns takes into account the the flagging or unflagging that is in the process of being performed.

Parameters

$entity_type: The entity type (usually 'node').

$entity_id: The entity ID (usually the node ID).

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.

2 calls to flag_get_counts()
flag_flag::get_count in includes/flag/flag_flag.inc
Returns the number of times an item is flagged.
_flag_hook_test_record_invocation in tests/flag_hook_test/flag_hook_test.module
Store the hook name and parameters into a variable for retrieval by the test.
2 string references to 'flag_get_counts'
flag_flag::_decrease_count in includes/flag/flag_flag.inc
Decreases the flag count for an object and clears the static counts cache.
flag_flag::_increase_count in includes/flag/flag_flag.inc
Increases the flag count for an object and clears the static counts cache.

File

./flag.module, line 1821
The Flag module.

Code

function flag_get_counts($entity_type, $entity_id) {
  $counts =& drupal_static(__FUNCTION__);
  if (!isset($counts[$entity_type][$entity_id])) {
    $counts[$entity_type][$entity_id] = array();
    $query = db_select('flag', 'f');
    $query
      ->leftJoin('flag_counts', 'fc', 'f.fid = fc.fid');
    $result = $query
      ->fields('f', array(
      'name',
    ))
      ->fields('fc', array(
      'count',
    ))
      ->condition('fc.entity_type', $entity_type)
      ->condition('fc.entity_id', $entity_id)
      ->execute();
    foreach ($result as $row) {
      $counts[$entity_type][$entity_id][$row->name] = $row->count;
    }
  }
  return $counts[$entity_type][$entity_id];
}