2

I'm working on a project where users vote other users using the Flag module. Each user has a department term reference field. I would like to stop a user from flagging another user if they have the same department. Is it possible? Please help! Thanks in advanced!

1

Finally I managed to solve my problem by creating a Rule. Here are the steps you can follow to achieve this:

  1. Create a new Rule and select as the Event "A user has been flagged, under 'your flag'";
  2. Add a condition "Entity has field" and select "flagging-user" as the entity and your field as the "Field";
  3. Add a new condition "List contains item" and select as a list your created field but it must be like this "flagging-user:field-your-field";
  4. Now at the same condition select as the item one of your field value;
  5. Add a AND condition;
  6. Add two more conditions exactly like you did at step 2, 3 and 4 but change "flagging-user" to "flagged-user".
  7. Finally, add an action with "Unflag a User" and select your flag first, then at the User select "flagged-user", at the "User on whose behalf to flag" select "flagging-user" and at "Skip permission check" check "True".

That's it. Hope I helped someone else.

As requested here's an exported code of the actual Rule:

{ "rules_nu_poate_vota_manager" : {
"LABEL" : "Nu poate vota manager",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "flag" ],
"ON" : { "flag_flagged_apreciere" : [] },
"IF" : [
  { "entity_has_field" : { "entity" : [ "flagging-user" ], "field" : "field_raion" } },
  { "list_contains" : { "list" : [ "flagging-user:field-raion" ], "item" : "18" } },
  { "AND" : [] },
  { "entity_has_field" : { "entity" : [ "flagged-user" ], "field" : "field_raion" } },
  { "list_contains" : { "list" : [ "flagged-user:field-raion" ], "item" : "18" } }
],
"DO" : [
  { "flag_unflaguser" : {
      "flag" : "apreciere",
      "user" : [ "flagged-user" ],
      "flagging_user" : [ "flagging_user" ],
      "permission_check" : "1"
    }
  },
  { "drupal_message" : {
      "message" : "Nu po\u0163i aprecia colegii de echip\u0103!",
      "type" : "warning"
    }
  }
]

} }

  • 1
    I trust this works for you, however it would clarify your answer if you add an export of your rule (+1 after you do so and ping me via an extra comment) ... Also, step 3 is redundant ... since AND is how multiple conditions are combined by default (try it if you want to check to see it works .... or checkout drupal.stackexchange.com/a/228125/39516). – Pierre.Vriens Jul 14 '18 at 12:28
  • Just edited the steps, now should work as expected. – staion16 Jul 14 '18 at 12:42
  • Merci for considering my prior comment. With this improved version I'm not sure anymore about the "AND" (if it is needed or not). Though I must admit that this updated version is even harder to digest/understand .. And hence adding an export of your actual rule would, at least for me, help to really understand/digest your answer. Up to you to yes/no do so ... – Pierre.Vriens Jul 14 '18 at 12:47
  • Just updated the question with the export code of the actual Rule in use. – staion16 Jul 16 '18 at 10:51
  • Merci for adding this exported rule. Now I do understand what your rule does (only reading the exported rule). However, the "AND" condition is really NOT needed for what your rule is designed to do. Also, you're not "stopping users" from flagging, instead your are doing like an "undo" after a user is flagged. Would you agree? Interested in an extra answer to (more or less) do as in your title? – Pierre.Vriens Jul 16 '18 at 18:16

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.