You are here

function flag_get_entity_flag_counts in Flag 7.3

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.

When called during a flagging or unflagging (such as from a hook implementation or from Rules), the flagging or unflagging that is in the process of being performed:

  • will be included during a flagging operation
  • will STILL be included during an unflagging operation. That is, the count will not yet have been decreased.

This is because this queries the {flagging} table, which only has its record deleted at the very end of the unflagging process.

Parameters

$flag: The flag.

$entity_type: The entity type. For example, 'node'.

Return value

int The number of flaggings for the flag.

2 calls to flag_get_entity_flag_counts()
flag_rules_action_fetch_entity_flag_count in ./flag.rules.inc
Base action implementation: Fetch count of flags for a particular entity type.
_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_entity_flag_counts'
flag_flag::flagging_delete in includes/flag/flag_flag.inc
Unflag an entity by deleting a Flagging.
flag_flag::flagging_insert in includes/flag/flag_flag.inc
Create a new Flagging to flag an entity.

File

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

Code

function flag_get_entity_flag_counts($flag, $entity_type) {
  $counts =& drupal_static(__FUNCTION__);

  // We check to see if the flag count is already in the cache,
  // if it's not, run the query.
  if (!isset($counts[$flag->name][$entity_type])) {
    $counts[$flag->name][$entity_type] = array();
    $result = db_select('flagging', 'f')
      ->fields('f', array(
      'fid',
    ))
      ->condition('fid', $flag->fid)
      ->condition('entity_type', $entity_type)
      ->countQuery()
      ->execute()
      ->fetchField();
    $counts[$flag->name][$entity_type] = $result;
  }
  return $counts[$flag->name][$entity_type];
}