function flag_get_user_flags in Flag 7.3
Same name and namespace in other branches
- 5 flag.module \flag_get_user_flags()
- 6.2 flag.module \flag_get_user_flags()
- 6 flag.module \flag_get_user_flags()
- 7.2 flag.module \flag_get_user_flags()
Find what a user has flagged, either a single entity or on the entire site.
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: (optional) The entity ID to check for flagging. If none given, all entities flagged by this user will be returned.
$uid: (optional) The user ID whose flags we're checking. If none given, the current user will be used.
$sid: (optional) The user SID (provided by Session API) whose flags we're checking. If none given, the current user will be used. The SID is 0 for logged in users.
Return value
If returning a single item's flags (that is, when $entity_id isn't NULL), an array of the structure [flag_name] => ( flagging_id => [flagging_id], uid => [uid], entity_id => [entity_id], timestamp => [timestamp], ...)
If returning all items' flags, an array of arrays for each flag: [flag_name] => [entity_id] => Object from above.
6 calls to flag_get_user_flags()
- flag_comment::get_flagging_record in includes/
flag/ flag_comment.inc - Overrides flag_flag::get_flagging_record().
- flag_flag::get_flagging_record in includes/
flag/ flag_flag.inc - Returns the flagging record.
- flag_properties_get_flagged_entities in ./
flag.module - Getter callback that returns entities the given user flagged.
- flag_properties_get_flagging_boolean in ./
flag.module - Getter callback that returns whether the given entity is flagged.
- hook_flag_validate in ./
flag.api.php - Perform custom validation on a flag before flagging/unflagging.
6 string references to 'flag_get_user_flags'
- FlagLinkTypeConfirmTestCase::testFlag in tests/
flag.test - Test usage of the flag confirm form.
- flag_comment::get_flagging_record in includes/
flag/ flag_comment.inc - Overrides flag_flag::get_flagging_record().
- flag_comment_flag_test_comment_load in tests/
flag_comment_flag_test/ flag_comment_flag_test.module - Implements hook_comment_load().
- flag_comment_flag_test_entity_view in tests/
flag_comment_flag_test/ flag_comment_flag_test.module - Implements hook_entity_view().
- flag_flag::flagging_delete in includes/
flag/ flag_flag.inc - Unflag an entity by deleting a Flagging.
File
- ./
flag.module, line 2133 - The Flag module.
Code
function flag_get_user_flags($entity_type, $entity_id = NULL, $uid = NULL, $sid = NULL) {
$flagged_content =& drupal_static(__FUNCTION__);
$uid = !isset($uid) ? $GLOBALS['user']->uid : $uid;
$sid = !isset($sid) ? flag_get_sid($uid) : $sid;
if (isset($entity_id)) {
if (!isset($flagged_content[$uid][$sid][$entity_type][$entity_id])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$sid][$entity_type][$entity_id] = array();
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id)
->condition(db_or()
->condition('uid', $uid)
->condition('uid', 0))
->condition('sid', $sid)
->execute();
foreach ($result as $flagging_data) {
$flagged_content[$uid][$sid][$entity_type][$entity_id][$flag_names[$flagging_data->fid]] = $flagging_data;
}
}
return $flagged_content[$uid][$sid][$entity_type][$entity_id];
}
else {
if (!isset($flagged_content[$uid][$sid][$entity_type]['all'])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$sid][$entity_type]['all'] = array();
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('entity_type', $entity_type)
->condition(db_or()
->condition('uid', $uid)
->condition('uid', 0))
->condition('sid', $sid)
->execute();
foreach ($result as $flagging_data) {
$flagged_content[$uid][$sid][$entity_type]['all'][$flag_names[$flagging_data->fid]][$flagging_data->entity_id] = $flagging_data;
}
}
return $flagged_content[$uid][$sid][$entity_type]['all'];
}
}