1

I'm trying to create a rule with the Rules module that posts a node when users achieve a goal (using the Goals module 7.x-1.3). I want to get fields from the goal like the goal image and other fields, but there's no data selector for the goal completed.

I can't find a way to fetch the completed goal entity. I've tried using a data comparison condition, but it'll only give "goal" data selector without any fields.

Any hints?

  • Have you tried the 'entity has field', or 'entity is of bundle' conditions on the goal? Sometimes that is required to have the desired fields show up. – Neograph734 Sep 9 '17 at 13:38
  • Yeah, but it only gives me site and awarded-user selectors. – zoom-ping Sep 9 '17 at 16:49
1

Have a look at this rule (in Rules export format), which is an example of how to get this to work:

{ "rules_show_a_message_after_goal_completion" : {
    "LABEL" : "Show a message after goal completion",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "goals" ],
    "REQUIRES" : [ "goals", "rules" ],
    "ON" : { "goal_awarded" : [] },
    "IF" : [ { "goals_goal_condition" : { "goal_id" : "2" } } ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "goal", "id" : "2" },
          "PROVIDE" : { "entity_fetched" : { "goal_fetched" : "Fetched goal" } }
        }
      },
      { "drupal_message" : { "message" : "Some details about the \u0022[goal-fetched:ftitle\u0022 goal which you just completed:\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n- \u003Cstrong\u003EGoal Description\u003C\/strong\u003E = [goal-fetched:field-goal-description]\u003Cbr \/\u003E\r\n- \u003Cstrong\u003EGoal Category\u003C\/strong\u003E = [goal-fetched:field-goal-category]\u003Cbr \/\u003E\r\n- \u003Cstrong\u003EGoal Level\u003C\/strong\u003E = [goal-fetched:field-goal-level]\u003Cbr \/\u003E" } }
    ]
  }
}

Some details about this rule:

  • Rules Event: "A goal is completed by a user", as in your question.
  • Rules Condition: "The goal completed by a user", a Rules Condition provided by the Goals module (disclosure: I'm its maintainer). In my case, I used the goal with goal id = "2" (replace with the goal id that matches your case).
  • Rules Actions:
    • "Fetch entity by id", whereas the entity is a goal, and the id of it is (also) "2" (replace with the goal id that matches your case).
    • "Show a message on the site", which is just to demo / test some of the field values related to that goal (adapt the machine names of those fields to match your field names).

So by adding the Rules Condition "The goal completed by a user", and by also adding the Rules Action to "Fetch entity by id", in any subsequent Rules Action you should have access to all fields of the goal (with the goal id that matches your case).

  • Is there a way to do fetch the goal that was completed and triggered the rule? Seems like I would have to create a rule for each goal (and there are many of them). – zoom-ping Sep 12 '17 at 9:01
  • @zoom-ping The question in your last comment here is actually a NEW question. Would you agree that my answer here DOES answer your original question, which is about "find a way to fetch the completed goal entity"? If so, how about marking this answer as "accepted", and then post a new question as a followup-question (with a link to this one)? After you do, I might also answer that new question (like "there is no out-of-the box answer with only the Goals module, but I can think of at least 1 alternative to create a single rule to cover all goals in it, provided you use an extra module also"). – Pierre.Vriens Sep 12 '17 at 12:02
  • Ok, thanks! I'll try to phrase my question better. – zoom-ping Sep 12 '17 at 18:50

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.