You are here

function flag_get_entity_flags in Flag 7.3

Return a list of users who have flagged an entity.

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

$entity_type: The type of entity that will be retrieved. Usually 'node'.

$entity_id: The entity ID to check for flagging.

$flag_name: (optional) The name of a flag if wanting a list specific to a single flag.

Return value

A nested array of flagging records (i.e. rows from the {flagging} table, rather than complete Flagging entities). The structure depends on the presence of the $flag_name parameter:

  • if $flag_name is omitted, the array is keyed first by the user ID of the users that flagged the entity, then by flag name. Each value is then the flagging record.
  • if $flag_name is given, the array is keyed only by user ID. Each value is the flagging record.

If no flags were found an empty array is returned.

3 calls to flag_get_entity_flags()
flag_flag_is_flagged_access_check in plugins/access/flag_is_flagged/flag_is_flagged.inc
Check for access.
flag_properties_get_flagging_users in ./flag.module
Getter callback that returns users who flagged the given entity.
_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_flags'
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 2213
The Flag module.

Code

function flag_get_entity_flags($entity_type, $entity_id, $flag_name = NULL) {
  $entity_flags =& drupal_static(__FUNCTION__, array());
  if (!isset($entity_flags[$entity_type][$entity_id])) {
    $flag_names = _flag_get_flag_names();
    $result = db_select('flagging', 'fc')
      ->fields('fc')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $entity_id)
      ->orderBy('timestamp', 'DESC')
      ->execute();
    $entity_flags[$entity_type][$entity_id] = array();
    foreach ($result as $flagging_data) {

      // Build a list of flaggings for all flags by user.
      $entity_flags[$entity_type][$entity_id]['users'][$flagging_data->uid][$flag_names[$flagging_data->fid]] = $flagging_data;

      // Build a list of flaggings for each individual flag.
      $entity_flags[$entity_type][$entity_id]['flags'][$flag_names[$flagging_data->fid]][$flagging_data->uid] = $flagging_data;
    }
  }
  if (empty($entity_flags[$entity_type][$entity_id])) {
    return array();
  }
  if (isset($flag_name)) {
    if (isset($entity_flags[$entity_type][$entity_id]['flags'][$flag_name])) {
      return $entity_flags[$entity_type][$entity_id]['flags'][$flag_name];
    }
    return array();
  }
  return $entity_flags[$entity_type][$entity_id]['users'];
}