0

In Drupal 7, I have two content types:

  1. Parent Events with tags (like "outdoors" or "popular" or "suitable for kids"). "Tags" are taxonomy terms in a vocabulary, connected via an Entity Reference field field_parent_tags (using the Checkboxes widget).

  2. Child Events with the same tags & reference in field_child_tags. Each Child also has a mandatory Entity Reference to a Parent Event, field_parent_event (using the Autocomplete widget).

I'm trying to have new Child Events automatically inherit their parent's tags when they're first created. For example:

  • There is a Parent Event called "Weddings", which has the tags "suitable for kids" and "popular".
  • When I make a new Child Event called "Joe & Sarah's Beach Wedding", I'd like it to automatically inherit the tags "suitable for kids" and "popular". Then I'd add the "outdoors" tag to this specific child event, which would apply only to this child and not the parent.
  • (Another child event might be someone else's wedding, and not suitable for kids, so I'd uncheck that in the child event).

How would I go about doing that? So far, I think I need to implement some variation of hook_form_alter(), possibly overriding its #ajax call so that as soon as the child event's form_parent_event is filled out, its field_child_tags will copy the values from field_parent_tags.

Am I going down the right path? I can't figure out where to put that code, if so. I tried overriding /node/add/childevent using a page--node--add-child-event.tpl.php file, but it seems to have no effect. A little lost :(

Any help would be very much appreciated. Thank you!

1 Answer 1

0

I ended up using the Rules module for this. It couldn't do it in AJAX when the parent field was specified, but it could make this happen as soon as the child was first saved, and only if its taxonomies were left blank.

The main Rule The main rule fires a Ruleset component when a child event is saved, and if the parent event is specified. This Ruleset has two parameters: 1) the parent node and 2) the child node.

enter image description here

Next, the Ruleset component just fires two child rules one after another, one for each set of taxonomy terms:

Ruleset

Finally, each individual child rule checks to see if the child event left its taxonomy blank, and if so, sets it to the parent's. I needed one child rule per taxonomy, so that the empty conditions could be checked individually.

child rule

That's it! New child events, upon saving, would get the parent tags if it had none of its own.

The code for the rule:

{ "rules_inherit_parent_event_taxonomies" : {
    "LABEL" : "Inherit Parent Event Taxonomies",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--event" : { "bundle" : "event" } },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "event" : "event" } } } },
      { "NOT data_is_empty" : { "data" : [ "node:field-program" ] } }
    ],
    "DO" : [
      { "component_rules_set_child_taxonomies" : { "parent_node" : [ "node:field-program" ], "child_node" : [ "node" ] } }
    ]
  }
}

And the ruleset component:

{ "rules_set_child_taxonomies" : {
    "LABEL" : "Set Child Taxonomies",
    "PLUGIN" : "rule set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "parent_node" : { "label" : "Parent Node", "type" : "node" },
      "child_node" : { "label" : "Child Node", "type" : "node" }
    },
    "RULES" : [
      { "RULE" : {
          "IF" : [
            { "node_is_of_type" : {
                "node" : [ "parent-node" ],
                "type" : { "value" : { "program" : "program" } }
              }
            },
            { "node_is_of_type" : {
                "node" : [ "child-node" ],
                "type" : { "value" : { "event" : "event" } }
              }
            },
            { "data_is_empty" : { "data" : [ "child-node:field-topic" ] } }
          ],
          "DO" : [
            { "data_set" : {
                "data" : [ "child-node:field-topic" ],
                "value" : [ "parent-node:field-topic" ]
              }
            }
          ],
          "LABEL" : "Inherit parent\u0027s topics if child has none"
        }
      },
      { "RULE" : {
          "IF" : [
            { "node_is_of_type" : {
                "node" : [ "parent-node" ],
                "type" : { "value" : { "program" : "program" } }
              }
            },
            { "node_is_of_type" : {
                "node" : [ "child-node" ],
                "type" : { "value" : { "event" : "event" } }
              }
            },
            { "data_is_empty" : { "data" : [ "child-node:field-audience" ] } }
          ],
          "DO" : [
            { "data_set" : {
                "data" : [ "child-node:field-audience" ],
                "value" : [ "parent-node:field-audience" ]
              }
            }
          ],
          "LABEL" : "Inherit parent\u0027s audiences if child has none"
        }
      }
    ]
  }
}

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.