1

I need some help with Drupal Commons (7.x-3.46), Trusted Contacts (one of its modules), and Rules. I need to create a rule so that whenever a user is approved as a trusted contact, the user is also flagged. It turns out this is not so simple.

In my attempts so far, however, the user ends up flagging him/herself.

Also, it should not depend on the user being logged in, as they might be approved while they're not logged-in. Or flagged twice if both users are logged in at the same time! In other words the approver should flag the approved, regardless of who is logged in at the time.

The rule thus far is

{ "og_og_member_subscribe2" : {
    "LABEL" : "OG member subscribe2",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "og" ],
    "ON" : { "og_user_approved" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "og-membership:state" ], "value" : "1" } },
      { "entity_has_field" : {
          "entity" : [ "og-membership:entity" ],
          "field" : "user_trusted_contacts"
        }
      },
      { "entity_is_of_type" : { "entity" : [ "og-membership:entity" ], 
"type" : "user" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "fired2" } },
      { "variable_add" : {
          "USING" : { "type" : "user", "value" : [ "og-membership:entity" ] 
},
          "PROVIDE" : { "variable_added" : { "user_added" : "Added variable" 
} }
        }
      },
      { "drupal_message" : { "message" : "uid [user-added:uid]  trusted 
contacts are [account:user-trusted-contacts]\r\n\r\n" } }
    ]
  }
}

When the user (= te st) confirms JeffNoyes as a trusted contact, this is the message that returns:

fired2
uid 3 trusted contacts are Jeff Noyes, Drew Robertson, Lisa Rex, Katelyn 
Fogarty, te st
fired2
uid 1 trusted contacts are te st, Jeff Noyes
fired2
uid 3 trusted contacts are Jeff Noyes, Drew Robertson, Lisa Rex, Katelyn 
Fogarty, te st

It's not quite clear to me why it fires 3 times.. It fires first for the approved, uid3 Jeff Noyes. Then a 2nd time for the approver, uid1, te st. And then a 3rd time, once again for uid3.. And Every user is the first user in his/her trusted contacts list, and the last in the list is the one just approved. I can't seem to fetch and limit it to the user who did the approving, or to their list of trusted contacts.

this is the Rules debug information:

" Reacting on event User membership has been approved.
0 ms Reacting on event User membership has been approved.
    11.459 ms Evaluating conditions of rule OG member subscribe2. [edit]
    13.223 ms The condition data_is evaluated to TRUE [edit]
    14.958 ms The condition entity_has_field evaluated to TRUE [edit]
    15.209 ms The condition entity_is_of_type evaluated to TRUE [edit]
    15.23 ms AND evaluated to TRUE.
    " Rule OG member subscribe2 fires. [edit]
    0 ms Rule OG member subscribe2 fires.
    0.201 ms Evaluating the action drupal_message. [edit]
    1.275 ms Evaluating the action variable_add. [edit]
    1.488 ms Added the provided variable user_added of type user [edit]
    109.832 ms Evaluating the action drupal_message. [edit]
    110.099 ms Rule OG member subscribe2 has fired.
    125.403 ms Finished reacting on event User membership has been approved.
    " Reacting on event User membership has been approved.
    0 ms Reacting on event User membership has been approved.
    0.687 ms Evaluating conditions of rule OG member subscribe2. [edit]
    1.389 ms The condition data_is evaluated to TRUE [edit]
    2.329 ms The condition entity_has_field evaluated to TRUE [edit]
     2.716 ms The condition entity_is_of_type evaluated to TRUE [edit]
    2.767 ms AND evaluated to TRUE.
    " Rule OG member subscribe2 fires. [edit]
    0 ms Rule OG member subscribe2 fires.
    0.356 ms Evaluating the action drupal_message. [edit]
    1.104 ms Evaluating the action variable_add. [edit]
    1.551 ms Added the provided variable user_added of type user [edit]
    15.337 ms Evaluating the action drupal_message. [edit]
     15.866 ms Rule OG member subscribe2 has fired.
    18.793 ms Finished reacting on event User membership has been approved.
    " Reacting on event User membership has been approved.
    0 ms Reacting on event User membership has been approved.
    0.309 ms Evaluating conditions of rule OG member subscribe2. [edit]
    0.628 ms The condition data_is evaluated to TRUE [edit]
    1.056 ms The condition entity_has_field evaluated to TRUE [edit]
    1.22 ms The condition entity_is_of_type evaluated to TRUE [edit]
     1.241 ms AND evaluated to TRUE.
    " Rule OG member subscribe2 fires. [edit]
    0 ms Rule OG member subscribe2 fires.
    0.146 ms Evaluating the action drupal_message. [edit]
    0.501 ms Evaluating the action variable_add. [edit]
    0.693 ms Added the provided variable user_added of type user [edit]
    6.823 ms Evaluating the action drupal_message. [edit]
    7.075 ms Rule OG member subscribe2 has fired.
    8.385 ms Finished reacting on event User membership has been approved.
0

1 Answer 1

1

Understanding what is happening

Since you're using Rules Event "User membership has been approved", it appears that the rule is triggered for each of the user's trusted contacts. That seems to explain why you get that fired2-message 3 times.

So unless you can find another Rules Event that only gets triggered once (for all trusted contacts together), you'll have to come up with a refinement of your Rules Conditions and/or Rules Actions, so that the relevant Rules Actions are only performed for the "trusted contact" that triggered the execution of the actual rule. Read on for an attempt to make that work also ...

Attempt to make it work as you want

Your Rules Conditions seems OK, but at a minimum you're missing a crucial Rules Action: a Rules Loop. If you're not familiar with such loops, then I strongly recommend to have a look at the tutorial titled "Lists and loops".

In your case, you should add a Rules Loop related to that token account:user-trusted-contacts that is already shown in your last Rules Action. After you do, you should iterate over each item in that list (= each user that is a contact, let's assume you name it like current_list_item).

For each iteration of such Rules Loop, you should then "do something", to only "flag" that user (which is processed in that iteration step), provided (= on condition) that the user matches the user for which the Rules Event is triggered.

With that, you have 2 challenges left:

  • do some extra research to see which token you have available in your rule, to know what the user is for which the Rules Event is triggered. My best guess would be that something related to that og-membership (such as a uid?), let's assume its name is some_token.
  • implement such "condition" (as an extra Rules Action!), which you can do by using the Conditional Rules module, so that you can specify Rules Conditions within Rules Actions. The actual condition should then be to check that current_list_item equals some_token.

Instead of using the Conditional Rules module, you could also rework the Rules Action to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

Advanced debugging

Something else that will help you find the answer to your question, is to have a look at Rules debugging ... Within the Rules settings, experiment a bit with the debugging facilities that come with the Rules module. At least have a look at the "Rules Evaluation Log" (to check that the Rules logic being executed, actually matches what you expect that logic to be).

Depending on what the result of that type of debugging is, you may also want to (temporary) enable the Devel module, so that you can (temporary) add a Rules Action like "Display a value".

For way more details about these 2 variations, refer to Rules debugging, as explained in the answer to "How can I display the value of a variable inside a condition?".

Putting it all together

After your additional edits (partially based on what I wrote in this answer above), you might get it to work if you add this additional Rules Actions:

  1. Use the Conditional Rules module to add, as your very first Rules Action, a condition to check that "current user" equals token [user-added:uid]. And move all your existing Rules Actions inside that Condition. Doing so will limit the execution of those Rules Actions to your 2nd iteration only for the testcase you're using. Do an extra test to ensure you get this intermediate result.
  2. Add an extra Rules Action (also within the Condition added in step 1) which is actually a Rules loop to iterate over each of the items contained in [account:user-trusted-contacts]. Inside this loop, use Conditional Rules again to check that the list item being processed is different from the "current user". If that condition is satisfied, do whatever you want to process "yet another trusted contact" of the current user.
5
  • Thank you for your detailed answer and great explanation. I really appreciate it and it helps make sense of what's going on. I find the conditional loops a bit daunting. I'll do my best to get to grips with it and report back.. One thing that I've already noticed, though, is that I can't seem to fetch a unique uid, since it is the nature of the trusted-contacts that both users appear simultaneously in both groups.
    – dnoncoder
    Apr 7, 2018 at 6:08
  • Thanks again. Before I dive into the loop thing, I edited my question a little. I expanded the rule (inspired by your bingo-trick) to show the uid it's reacting to, and the new messages and log it returns.
    – dnoncoder
    Apr 8, 2018 at 6:43
  • This question is actually an offshoot of another question I posted (drupal.stackexchange.com/questions/259168/…) regarding how to use trusted contacts as a selection rule in Panels. I didn't find a solution for it, so thought to use flags instead. Perhaps an easier solution is there? Something like a list count comparison perhaps? So if an item has been added to list, then flag the last item on the list. Though I understand the problem with that is that an item is added to both the users' lists... hmmpf.
    – dnoncoder
    Apr 8, 2018 at 6:46
  • @dnoncoder with the extra Rules debugging info, I think you should be able to make it work as per what I wrote at the end of my updated answer. Your turn again to try to digest/implement what I wrote ... Let me know how it goes ... Apr 8, 2018 at 7:48
  • yes. thank you Pierre.Vriens for your detailed support. I think I followed correctly. First we narrow it down to the logged-in user, then loop through his/her list of trusted contacts, excluding him/her. One thing I've noticed is that then it flags ALL of the user's trusted contacts, not only the latest addition. Is that the behavior you expected? (Actually for this use case that's fine. You can trust your contacts again and again.In a different use case, for example unflagging an un-trusted contact, it would present a problem) Thanks again, D.
    – dnoncoder
    Apr 13, 2018 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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