Below is a variation of the rule that I already posted in my answer in the related question. The Rules Event used in this answer, is (still) related to "when viewing content (of type article)", as was (also) the question in the related questions. In another answer, I'll add some details about not using this event, but some other event(s) instead (and a variation of this rule for that scenario). This rule here will display specific Drupal messages, to indicate what has happened, and what still needs to happen (to have Field C show the requested value).
In my own setup (where I QA-tested this ...) it's using 3 fields (as in the question here):
- Field A, machine name
field_a
, a term reference using a vocabulary with machine name vocabulary_a
, which has terms Term A1
, Term A2
and Term A3
.
- Field B, machine name
field_b
, a term reference using a vocabulary with machine name vocabulary_b
, which has terms Term B1
, Term B2
and Term B3
.
- Field C, machine name
field_c
, a term reference using a vocabulary with machine name vocabulary_c
.
But there are a few things about this rule to be aware off (more details on that below the rule). Here is my rule:
{ "rules_combine_taxonomy_terms_in_a_3rd_taxonomy" : {
"LABEL" : "Combine taxonomy terms in a 3rd taxonomy",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "rules_conditional" ],
"ON" : { "node_view--article" : { "bundle" : "article" } },
"DO" : [
{ "drupal_message" : { "message" : "Value of term for Field C has to be set to \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E, i.e. the combination of the value of Field A (= \u003Cstrong\u003E[node:field-a]\u003C\/strong\u003E) and the value of Field B (= \u003Cstrong\u003E[node:field-b]\u003C\/strong\u003E). However the page is already rendered BEFORE subsequent Rules Actions will be executed. So to actually see the new value for Field C, an extra page refresh is needed." } },
{ "variable_add" : {
"USING" : { "type" : "text", "value" : "[node:field-a][node:field-b]" },
"PROVIDE" : { "variable_added" : { "combined_term" : "Combined Term" } }
}
},
{ "entity_query" : {
"USING" : {
"type" : "taxonomy_term",
"property" : "vocabulary",
"value" : "vocabulary_c"
},
"PROVIDE" : { "entity_fetched" : { "existing_terms" : "Existing Terms" } }
}
},
{ "variable_add" : {
"USING" : { "type" : "boolean", "value" : "0" },
"PROVIDE" : { "variable_added" : { "new_term_exist" : "New term exists" } }
}
},
{ "LOOP" : {
"USING" : { "list" : [ "existing-terms" ] },
"ITEM" : { "current_term" : "Current term" },
"DO" : [
{ "CONDITIONAL" : [
{
"IF" : { "text_matches" : { "text" : [ "current-term:name" ], "match" : "[combined-term:value]" } },
"DO" : [
{ "data_set" : { "data" : [ "new-term-exist" ], "value" : "1" } },
{ "data_set" : { "data" : [ "node:field-c" ], "value" : [ "current-term" ] } },
{ "drupal_message" : { "message" : "Term \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E already existed and will be used to automatically set the value for Field C to this term also ... But only \u003Cstrong\u003Enext time this node is viewed\u003C\/strong\u003E (as per the used Rules Event ...)!!! If you\u0027re in a hurry to see the new value for Field C, just refresh this page." } }
]
}
]
}
]
}
},
{ "CONDITIONAL" : [
{
"IF" : { "data_is" : { "data" : [ "new-term-exist" ], "value" : "0" } },
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "taxonomy_term",
"param_name" : "[combined-term:value]",
"param_vocabulary" : "vocabulary_c"
},
"PROVIDE" : { "entity_created" : { "term_created" : "Created term" } }
}
},
{ "entity_save" : { "data" : [ "term-created" ], "immediate" : "1" } },
{ "data_set" : { "data" : [ "node:field-c" ], "value" : [ "term-created" ] } },
{ "drupal_message" : { "message" : "Term \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E did not yet exist and was created just now. It will be used to automatically set the value for Field C to this term also ... But only \u003Cstrong\u003Enext time this node is viewed\u003C\/strong\u003E (as per the used Rules Event ...)!!! If you\u0027re in a hurry to see the new value for Field C, just refresh this page." } }
]
}
]
}
]
}
}
Things to be aware of:
For reasons I don't know (yet), you cannot simply "import" the above rule, using the typical Rules UI to do so. Try to import it somewhere, I bet you'll run into an import error which looks pretty similar (or "exactly"?) to this one:
Integrity check for the imported configuration failed. Error message:
The data type of the configured argument does not match the
parameter's <em class="placeholder">
value</em>
requirement..
But there is a work around for this import "issue", based on a similar trick I found in this Group Module issue (ie comment #6 which is similar and comment #12 which describes the work around), which is to delete the Rules Action that may be causing the import problem, and then after the import succeeded, you just add those Rules Actions manually again.
In this specific case, you have to delete these lines from the above rule (in export format) before trying to import the remaining part of it (both are located within the "Loop" Rules Action, they start with data_set
):
{ "data_set" : { "data" : [ "new-term-exist" ], "value" : "1" } },
{ "data_set" : { "data" : [ "node:field-c" ], "value" : [ "current-term" ] } },
After you removed those 2 lines, retry your Rules import (in my case it works then). Next you need to manually (using the Rules UI) edit that rule and add these 2 Rules Actions right before the "Show a message on the site" again (using "Add Action"-link, locate to the right of "If: Text comparison"). These actions look like so:
- "Set a data value", with Data Selector =
new-term-exist
and Data Value = True
. Next "move" that action before the "Show a message on the site", and "Save changes".
- "Set a data value", with Data Selector =
node:field-c
and Data Value = current-term
. Next "move" that action before the "Show a message on the site", and "Save changes".
To check if you didn't introduce any mistakes with this special editing, export your rule, and compare it to the original rule in my answer here. They should match exactly (if you're using the same machine names, etc).
Scenario for creating a new Article
This Rule does work now also as I would like (expect), in the scenario where you're creating a new Article. However, to make this possible, in the entity_query
I had to "hardcode" the machine name of the vocabulary used for Field C (= vocabulary_c
).
If you use [ "node:field-c:vocabulary" ]
instead, you run into some rules errors (starting with "Unable ...") that you can see if you enable Rules Debugging (in the Rules Log). Here is an example of such Rules Log, containing those unable-messages:
0 ms Rule Combine taxonomy terms in a 3rd taxonomy fires.
4.244 ms Evaluating the action drupal_message. [edit]
5.009 ms Evaluating the action variable_add. [edit]
5.055 ms Added the provided variable combined_term of type text [edit]
5.988 ms Unable to get the data value for the variable or parameter value. Error: Unable to get the data property vocabulary as the parent data structure is not set.
6.421 ms Unable to evaluate action entity_query. [edit]
6.52 ms Evaluating the action variable_add. [edit]
6.559 ms Added the provided variable new_term_exist of type boolean [edit]
7.031 ms Unable to get variable existing_terms, it is not defined.
7.459 ms Unable to evaluate loop. [edit]
9.287 ms The condition data_is evaluated to TRUE [edit]
10.428 ms Evaluating the action entity_create. [edit]
10.637 ms Added the provided variable term_created of type taxonomy_term [edit]
10.734 ms Evaluating the action entity_save. [edit]
10.781 ms Saved term_created of type taxonomy_term.
12.934 ms Evaluating the action data_set. [edit]
15.147 ms Evaluating the action drupal_message. [edit]
15.34 ms Rule Combine taxonomy terms in a 3rd taxonomy has fired.
Demo
Here are some samples (demos) of the kind of messages shown, depending on the used scenario:
If the combination of terms in Field A and Field B already exists as a term in vocabulary_c
(which is the vocabulary used by Field C):
Value of term for Field C has to be set to Term A2Term B3, i.e. the combination of the value of Field A (= Term A2) and the value of Field B (= Term B3). However the page is already rendered BEFORE subsequent Rules Actions will be executed. So to actually see the new value for Field C, an extra page refresh is needed.
Term Term A2Term B3 already existed and will be used to automatically set the value for Field C to this term also ... But only next time this node is viewed (as per the used Rules Event ...)!!! If you're in a hurry to see the new value for Field C, just refresh this page.
If the combination of terms in Field A and Field B does NOT YET exist as a term in vocabulary_c
(which is the vocabulary used by Field C):
Value of term for Field C has to be set to Term A2Term B1, i.e. the combination of the value of Field A (= Term A2) and the value of Field B (= Term B1). However the page is already rendered BEFORE subsequent Rules Actions will be executed. So to actually see the new value for Field C, an extra page refresh is needed.
Term Term A2Term B1 did not yet exist and was created just now. It will be used to automatically set the value for Field C to this term also ... But only next time this node is viewed (as per the used Rules Event ...)!!! If you're in a hurry to see the new value for Field C, just refresh this page.