2

I am building a rule that on node save will loop through taxonomy terms added to an entity reference (taxonomy term) field, build up a list of users from an entity reference (user) field in those taxonomy terms, and e-mail those users.

The problem I'm having is that my data selector won't give me the tokens for the user reference field in the taxonomy term loop. This rules issue tells me that I need to have the "entity has field condition" condition applied or the "content is of type" condition. Unfortunately all my conditions are referencing the node that is being saved.

I'm guessing that the answer would be to build the loop into some type of component so I can do a second condition check, but I'm not sure what the right approach for that would be. How can I let rules know what kind of entity I'm looping through, so it will give me access to the custom fields on that entity?

2

Have a look at the question about "How to access commerce_product field of line-items using Rules to flag the Commerce Product?". The accepted answer to it (disclosure: it's mine ...) explains how that question can be solved (also) by creating a Rules Component, and which looks like so:

{ "rules_perform_an_action_on_a_selected_line_item" : {
    "LABEL" : "Perform an action on a selected line item",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "selected_line_item" : { "label" : "Selected Line Item", "type" : "commerce_line_item" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "selected-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "selected-line-item:commerce-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "product_fetched" : "Fetched Product" } }
        }
      },
      { "drupal_message" : { "message" : "Some details about this product: Product ID = [product-fetched:product-id], Product SKU = [product-fetched:sku], Product Title = [product-fetched:title], Product Creator = [product-fetched:creator]" } }
    ]
  }
}

Note the parameters used in this Rules Component, i.e. selected_line_item ( = the line item being processed). And also note the type of that parameter (a Commerce line item).

The main rule just executes this Rules Component, as part of a Rules Loop in it, whereas in each iteration of that loop the actual line item being processed is passed as the value for that parameter. So with this example in mind, it seems that this might be a solution to your questions:

How can I let rules know what kind of entity I'm looping through

The type of the parameter you pass to the Rules Component.

... so it will give me access to the custom fields on that entity

I think your Rules Component would not need any extra Rules Conditions, though if needed (as shown in my Rules Component example with the Rules Condition related to "Commerce Product"), you could add relevant other Rules Conditions if needed.

For the sake of completeness, some more details about that Rules Component:

  • By adding the Rules Condition, I got access to the value of the id (key) of the Commerce Product that the line item was about.
  • Since I had made the value of that id (key) of the Commerce Product avaiable, I could use the Rules Action to fetch an entity by id (entity being the Commerce Product). And by doing that, from then on I had ALL the data (fields) of the Commerce Product available.
  • 1
    Thanks, that got me further along. I'm still getting a single value where it seems like I should be getting multiple so I'll try to work that out tomorrow when I'm not working from my phone, and probably accept. – UltraBob Aug 23 '17 at 11:54
  • @UltraBob merci for your first feedback! To also resolve your remaining issue (single value instead of multiple values), I can think (guess?) of 2 possible reasons (remaining errors). It may help to also pinpoint that remaining issue if you edit your question and include an export of the rule(s) you have so far (if if they don't work entirely yet). Keep me updated! – Pierre.Vriens Aug 23 '17 at 12:01
  • 1
    Thanks Pierre, if I have a chance to look at it and rule out a stupid error like adding to list in the wrong layer of loop, I'll come back and provide more detail. – UltraBob Aug 23 '17 at 12:02
  • As I suspected, the problem was a dumb mistake on my part. This answer was just the hint I needed, thanks! – UltraBob Aug 24 '17 at 0:45
  • 1
    @UltraBob that seems to make sense, though your question somehow made me wonder if the Rules Link module could be a possible solution (= trigger some rule when anhyperlink is clicked). – Pierre.Vriens Aug 24 '17 at 7:24

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.