3

I am using Flag 7.x-3.0 to enable users to flag file entities. I only want users to be able to flag files that other users have uploaded. However, I am missing the checkbox option "allow users to flag own content" that I remember having been a part of earlier versions of Flag module.

Has this feature been removed?

1

This is a specific use-case so it may be better to approach this using the Flag API. Specifically, try hook_flag_access and add this to your custom module (or create one). This function takes the following parameters:

hook_flag_access($flag, $entity_id, $action, $account)

and what you are specifically interested in is the following:

$entity_id: The id of the entity in question.

$account: The user on whose behalf to test the flagging action.

The $entity_id will allow you to determine what user id created the entity and the account object will already contain the account of the current user so this function will block flag display if they are the same:

function YOUR_MODULE_flag_access($flag, $entity_id, $action, $account) {

  //load data for the entity 
  $entity = entity_load($entity_type, array($entity_id));

  // if the account that created the entity is the same as the user attempting to flag it then block access
  if ($entity[$entity_id]->uid == $account->uid) {
    return false;
  }

}
| improve this answer | |
  • The line if ($entity[$entity_id]->uid == $account->uid) { causes an error, "Notice: Undefined variable: entity" and "Notice: Trying to get property of non-object". What is the correct way to load the file entity and the file entity's author id? – deinqwertz Jul 19 '13 at 15:02
  • I used $author_id = db_query("SELECT uid FROM {file_managed} WHERE fid = :fid", array(':fid' => $entity_id))->fetchField(); and then if ($author_id == $account->uid) { instead, which works like a charm. Can anyone tell me if this would be a lightweight solution? – deinqwertz Jul 19 '13 at 15:46
  • Right - again, forgetting that you're using a managed file field here this query would work better for you. This query is about as preformant as you can get, especially since you're no longer using the entity_load function above. – schnippy Jul 19 '13 at 18:01
1

That feature is still in there. Edit your flag and you will see it about midway through the settings

enter image description here

| improve this answer | |
  • I do see it for flags of flag type "Nodes" but not for flags of flag type "File". Is File Entity Module causing issues so that the flag access settings don't appear for "File" flags? Or are flag access settings just not provided for "File" flags? – deinqwertz Jul 14 '13 at 11:43
  • Ah that must be it. Files are their own entities and most likely do not have the same author property as nodes. You'll probably have to put in a feature request, do something in code as schnippy shows below or you could do it with rules. – Nigel Waters Jul 14 '13 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.