Skip to main content
added 100 characters in body
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules (refer to the answer to "https://drupal.stackexchange.com/q/245049/39516" for more details on that), the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules (refer to the answer to "https://drupal.stackexchange.com/q/245049/39516" for more details on that), the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

added 551 characters in body
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

Even though it's not an obvious rule to digest (I think), it mostly does 2these things:

  • Perform an entity_query to create a list of existing terms (stored in existing_terms), for the vocabulary used in Field A (as per the node:field-a:vocabulary in that entity_query). So the assumption is that Field A and Field C use the same vocabulary (there are no details about that in the question). If that's not the case, just replace that vocabulary with whatever fits, or maybe just hardcode it.
  • Calculate a boolean variable with machine name new_term_exist, which verifies if the term to be saved in Field C already exists.
  • Set the value for field C to an existing term (if it already exist).
  • Create the term for field C on the fly (if it does not yet exist) and then use that to set the value for field C.

NoteNote: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

PS: Q: How do you eat an elephant? A: 1 bite at a time ... (though there is only1only 1 bite left of it now ... I think).

Even though it's not an obvious rule to digest (I think), it mostly does 2 things:

  • Set the value for field C to an existing term (if it already exist).
  • Create the term for field C on the fly (if it does not yet exist) and then use that to set the value for field C

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

PS: Q: How do you eat an elephant? A: 1 bite at a time ... (though there is only1 bite left of it now ... I think).

Even though it's not an obvious rule to digest (I think), it mostly does these things:

  • Perform an entity_query to create a list of existing terms (stored in existing_terms), for the vocabulary used in Field A (as per the node:field-a:vocabulary in that entity_query). So the assumption is that Field A and Field C use the same vocabulary (there are no details about that in the question). If that's not the case, just replace that vocabulary with whatever fits, or maybe just hardcode it.
  • Calculate a boolean variable with machine name new_term_exist, which verifies if the term to be saved in Field C already exists.
  • Set the value for field C to an existing term (if it already exist).
  • Create the term for field C on the fly (if it does not yet exist) and then use that to set the value for field C.

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

PS: Q: How do you eat an elephant? A: 1 bite at a time ... (though there is only 1 bite left of it now).

added 1248 characters in body
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

Easy enough, no? ... All you need to know is how to actually perform a check in Rules to verify "If that term is already contained in the list of terms of your taxonomy". That's what you can do with "List contains item", with list = the terms that exist before the rule starts executing, and item the term [node:field-a][node:field-b].

Part 2 - Partial solution

Below is a partial solution for what is above (it does need some extra "work" ...

Updated Rule

Part 2 - Solution using Conditional Rules

HaveBelow is a look at this rule (which now performs an appropriatepossible solution using Conditional Rules Component). The improved rule looks like so:

{ "rules_improved_combination_of_taxonomy_terms""rules_combine_taxonomy_terms_without_creating_duplicates" : {
    "LABEL" : "Improved"Combine combinationtaxonomy ofterms taxonomywithout terms"creating duplicates",
    "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 shouldwill be set to: \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E"strong\u003E." } },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[node:field-a][node:field-b]" },
          "PROVIDE" : { "variable_added" : { "variable_added""combined_term" : "Added"Combined variable"Term" } }
        }
      },
      { "entity_query" : {
          "USING" : {
            "type" : "taxonomy_term",
            "property" : "vocabulary",
            "value" : [ "node:field-a:vocabulary" ]
          },
          "PROVIDE" : { "entity_fetched" : { "existing_terms" : "Existing Terms" } }
        }
      },
      { "component_rules_add_term_if_not_yet_created""variable_add" : {
          "term_parm""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" ] } },
          "terms_list_parm"          { "drupal_message" : [{ "existing"message" : "Term \u003Cstrong\u003E[node:field-terms"a][node:field-b]\u003C\/strong\u003E already exists (no need to create it now)." } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : { "data" : [ "new-term-exist" ], "value" : "0" } },
            "DO" : [
              { "entity_create" : {
                  "USING" : {
                    "type" : "taxonomy_term",
                    "param_name" : "[variable"[combined-addedterm:value]",
                    "param_vocabulary" : "tags"
                  },
                  "PROVIDE" : { "entity_created" : { "entity_created""term_created" : "Created entity"term" } }
                }
              },
              { "entity_save" : { "data" : [ "entity"term-created" ], "immediate" : "1" } },
              { "data_set" : { "data" : [ "node:field-c" ], "value" : [ "entity"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." } }
            ]
          }
        ]
      },
      { "entity_save" : { "data" : [ "node:field-c" ], "immediate" : "1" } },
      { "drupal_message" : { "message" : "\u003Cstrong\u003ENote\u003C\/strong\u003E: After updating field A or B, a page refresh is needed to actually SEE a value of field C = \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E." } }
    ]
  }
}

New Rules Component

Even though it's not an obvious rule to digest (I think), it mostly does 2 things:

This is the related Rules Component:

  • Set the value for field C to an existing term (if it already exist).
  • Create the term for field C on the fly (if it does not yet exist) and then use that to set the value for field C
{ "rules_add_term_if_not_yet_created" : {
    "LABEL" : "Add term if not yet created",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "term_parm" : { "label" : "Term Parameter", "type" : "taxonomy_term" },
      "terms_list_parm" : {
        "label" : "List of Terms Parameter",
        "type" : "list\u003Ctaxonomy_term\u003E"
      }
    },
    "IF" : [
      { "list_contains" : { "list" : [ "terms-list-parm" ], "item" : [ "term-parm" ] } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Term with name [term-parm:name] does exist already.\r\nTodos: (1) move Rules Actions \u0022Create Entity\u0022 and \u0022Save Entity\u0022 to Actions of this component and (2) Get the negation of the Rules Condition here to work if value of Field C is still empty " } }
    ]
  }
}

However, there is 1 minor detail to be aware of, as detailed in the message of the very last Rules Action ... For anybody interested in addressing that (to make that message obsolete), please post an extra answer and feel free to use this rule here to start from.

AllNote: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that remains now is that you run in a tough-to-debug issue when passing values to implement the 2 todos as shownsuch Rules Components, in the "Message" atcases where the endvalue of Field C is still blank (eg when creating a new node).

PS: Q: How do you eat an elephant? A: 1 bite at a time ... (though there is only1 bite left of it now ... I think).

Easy enough, no? ... All you need to know is how to actually perform a check in Rules to verify "If that term is already contained in the list of terms of your taxonomy". That's what you can do with "List contains item", with list = the terms that exist before the rule starts executing, and item the term [node:field-a][node:field-b].

Part 2 - Partial solution

Below is a partial solution for what is above (it does need some extra "work" ...

Updated Rule

Have a look at this rule (which now performs an appropriate Rules Component):

{ "rules_improved_combination_of_taxonomy_terms" : {
    "LABEL" : "Improved combination of taxonomy terms",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_view--article" : { "bundle" : "article" } },
    "DO" : [
      { "drupal_message" : { "message" : "Value of term for field C should be set to: \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E" } },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[node:field-a][node:field-b]" },
          "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
        }
      },
      { "entity_query" : {
          "USING" : {
            "type" : "taxonomy_term",
            "property" : "vocabulary",
            "value" : [ "node:field-a:vocabulary" ]
          },
          "PROVIDE" : { "entity_fetched" : { "existing_terms" : "Existing Terms" } }
        }
      },
      { "component_rules_add_term_if_not_yet_created" : {
          "term_parm" : [ "node:field-c" ],
          "terms_list_parm" : [ "existing-terms" ]
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "taxonomy_term",
            "param_name" : "[variable-added:value]",
            "param_vocabulary" : "tags"
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : "1" } },
      { "data_set" : { "data" : [ "node:field-c" ], "value" : [ "entity-created" ] } },
      { "entity_save" : { "data" : [ "node:field-c" ], "immediate" : "1" } }
    ]
  }
}

New Rules Component

This is the related Rules Component:

{ "rules_add_term_if_not_yet_created" : {
    "LABEL" : "Add term if not yet created",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "term_parm" : { "label" : "Term Parameter", "type" : "taxonomy_term" },
      "terms_list_parm" : {
        "label" : "List of Terms Parameter",
        "type" : "list\u003Ctaxonomy_term\u003E"
      }
    },
    "IF" : [
      { "list_contains" : { "list" : [ "terms-list-parm" ], "item" : [ "term-parm" ] } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Term with name [term-parm:name] does exist already.\r\nTodos: (1) move Rules Actions \u0022Create Entity\u0022 and \u0022Save Entity\u0022 to Actions of this component and (2) Get the negation of the Rules Condition here to work if value of Field C is still empty " } }
    ]
  }
}

All that remains now is to implement the 2 todos as shown in the "Message" at the end.

PS: Q: How do you eat an elephant? A: 1 bite at a time ...

Easy enough, no? ... All you need to know is how to actually perform a check in Rules to verify "If that term is already contained in the list of terms of your taxonomy".

Part 2 - Solution using Conditional Rules

Below is a possible solution using Conditional Rules. The improved rule looks like so:

{ "rules_combine_taxonomy_terms_without_creating_duplicates" : {
    "LABEL" : "Combine taxonomy terms without creating duplicates",
    "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 will be set to: \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E." } },
      { "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" : [ "node:field-a:vocabulary" ]
          },
          "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 exists (no need to create it now)." } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : { "data" : [ "new-term-exist" ], "value" : "0" } },
            "DO" : [
              { "entity_create" : {
                  "USING" : {
                    "type" : "taxonomy_term",
                    "param_name" : "[combined-term:value]",
                    "param_vocabulary" : "tags"
                  },
                  "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." } }
            ]
          }
        ]
      },
      { "entity_save" : { "data" : [ "node:field-c" ], "immediate" : "1" } },
      { "drupal_message" : { "message" : "\u003Cstrong\u003ENote\u003C\/strong\u003E: After updating field A or B, a page refresh is needed to actually SEE a value of field C = \u003Cstrong\u003E[node:field-a][node:field-b]\u003C\/strong\u003E." } }
    ]
  }
}

Even though it's not an obvious rule to digest (I think), it mostly does 2 things:

  • Set the value for field C to an existing term (if it already exist).
  • Create the term for field C on the fly (if it does not yet exist) and then use that to set the value for field C

However, there is 1 minor detail to be aware of, as detailed in the message of the very last Rules Action ... For anybody interested in addressing that (to make that message obsolete), please post an extra answer and feel free to use this rule here to start from.

Note: even though, in theory, you should also be able to use Rules Components instead of Conditional Rules, the key challenge with that is that you run in a tough-to-debug issue when passing values to such Rules Components, in the cases where the value of Field C is still blank (eg when creating a new node).

PS: Q: How do you eat an elephant? A: 1 bite at a time ... (though there is only1 bite left of it now ... I think).

added 3314 characters in body
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182
Loading
deleted 2 characters in body
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182
Loading
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182
Loading