1

I need to create a node and populate its fields with data from a completed Goal (implemented via the Goals module) using Rules. The completed Goal info is fetched using the method from the question about "How to fetch the completed goal as an entity with the Rules module?".

The tokens are available for message display action and also for the node title, but I can't use them for any other fields for some reason. How can I use these tokens to populate the other node fields?

1

To populate some fields of the node to be created using some of the fields of the completed Goal, you need to make those fields (of the Goal) available as row variables in the Views loop of the rule you're using.

Have a look at this improved version of my rule that was included in my answer to your question about "How to fetch the completed goal as an entity with the Rules module?":

{ "rules_create_node_with_details_about_completed_goal" : {
    "LABEL" : "Create a node with details about completed goal",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "goals" ],
    "REQUIRES" : [ "rules", "goals" ],
    "ON" : { "goal_awarded" : [] },
    "DO" : [
      { "VIEW LOOP" : {
          "VIEW" : "goals_info_about_completed_goals",
          "DISPLAY" : "views_rules_1",
          "USING" : { "goal_completed_by_uid" : [ "awarded-user:uid" ] },
          "ROW VARIABLES" : {
            "goal_title" : { "goal_title" : "Title" },
            "goal_id" : { "goal_id" : "Goal ID" },
            "goal_userpoints" : { "goal_userpoints" : "User Points" },
            "field_goal_category" : { "field_goal_category" : "Goal Category" },
            "field_goal_level" : { "field_goal_level" : "Goal Level" }
          },
          "DO" : [
            { "entity_fetch" : {
                "USING" : { "type" : "goal", "id" : [ "goal-id" ] },
                "PROVIDE" : { "entity_fetched" : { "goal_completed" : "Completed goal" } }
              }
            },
            { "drupal_message" : { "message" : "Details about completed goal:\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n- Goal ID: [goal-completed:goal-id]\u003Cbr \/\u003E\r\n- Goal Title: [goal-completed:title]\u003Cbr \/\u003E\r\n- Goal Category: [goal-completed:field_goal_category]\u003Cbr \/\u003E\r\n- Goal Level: [goal-completed:field_goal_level]\u003Cbr \/\u003E" } },
            { "entity_create" : {
                "USING" : {
                  "type" : "node",
                  "param_type" : "article",
                  "param_title" : "[goal-completed:title]",
                  "param_author" : [ "awarded-user" ]
                },
                "PROVIDE" : { "entity_created" : { "node_created" : "Created entity" } }
              }
            },
            { "data_set" : {
                "data" : [ "node-created:field-free-format-text" ],
                "value" : "Goal category = [field-goal-category:value] \/ Goal level = [field-goal-level:value]"
              }
            },
            { "data_set" : {
                "data" : [ "node-created:field-free-format-nr" ],
                "value" : [ "goal-userpoints" ]
              }
            },
            { "entity_save" : { "data" : [ "node-created" ], "immediate" : "1" } }
          ]
        }
      }
    ]
  }
}

Some more details about the improvements in the above rule:

  • Note these additional row variables I added (in the Views loop), i.e.:

    • User Points (goal_userpoints), one of the fields that come with the Goals module.
    • Goal Category (field_goal_category), a custom field I added to my Goals entity (a single value text field, with values such as Contribution, Participation, Moderation, Privilege).
    • Goal Level (field_goal_level), a custom field I added to my Goals entity (a single value text field, with values such as Bronze, Silver, Gold).
  • After I added the extra Rules Action "Create a new entity" (to create the new node), I added these extra Rules Actions to set the values of some of the fields for the node to be created (= node-created):

    • node-created:field-free-format-text (of type text) is assigned the value of Goal category = [field-goal-category:value] / Goal level = [field-goal-level:value] (using 2 of the row variables I added in my view).
    • node-created:field-free-format-nr (of type integer) is assigned the value of goal-userpoints (using 1 of the row variables I added in my view).

With the above in mind, I think the real answer to your question (= "... I can't use them for any other fields for some reason") is that you need to pass the values of the fields you want to use as row variables also (and make sure the data types of those fields you pass along in your view are also matching with the node's field in which you want to use them).

BTW, make sure to understand the difference between:

  • "Goal completed by user", which is specific to a single user who completed a goal.
  • The actual configuration of a "Goal" itself, with fields such as User Points to be granted, or custom fields such as "Level" and "Category", which is identical for all users (and only changes if you change the configuration of a goal).
  • @zoom-ping please review my updated answer (now that I had some time to review your question). IMO your prior comment no longer applies (so you may want to delete that now). Let me know if this updated answer helps to get you going, OK? – Pierre.Vriens Oct 31 '17 at 13:23
  • Thanks! I've managed to populate the text fields, but having trouble with the image field. RulesList view's row variables doesn't have a data type "Image" to select. Having no luck experimenting with "URI" and "File" either. – zoom-ping Nov 2 '17 at 12:01
  • OK, seems like Drupal doesn't want to reuse images. I have worked around it by putting the image in a text field of the node and edited the field template to wrap <img> tags around the URL. Let me know if this is retarded and there's a better way for doing it. Thanks again for your help! – zoom-ping Nov 2 '17 at 19:40

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.