2

I have created a rule to add terms to an existing Term Field. The rule fires correctly, it's adding the Term to the Field. However, it's overwriting all the existing terms within the field.

I have tried to follow the steps from this similar question as well as this Tutorial, however, it's still a no go for me.

More details about my setup:

  • Vocabulary: Emails (machine name: emails)
  • Email Field: Email Field (machine_name: field_primary_email), which is a single value field.
  • Term Reference: Email Term Reference (machine_name: field_email_term_reference)

The requirement is to display Email communication basis of the Email address present in the node [ Contact Content Type].

I have a rule which picks up the primary email & creates a taxonomy term from it. The second rule, assigns the relevant terms basis of the email present in the node. The email address in the node may change, however, the terms are to remain to display the past communication. When the new email is updated in the node, the first rule creates a new taxonomy term basis of the updated email address. When the new term is created, the second rule picks up the term & assigns it to the Term reference field along with the existing ones. It is not to overwrite the terms already present in the field. Basis of the terms present in the node, I can display the email communication.

My rule I created so far (in Rules export format):

{ "rules_update_email_term_reference" : {
"LABEL" : "Update Email Term Reference",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_view" : [] },
"IF" : [
  { "entity_is_of_bundle" : {
      "entity" : [ "node" ],
      "type" : "node",
      "bundle" : { "value" : { "candidates" : "candidates", "clients" : "clients" } }
    }
  },
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_email_term_reference" } }
],
"DO" : [
  { "LOOP" : {
      "USING" : { "list" : [ "node:field-email-term-reference" ] },
      "ITEM" : { "current_term" : "Current Term" },
      "DO" : []
    }
  },
  { "entity_query" : {
      "USING" : {
        "type" : "taxonomy_term",
        "property" : "name",
        "value" : [ "node:field-primary-email" ]
      },
      "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
    }
  },
  { "list_add" : {
      "list" : [ "node:field-email-term-reference" ],
      "item" : [ "entity-fetched:0" ],
      "unique" : "1"
    }
  }
]

} }

4
  • The loop pulls in all the terms attached to the node. You're right, the action is not within the loop, however, I tried with that too. I set the action within the loop as well, it still overwrites the terms within the field Commented May 15, 2017 at 13:21
  • @Pierre.Vriens Hi, I have edited the question with the information. I do believe it is not a Rules bug, I am sure I have missed a step, but not able to pin the cause. Please do guide me. Thank you for your help :) Commented May 15, 2017 at 14:35
  • "field_primary_email" is a single value field Commented May 15, 2017 at 15:14
  • OK, your turn again. Time to digest the various "issues" I described in my answer. Based on your additonal feedback, I might further extend it later on. Extra question: "field_email_term_reference" is the node's term ref field (that's clear). But what exactly is that other node related field, which is "field_primary_email" (ie what is that used for)? I'm still confused about the purpose of those 2 fields (how they relate to each other, or what the difference and/or their purpose is). Can you edit your question to also clarify that? Commented May 15, 2017 at 16:02

1 Answer 1

2

Your rule in your question seems to have "a few" (not just one) issues that need to be addressed. So I'll build a list of these issues here (and will add more of them later on if needed) ...

Issue 1:

Think about this: "Why have that 1st Rules action (a loop), if inside that loop there is nothing happening (as per the "DO" : [] in it)?". Either the loop is just redundant, or there is some Rules action(s) still missing inside that loop (more research needed).

If it was me to decide, for now I'd delete that Rules Action. And if needed I'd restore it later on if I have a real reason to do so (i.e. if there is at least 1 Rules Action that I know I'd need, as part of that loop.

Issue 2:

At the core of your question, there is your "It is not to overwrite the terms already present in the field". The typical mistake in using Rules is that someone incorrectly uses a Rules Action like "Set a data value" (for a multi value field), and that will remove all existing values for that field. The correct way is to use a Rules Action "Add to list" (that will preserve existing values). Your last Rules Action does indeed perform a "Add to list" (as it should be done to achieve your goal), but what is suspicious in that Rules Action is that you use entity-fetched:0 ... That is a single value (as per the ...:0 part of it, but entity-fetched is a multi value list (it has to be like that, otherwise you wouldn't use that ...:0 part in it). But nowhere there is an indication why "the first value" in the multi value list (stored in entity-fetched), is the actual value you want to add to your list.

Issue 3:

The rules event your using (= node_view), combined also with the Rules Condition, is rather suspicious also. For 2 reasons: (a) what if the event and/or condition are not satisfied (in that case none of the Rules actions get executed) and (b) if you're sure the Rules event and Rules condition is "always" satisfied, then you might be doing those Rules Actions over and over again.

Though there is possibly only reasons to do so in 1 specific scenario. I.e. for those situations related to your "I have a rule which picks up the primary email & creates a taxonomy term from it" (let's call that your 1st rule, as compared to your 2nd rule shown in your question). It seems to me that your 2nd rule (as in your question) should be reworked like so:

  • Create a new Rules Component, with Rules Conditions and Rules Actions as you already have in your 2nd rule here.
  • Add and extra Rules Action to your 1st rule, which is simply/only to perform (= trigger the execution) of your 2nd rule.
  • In doing this redesign, you may have relevant / interesting data available in your 1st rule (like node id, user id, eMail id, term value, etc), which you can pass on to the Rules Component as a so called "Parameter" for that component.
  • And by having such parameters available in your Rules Component, you might possibly simplify the set of required Rules Actions needed to achieve your goal.
  • Don't forget to at least disable (or even delete) your current 2nd rule, after you reworked it to such Rules Component.

Possible other issues to be resolved

The above issues are, for now, only some suggestions to help you come closer to your goal. It's well possible (actually I'm about sure ...) you'll need other issues to be resolved also. But pretty sure the above issues will help you come closer. Btw, debugging is the art of taking away all possible reasons why it doesn't work ... as you want it to work.

2
  • 1
    Thank you very much for the detailed information, appreciate it. I'll rework on the rules & hope to get it right. I'll let you know how it goes. Thank you Commented May 15, 2017 at 18:21
  • Sorry for the delayed response. Yes, Its working fine now Thanks to your guidance. Appreciate it. Thank You. Commented Feb 19, 2018 at 6:15

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.