1

I try to send a list of nodes with certain value in a specific field via the Rules module.

This is what I have so far:

  • Event: "After updating existing content of type MyContentType"

  • Conditions: "Parameter: Data to compare: [node:field-status], Data value: sendmail"

  • Actions:

    • "Parameter: Entity type: Node, Property: Status, Value: [node:field-status], Limit result count: 100 Provides variables: Fetched entity (entity_fetched)"

    • "Send HTML e-mail"

Here is an export of this rule:

{ "rules_send_node_list" : {
    "LABEL" : "Send node list",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "mimemail" ],
    "ON" : { "node_update--mycontenttype" : { "bundle" : "mycontenttype" } },
    "IF" : [ { "data_is" : { "data" : [ "node:field-status" ], "value" : "0" } } ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "node",
            "property" : "field_status",
            "value" : [ "node:field-status" ],
            "limit" : "100"
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "mimemail" : {
          "key" : "[node:title]",
          "to" : "to@example.com",
          "from_name" : "QX",
          "from_mail" : "from@example.com",
          "subject" : "Node flagged",
          "body" : "[entity-fetched:0]\r\n[entity-fetched:1]\r\n[entity-fetched:2]\r\n[entity-fetched:3]",
          "language" : [ "" ]
        }
      }
    ]
  }
}

The list with nodes with the field-status set to "sendmail" is indeed send out. The problem I'm facing here is that I have to put in the email body tokens like [entity-fetched:0], [entity-fetched:1], etc.

In the section Replacement patterns for Fetched entity I only have [entity-fetched:0] till [entity-fetched:3] available. But I have more nodes than that. I would like to have the complete list with these flagged nodes.

Any suggestions about this?

1

How to get a complete list

Your Rules Events and Conditions are pretty sure OK, but 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".

The result of your first Rules Action is actually a list, which as per the "PROVIDE" that is shown in your exported rule, is stored in a variable named entity_fetched (BTW, try to use a more meaningful variable name for such variables, eg nodes_with_status_sendmail).

As you seemed to have understood already, the entity-fetched:0, ..., entity-fetched:3, correspond to your 1st, 2nd, 3rd and 4th node in that list of fetched entities (nodes).

What you need to do to repair your Rules Actions, are these steps:

  1. AFTER your first Rules Action (which fetch those nodes, and which is fine), use the link "Add a loop" as the next (2nd) Rules Action. When doing so, specify these options for it (note the looooong variable name for my 'List item', I bet you get its meaning just from its name ...):

    • Parameter: List: [entity-fetched]
    • List item: Single node with status sendmail (single_node_with_status_sendmail)
  2. Within that loop you added, use the link to the right of it (in the Rules UI), to "Add an action". That way the Rules Action you add, will be performed as part of the loop. The actual Rules Action to add is similar to the (last) Rules Action you already have, with machine name "mimemail". As you will notice while creating a similar Rules Action, you'll now be able to select/specify tokens that relate to ... single_node_with_status_sendmail. However, the content of your eMail message body should now only (repeat: only!) refer to the "List item" being processed in the current iteration of the Rules loop (instead of those 4 items in your existing "mimemail" action).

    As you will probably understand, since you're now processing 1 single node in each iteration, and it doesn't matter how many nodes you have in your list of nodes, there is no more restriction to 4 nodes only.

  3. After you completed the previous step, there is no more need for the existing (last) Rules action about "mimemail", so just delete that Rules Action.

With these changes you solve your limitation of 4 nodes maximum. However, in case you don't realize yet, you may have introduced a new challenge, as detailed below ...

Solution to your problem ... might (will?) change the problem

Using the approach detailed above, the result of your adapted rule is that you will get 1 separate eMail for each node fetched via that first Rules Action (which created the list of nodes to be processed). If you typically only have a few nodes that may not be a real issue to worry about. But if you have dozens (or more?) of such nodes then you might want to consider consolidating all such eMails in a single eMail, which contains (in its eMail body) the complete list of nodes.

A possible solution to implement such consolidation, is similar to what is shown in the Rules example included in my answer to "How to concatenate all token values of a list in a single field within a Rules loop?". In your case, you could make it work like so:

  1. Add some new Rules variable that will be used later on as part of the eMail body, before the start of your loop. Say you name the variable nodes_list_var_for_email_body.
  2. Within your loop, for each iteration, prepend or append the value for each "list item" to that variable nodes_list_var_for_email_body.
  3. Move your (new) "mimemail" action outside your loop, and after the loop completed. And finetune the details (configuration) of your (new) "mimemail" Rules Action. When doing so, you'll be able to select the token for nodes_list_var_for_email_body to include anywhere in your eMail body.

Summary

If you think about it, you'll end up more or less with a variation of the last Rules Action in the rule you already have, in which the body part now contains nodes-list-var-for-email-body instead of entity-fetched:0, ..., entity-fetched:3.

But for this (crucial) nodes-list-var-for-email-body token to be available, you need to (a) add the Rules Loop and (b) add 2 more Rules Actions to create the content (value) of nodes-list-var-for-email-body.

Voilà

... That's really all it takes to get this to work.

0

You need to loop through the list of nodes. Create a views list of the nodes to loop through, add a vbo field (doesn't matter which) and save it. Now rules components (action set) will have the list available to loop through.

  • I'm now using VBO to load a list of views items. The view is setup to show the exact list i want to send. Problem is that rules again only gives me tokens for [entity-list:0] - [entity-list:3]. Each of this lists contains exactly one node title. So pretty much where i was before. – martinro Jun 9 '15 at 0:17
  • @martinro : indeed, no surprise, works as implemented. For a solution that should work, refer to my answer I just posted. To Lu R: Rules and VBO work great together, but for this question your answer is not a solution ... I think. – Pierre.Vriens Nov 24 '16 at 19:58

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.