2

I'm trying to create a Rule that will create a comment on another post when a node is created. I would like the comment to contain a number of fields from the original post in a formatted manner.

For example: I have a content type called "Project Status Reports" which has 3 text area fields: field_preparation, field_methods and field_outcomes and a node reference field: field_project_node.

When a Project Status Report is created, the user selects a node to associate it with (in field_project_node) and fills the 3 text areas.

I would like to create a Rule that gets triggers when a Project Status Report is created, which will then make a comment on the node referenced in field_project_node. I would like the body of the comment to look something like this:

<h4>Preparation</h4>
<p>[node:field_preparation]</p>
<h4>Methods</h4>
<p>[node:field_methods</p>
<h4>Outcomes</h4>
<p>[node:field_outcomes]</p>

I have created the Rule successfully and have it triggering however I am unable to work out how to fill the Main Body Text of the comment with formatted text which will do the replacements. All I can seem to do is either add static html or have it post a single field.

Is this possible? Thanks in advance

3
  • Please edit your question to describe, in plain English, what the rule you included is supposed to do. While doing so, make sure to also mention which content type(s) and which (machine names of) fields are used to do what. Without such improvement of your question, it appears to me like not obvious to understand what you're trying to achieve. PS: I think I can guess what's wrong or missing, but I don't want to write an answer based on guessing what the question may be about ... Jun 6 '17 at 7:46
  • Thanks for your comment Pierre, I've rewritten the text. I hope its clearer now. Jun 7 '17 at 0:17
  • Well, you confirmed what I was guessing about in the PS of my prior comment ... refer to my answer for what IMO might be the solution to your question. PS: maybe you should re-edit your question and also include that exported rule from revision 1 of your question again (at the end of it or so). Curious for your additinal feedback ... Jun 7 '17 at 7:12
2

Step 1: Getting around a known comment body limitation

I think the core of your question relates to what's written in Comment # 10 of the Rules (support) issue titled "Access and Set Comment extra fields ", i.e.:

... param_comment_body doesn't assume any data types except 'formatted text' ...

This issue includes a Rule example that looks like so:

{ "rules_test" : {
    "LABEL" : "Comment bot",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_update" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_status" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "comment",
            "param_subject" : "zzz",
            "param_node" : [ "node" ],
            "param_author" : [ "node:author" ],
            "param_comment_body" : { "value" : {
                "value" : "This text will be replaced in the next action.",
                "format" : "filtered_html"
              }
            }
          },
          "PROVIDE" : { "entity_created" : { "new_comment" : "New comment" } }
        }
      },
      { "data_set" : {
          "data" : [ "new-comment:comment-body:value" ],
          "value" : [ "node:field-status" ]
        }
      }
   ]
  }
} 

The clue to solve your issue (= work around?) is to rework your exported rule (as in revision 1 of your question) so that it uses that last Rules Action starting with "data_set".

Step 2: Combining 3 fields

However, since in your case you want to combine those 3 text area fields, you should also include these extra Rules Actions (prior to that Rules Action starting with "data_set"):

  • Add a variable (to use as your comment-body later on), say with machine name like variable_for_comment_body.
  • 3 (similar) Rules Actions to Set a data value, for variable [variable-for-comment-body] (added in the prior Rules Action), whereas each of these 3 Rules Actions will refer to the value of each of your 3 text area fields.

For an example rule where you can see this technique at work, refer to my answer to "How to use Rules to send an eMail with a body containing all items of a field collection?" (you don't need a Rules loop though, just those 3 similar Rules Actions.

And with the above logic in place, theh value in your last Rules Action starting with "data_set" simply becomes [ "variable-for-comment-body" ].

2
  • thanks for the reply Pierre, I did see this answer in another thread however I'm not sure how I can get multiple fields formatted in this example. Correct me if I'm wrong, but doesn't this example just replace the param_comment_body value with the node:field-status field? Do I add multiple data_sets to add more fields? Jun 7 '17 at 8:50
  • @chrisgooley : check my updated answer (= Step 2 that I added). Jun 7 '17 at 9:19

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.