-2

I'm using Drupal 7.57 and I have 2 roles in my website, role X and role Y.

I want to compare a field (field_code_postal) of the X member profile of the current-user, with the fields (field_code_postal) of all Y members profiles, and this when X member save a new entityform submission.

Each time the field field_code_postal is the same, an email should be sent to that Y member.

So if there are 100 Y members and for 20 of them the value of the field is equal to the value of it for the current user X, then 20 emails are sent.

Do I need to use flags, or are the roles sufficient?

I'm a bit lost, any help?

6
  • It is possible with simple hook in a custom module, but please specify what Drupal version you are using and the machine name of the field.
    – AltaGrade
    Commented Mar 21, 2018 at 15:14
  • Thanks for the answer, i'm using drupal 7.57 with rules, the machine name of the field is : field_code_postal, i added it in the main post ^^
    – Quentin
    Commented Mar 21, 2018 at 15:21
  • @Pierre.Vriens : After saving a new entityform submission
    – Quentin
    Commented Mar 21, 2018 at 15:24
  • I used node as an example in below answer, however it can be replaced by another entity type. What is your entity type?
    – AltaGrade
    Commented Mar 21, 2018 at 22:39
  • It's good you are going to try the proposed methods, however please do not leave your question with no answer accepted if you want others to keep helping you. Choose one and accept it or share with your own solution.
    – AltaGrade
    Commented Mar 23, 2018 at 1:21

3 Answers 3

2

Import and give a try to the following rule. Note I used After saving new content trigger, however you can change to or add another event suitable to your case. For example, you could add After saving a new comment.

{ "rules_notify_users_with_the_same_postal_code" : {
    "LABEL" : "Notify users with the same postal code",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert" : [] },
    "IF" : [
      { "NOT data_is_empty" : { "data" : [ "node:author:field-code-postal" ] } }
    ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "user",
            "property" : "field_code_postal",
            "value" : [ "site:current-user:field-code-postal" ],
            "limit" : "100"
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "entity-fetched" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "mail" : {
                "to" : "list-item:mail",
                "subject" : "A new post #[node:nid] [node:title] has been posted",
                "message" : "A new post #[node:nid] [node:title] has been posted",
                "from" : "[site:mail]",
                "language" : [ "node:language" ]
              }
            }
          ]
        }
      }
    ]
  }
}
5
  • Reference or just a regular field doesn't matter that much as long as you implement similar rule with the same principles. Anyway, I've updated my answer providing exported rule that should work.
    – AltaGrade
    Commented Mar 21, 2018 at 17:40
  • It looks to me that the "field_code_postal" field is related to user entities, not to nodes. So what's the purpose of those node-tokens in your exported rule? Commented Mar 21, 2018 at 18:26
  • The [ "node:author:field-code-postal" ] is not a node token. PO mentioned entity submission without specifying what kind of entity it was, so I used node entity as an example. It can be easily replaced by any entity, but what is most important is that we need to derive entity author's field_code_postal anyway.
    – AltaGrade
    Commented Mar 21, 2018 at 22:37
  • Hi @AltaGrade, i'm comming back to say thank you, it's work pretty well, i changed some fields and added a conditionnal to check the role of the user. i was using entityform ^^
    – Quentin
    Commented Mar 27, 2018 at 13:53
  • 1
    Glad it worked for you.
    – AltaGrade
    Commented Mar 27, 2018 at 14:51
2

D7, use rules views module, make a users rules view and load it in rules as a views rules loop, in view settings add a filter criteria Y members = your special field, inside loop, add an action: fetch entity by id: UID from loop, action: send email to fetched UID.

EDIT: because of Pierre comment i'm adding a little more info. Let me first explain something: ID's (uid, nid, tid, etc.) must be passed from rules views to rules as an integer, if you don't, rules will not understand what you are sending in, this is done in rules view settings in "row variable settings" in middle column. So rules inside loop recieves UID as an integer, this means you do not have that user object avilable in rules, that's why you must fetch user by the integer value (UID) passed to rules. Now, you have two objects to work with, inside rule views loop, you have to compare current logged-in user postalcode field value againts the postalcode field of the fetched user, and last, send your email or whatever. you may need also rules conditional module

1
  • That seems indeed another approach, though I wonder why you'd need "fetch entity by id". Since for any data you'd need to send the eMail, you can just include them as an extra field in your view, so that those data are available as rules tokens within the Views-loop in your rule. No? The only thing I'm unsure about: how to pass the value of the "special field" for the current user (from the rule to the view, within the Rules action to create that Views loop). Can you refine your answer about that? Commented Mar 21, 2018 at 16:08
1

You may want to give it a try to process the results of a VBO enabled view via Rules, whereas your rule performs 2 Rules Actions:

  • Load the VBO list that you want to be processed.
  • Add a loop to perform a Rules Action, for each row contained in your Views results.

For more details, have a look at the tutorial "How to process the results of a VBO enabled view via Rules?".

To actually trigger such Rules Action, you should be able to use pretty much any Rules Event that fits your needs.

Also, to select only those members (= Y) with the same value of that field_code_postal field, it seems you can use either of these techniques:

  • Pass it as an argument to the view (so that you only get 1 views result for each member). Refer to the video in the linked tutorial about how to do so.
  • Yse an extra Rules Component in which you actually send those eMails, and in which you perform an extra Rules Condition to check that the value for the field_code_postal field of member Y is equal to the one for member X (you probably want to pass this value as an extra Rules Component parameter).

Your Answer

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

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