3

I've defined a content type that contains a field collection. The field collection contains 4 fields. When the content is created, it may have one or more of the field collections.

I set up a rule to send the email to users of a specific role. I've used tokens in the rule to specify some data fields in the body. I'm currently stumbling on how to specify the one or more field collection fields in the body of the email.

I found some posts that indicated that I needed to add a condition for "node has field" which I have done. I wasn't sure what to select from the drop down lists, so, I took a logical guess. That seemed to provide some more tokens.

However, I've tried several different methods to specify the field collection in the body of the email, but, all have failed. The email is empty in the spot where I tried to display the field collection.

How can I specify the fields of a field collection in the body of an email?

0

1 Answer 1

1

Part 1: Custom Rule

Have a look at this rule (in Rules export format), which IMO is a possible answer to your question:

{ "rules_field_collection_msg_and_email_body" : {
    "LABEL" : "Field Collection msg and eMail body",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_view" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_article_details" } },
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : "\u003Ch2\u003EThis is a demo of a node containing a field collection ... \u003C\/h2\u003E\r\n\u003Chr \/\u003E\r\n\u003Cstrong\u003EAbout this demo\u003C\/strong\u003E:\u003Cbr \/\u003E\r\n\u003Cbr \/\u003E\r\nThe machine name of the field collection is \u003Cstrong\u003Efield_article_details\u003C\/strong\u003E, and the number of values for it is unlimited.\u003Cbr \/\u003E\r\nThis field collection has these fields:\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n- a text field labeled \u003Cstrong\u003EAuthor\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_author\u003C\/strong\u003E.\u003Cbr \/\u003E\r\n- a decimal field labeled \u003Cstrong\u003EPrice\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_price\u003C\/strong\u003E.\u003Cbr \/\u003E\r\n- a boolean field labeled \u003Cstrong\u003EApproval required\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_approval_required\u003C\/strong\u003E.\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n\u003Chr \/\u003E\r\n\u003Cstrong\u003EPart 1\u003C\/strong\u003E: The  list of items contained in the field collection with machine name \u003Cstrong\u003E\u0027field_article_details\u0027\u003C\/strong\u003E looks like so (note the comma to separate each item):\r\n\u003Chr \/\u003E\r\n[node:field_article_details]\r\n \u003Chr \/\u003E\r\n\u003Cstrong\u003EPart 2\u003C\/strong\u003E: The result of a \u003Cstrong\u003ERules loop\u003C\/strong\u003E that iterates for each of the field collection items (using its unique ID) ...\r\n\u003Chr \/\u003E"
          },
          "PROVIDE" : { "variable_added" : { "variable_for_email_body" : "Variable to use in eMail body" } }
        }
      },
      { "drupal_message" : { "message" : "\u003Ch2\u003EThis is a demo of a node containing a field collection ... \u003C\/h2\u003E\r\n\u003Chr \/\u003E\r\n\u003Cstrong\u003EAbout this demo\u003C\/strong\u003E:\u003Cbr \/\u003E\r\n\u003Cbr \/\u003E\r\nThe machine name of the field collection is \u003Cstrong\u003Efield_article_details\u003C\/strong\u003E, and the number of values for it is unlimited.\u003Cbr \/\u003E\r\nThis field collection has these fields:\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n- a text field labeled \u003Cstrong\u003EAuthor\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_author\u003C\/strong\u003E.\u003Cbr \/\u003E\r\n- a decimal field labeled \u003Cstrong\u003EPrice\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_price\u003C\/strong\u003E.\u003Cbr \/\u003E\r\n- a boolean field labeled \u003Cstrong\u003EApproval required\u003C\/strong\u003E with machine name \u003Cstrong\u003Efield_approval_required\u003C\/strong\u003E.\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n\u003Chr \/\u003E\r\n\u003Cstrong\u003EPart 1\u003C\/strong\u003E: The list of items contained in the field collection with machine name \u003Cstrong\u003E\u0027field_article_details\u0027\u003C\/strong\u003E looks like so (note the comma to separate each item):\r\n\u003Chr \/\u003E\r\n[node:field_article_details]\r\n \u003Chr \/\u003E\r\n\u003Cstrong\u003EPart 2\u003C\/strong\u003E: The result of a \u003Cstrong\u003ERules loop\u003C\/strong\u003E that iterates for each of the field collection items (using its unique ID) ...\r\n\u003Chr \/\u003E" } },
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-article-details" ] },
          "ITEM" : { "article_details_item" : "article details item" },
          "DO" : [
            { "data_set" : {
                "data" : [ "variable-for-email-body" ],
                "value" : "[variable-for-email-body:value]\u003Cbr \/\u003E\r\nThe host entity of this field collection item: [article-details-item:host]\u003Cbr \/\u003E\r\nThe unique ID of the field collection item: [article-details-item:item-id]\u003Cbr \/\u003E\r\nField \u0022field_author\u0022: [article-details-item:field-author]\u003Cbr \/\u003E\r\nField \u0022field_price\u0022: [article-details-item:field-price]\u003Cbr \/\u003E\r\nField \u0022field_approval_required\u0022: [article-details-item:field-approval-required]\u003Cbr \/\u003E\r\n\u003Chr \/\u003E"
              }
            },
            { "drupal_message" : { "message" : "The host entity of this field collection item: [article-details-item:host]\u003Cbr \/\u003E\r\nThe unique ID of the field collection item: [article-details-item:item-id]\u003Cbr \/\u003E\r\nField \u0022field_author\u0022: [article-details-item:field-author]\u003Cbr \/\u003E\r\nField \u0022field_price\u0022: [article-details-item:field-price]\u003Cbr \/\u003E\r\nField \u0022field_approval_required\u0022: [article-details-item:field-approval-required]\u003Cbr \/\u003E\r\n\u003Chr \/\u003E" } }
          ]
        }
      },
      { "mail" : {
          "to" : [ "site:current-user:mail" ],
          "subject" : "Demo of an eMail with a body containing all items of a field collection",
          "message" : [ "variable-for-email-body" ],
          "language" : [ "" ]
        }
      }
    ]
  }
}

Some more details about what this rule does:

- Rules Event: Content is viewed (change to whatever fits).

- Rules Conditions:

  • Entity has field, for entity=node and field (machine name)= field_article_details (adapt this machine name to whatever fits).
  • User has role(s), for user=[site:current-user] and role administrator (adapt this condition to whatever fits, or just delete it entirely, doesn't make any difference).

- Rules Actions (this is where the party starts ... pay attention!):

  • Add a variable (to use in eMail body later on ...), with machine name variable_for_email_body (adapt to whatever fits), and set an initial value which corresponds in the demo with everything from Part 1 and the header line from Part 2.

  • Show a message on the site, this is just for QA-testing, to see what's happening when the previous Rules Action is executed. It shows the equivalent of the initial value assigned to the variable_for_email_body variable from the previous step.

  • Add a Loop for list [node:field-article-details] (are you following, the field collection field!), and for list item use article_details_item. Within this loop, add these Rules Actions:

    • Set a data value, for variable [variable-for-email-body] (which we added as the very first Rules Action!), and with value (watch out, here comes the clue) ... something that looks like so (in each iteration of this Rules loop, we concatenate (append) the actual data for each field collection item to the end of the our [variable-for-email-body:value], and surround it with whatever other fixed text and/or HTML tags that we want to see in our eMail body):

    [variable-for-email-body:value]<br />

    Host entity of this field collection item: [article-details-item:host]<br />

    Unique ID of the field collection item: [article-details-item:item-id]<br />

    Field "field_author": [article-details-item:field-author]<br />

    Field "field_price": [article-details-item:field-price]<br />

    Field "field_approval_required": [article-details-item:field-approval-required]<br />

    <hr />

    • Show a message on the site, this is just for QA-testing (also), to see what's happening when the previous Rules Action is executed. It shows what got concatenated (appended) to the previous value assigned to the variable_for_email_body variable.
  • Send mail, which is extremely basic (after the work completed in the loop above ...):

    • To: [site:current-user:mail] (adapt to whatever fits)
    • Subject: Demo of an eMail with a body containing all items of a field collection (adapt to whatever fits)
    • Message: [variable-for-email-body] (adapt to whatever fits, either you add anything to the message body BEFORE or AFTER this message body here, or you adapt what gets moved as content of this variable in the previous Rules Actions Add a variable or Set a data value)

If you have the Rule UI enabled, you should be able to import the above rule in your own site, provided you first apply some of the changes I suggested, after you adapt the machine names to fit your names. After importing you may want to also change any of its events or conditions in it, or apply changes to any of its actions.

Part 2: Demonstration

Status message

This is a demo of a node containing a field collection ...
About this demo:

The machine name of the field collection is field_article_details, and the number of values for it is unlimited.
This field collection has these fields:

- a text field labeled Author with machine name field_author.
- a decimal field labeled Price with machine name field_price.
- a boolean field labeled Approval required with machine name field_approval_required.

Part 1: The list of items contained in the field collection with machine name 'field_article_details' looks like so (note the comma to separate each item):
Approval required:     No
Price:     12.45
Author:    Pierre Vriens
,
Approval required:     Yes
Price:     9.15
Author:    Barak Obama
,
Approval required:     No
Price:     3.40
Author:    Hillary Trump
,
Approval required:     Yes
Price:     1.23
Author:    Donald Clinton

Part 2: The result of a Rules loop that iterates for each of the field collection items (using its unique ID) ...
Host entity of this field collection item: 73
Unique ID of the field collection item: 1
Field "field_author": Pierre Vriens
Field "field_price": 12.45
Field "field_approval_required": No
Host entity of this field collection item: 73
Unique ID of the field collection item: 2
Field "field_author": Barak Obama
Field "field_price": 9.15
Field "field_approval_required": Yes
Host entity of this field collection item: 73
Unique ID of the field collection item: 3
Field "field_author": Hillary Trump
Field "field_price": 3.40
Field "field_approval_required": No
Host entity of this field collection item: 73
Unique ID of the field collection item: 4
Field "field_author": Donald Clinton
Field "field_price": 1.23
Field "field_approval_required": Yes

eMail body

This is how the entire content of the body of the delivered eMail looks like (look for "Part 2" in it to see the most interesting, I think, part of it):

-------- THIS IS A DEMO OF A NODE CONTAINING A FIELD COLLECTION ...----------

------------------------------------------------------------------------------
*About this demo*:
The machine name of the field collection is *field_article_details*, and the
number of values for it is unlimited.
This field collection has these fields:
- a text field labeled *Author* with machine name *field_author*.
- a decimal field labeled *Price* with machine name *field_price*.
- a boolean field labeled *Approval required* with machine name
*field_approval_required*.
------------------------------------------------------------------------------
*Part 1*: The  list of items contained in the field collection with machine
name *'field_article_details'* looks like so (note the comma to separate each
item):
------------------------------------------------------------------------------
-------- APPROVAL REQUIRED: ------------------------------------------------

No
-------- PRICE: ------------------------------------------------------------

12.45
-------- AUTHOR: -----------------------------------------------------------

Pierre Vriens
 * Delete [1]
 * Edit [2]

,
-------- APPROVAL REQUIRED: ------------------------------------------------

Yes
-------- PRICE: ------------------------------------------------------------

9.15
-------- AUTHOR: -----------------------------------------------------------

Barak Obama
 * Delete [3]
 * Edit [4]

,
-------- APPROVAL REQUIRED: ------------------------------------------------

No
-------- PRICE: ------------------------------------------------------------

3.40
-------- AUTHOR: -----------------------------------------------------------

Hillary Trump
 * Delete [5]
 * Edit [6]

,
-------- APPROVAL REQUIRED: ------------------------------------------------

Yes
-------- PRICE: ------------------------------------------------------------

1.23
-------- AUTHOR: -----------------------------------------------------------

Donald Clinton
 * Delete [7]
 * Edit [8]

 * Add [9]

------------------------------------------------------------------------------
*Part 2*: The result of a *Rules loop* that iterates for each of the field
collection items (using its unique ID) ...
------------------------------------------------------------------------------
Host entity of this field collection item: 73
Unique ID of the field collection item: 1
Field "field_author": Pierre Vriens
Field "field_price": 12.45
Field "field_approval_required": No
------------------------------------------------------------------------------
Host entity of this field collection item: 73
Unique ID of the field collection item: 2
Field "field_author": Barak Obama
Field "field_price": 9.15
Field "field_approval_required": Yes
------------------------------------------------------------------------------
Host entity of this field collection item: 73
Unique ID of the field collection item: 3
Field "field_author": Hillary Trump
Field "field_price": 3.40
Field "field_approval_required": No
------------------------------------------------------------------------------
Host entity of this field collection item: 73
Unique ID of the field collection item: 4
Field "field_author": Donald Clinton
Field "field_price": 1.23
Field "field_approval_required": Yes
------------------------------------------------------------------------------

[1]http://www.example.com/field-collection/field-article-details/1/delete?destination=node/73
[2]http://www.example.com/field-collection/field-article-details/1/edit?destination=node/73
[3]http://www.example.com/field-collection/field-article-details/2/delete?destination=node/73
[4]http://www.example.com/field-collection/field-article-details/2/edit?destination=node/73
[5]http://www.example.com/field-collection/field-article-details/3/delete?destination=node/73
[6]http://www.example.com/field-collection/field-article-details/3/edit?destination=node/73
[7]http://www.example.com/field-collection/field-article-details/4/delete?destination=node/73
[8]http://www.example.com/field-collection/field-article-details/4/edit?destination=node/73
[9]http://www.example.com/field-collection/field-article-details/add/node/73?destination=node/73

I know, the formatting of this eMail body is pretty (really?) poor, but watch what's below the line with "Part 2": everything you possibly may want to know about every item in your field collection, in the eMail body ... which is exactly what this question was about, no? So just reformat the eMail content to your liking (i.e delete everything in it you don't want, and improve the formatting of what you're really interested in).

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.