1

I have a content type called Items.

Within the items I have 3 unlimited term reference fields.

Location (field_location_pi)
Phase (field_phases_pi)
Structure (field_structure_type_pi)

I am creating a drill down system using reference option limit.

So in a second content type, you would select item, then the location taxonomy field would be limited by those designated by that item, then the phases would be limited by those in the locations and the structure by those in the phases.

For this to work I need to have a reference field in each taxonomy that refers to taxonomy that limits it.

For Example

An Item is called Pour Cement. This item has 3 possible locations terms tagged: North, South, West. It also has 2 Phases terms tagged: First, Second. Finally it also has two structures terms tagged: sidewalk, wall.

So the Location Taxonomy Terms have an entity reference field (field_pay_items_loc) that references all Items tagged with the terms.

Then the Phase Taxonomy Terms have a term reference field (field_location_ref) that references all the Locations they have in common in the Items.

Then the Structure Taxonomy Terms have a term reference field that references all the phases they have in common in the items.

To further extrapolate on the aforementioned example:

Each Location Term: North & South & West have one item referenced: Pour Cement

Each Phase Term: First & Second have 3 locations referenced: North, South, West

Each Structure Term: Wall & Sidewalk have 2 Phases referenced: First, Second

Right now I have to set this manually, but I would like to create a rule that automatically selects the correct item(s) in correct reference fields when a pay item is saved or created.

I can do this when the reference fields are limited to 1 by using the following rule, This rule adds the item to the phase and the phase to the location. I know I need to change the action to adding to a list and it needs to happen within a loop but, for the life of me, I can not get it working when the reference fields are set to unlimited.

I've been slogging my way through the components (I watched this video: https://youtu.be/Bol6kNwVenA on creating multiple back links for a guide) and I can not get it to work with taxonomies. Any advice would be greatly appreciated.

{ "rules_add_pi_to_phase" : {
    "LABEL" : "Add item to Phase",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : {
      "node_update--item" : { "bundle" : "item" },
      "node_insert--item" : { "bundle" : "item" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node:field-phases-pi" ], "field" : "field_pay_items_loc" } }
    ],
    "DO" : [
      { "data_set" : {
          "data" : [ "node:field-phases-pi:field-items-loc" ],
          "value" : [ "node" ]
        }
      },
      { "data_set" : {
          "data" : [ "node:field-phases-pi:field-location-ref" ],
          "value" : [ "node:field-location-pi" ]
        }
      }
    ]
  }
}

Here is the rule I have so far to loop through all the phases in the Item. Without the component.

{ "rules_add_pi_to_phase" : {
    "LABEL" : "Add PI to Phase",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : {
      "node_update--pay_item" : { "bundle" : "pay_item" },
      "node_insert--pay_item" : { "bundle" : "pay_item" }
    },
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "pay_item" : "pay_item" } }
        }
      }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-phases-pi" ] },
          "ITEM" : { "phase_item" : "Current Phase" },
          "DO" : []
        }
      }
    ]
  }
}

I just realized that I wasn't thinking about this the right way. I need to get all the phases terms referenced (field_phases_pi) in the item AND all the structure types terms referenced (field_structure_type_pi) in the item too. Then I need to add all the phases terms to the referenced field (field_phases_ref) to all the structure types terms referenced. So that would be multiple loops, I guess.

Here is my attempt at the component but I know it's not right because when I try to add it to the rule it doesn't allow me to pick a data selection and it wants Taxonomy term identifiers that I don't know what to do with...

{ "rules_add_phase_to_structure_type" : {
    "LABEL" : "Add Phase To Structure Type",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "phase_term" : { "label" : "Phase To Add", "type" : "list\u003Ctaxonomy_term\u003E" },
      "structure_term" : {
        "label" : "Structure Type To Add To",
        "type" : "list\u003Ctaxonomy_term\u003E"
      }
    },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "structure-term" ] },
          "ITEM" : { "structure_item" : "Current Structure item" },
          "DO" : [
            { "entity_fetch" : {
                "USING" : { "type" : "taxonomy_term", "id" : [ "structure-item:tid" ] },
                "PROVIDE" : { "entity_fetched" : { "fetched_structure" : "Fetched structure" } }
              }
            },
            { "list_add" : {
                "list" : [ "phase-term" ],
                "item" : [ "structure-item" ],
                "unique" : "1"
              }
            }
          ]
        }
      }
    ]
  }
}
1

Looks like you could just use the "Add to list" Rules Action.

For multi value fields you should not use "Set a data value" (which seems to be what you've tried, a common mistake when using Rules for such fields).

Refer to the answer to "How to set multiple values to an unlimited Term Refence field with Rules" for "some" more details ...

  • I've tried add to list in many different ways, I will check your link... – Dab0416 Feb 3 '18 at 19:44
  • @Dab0416 if you could EDIT your question by mentioning the relevant field names (as shown in your exported rule), i might be able to further improve (refine) my answer to indicate how to further improve/rework your rule. – Pierre.Vriens Feb 3 '18 at 19:56
  • Thank you - I will do that, I'm trying to implement the instructions from your other post, but I'm getting lost in what references what and what to add too, one thing I did notice is that I wasn't using the "list of taxonomy term items" in the components settings. – Dab0416 Feb 3 '18 at 20:02
  • 1
    After trying to digest your edited question, I'm even more confused about what exactly you are trying to get to work. I suggest you edit your question (near the top of it), to include some example(s) about your node(s) and various taxonomies and terms. Let me know (via an extra comment) when you performed such edit. PS: I keep thinkiing that my answer is part of the solution to your question, though probably not "complete" (yet). – Pierre.Vriens Feb 4 '18 at 9:19
  • 1
    @Dab0416 I've been digesting your edited version of your question like a dozen of times again. I start to undertstand it a bit better (what you're loking for), I think, but still not enough. It seems to me you should try to cut down your question in multiple questons. So therefor, how about you first post a new question, but only limited to 1 taxonomy (ie "location", not (yet) "phase" or "structure"). .After we figured out that one, we "return" to this one. What do you think? – Pierre.Vriens Feb 5 '18 at 21:25
1

This is to Add the location reference term to the correct phase terms.

First: I changed my condition to "entity has field", "node", "field_phases_pi"

Then I looped through the phases in the pay item

Then I add the pay item to the Pay Item entity reference field in each of the phases. (This was more of a test as I already knew how to get this done)

Then I added a nested loop to get all the locations in the pay item.

And I got stuck here so I set a message to confirm the values I was getting for each loop.

Once I got the loops to return the right value, it was just a matter of targeting the correct reference field in each phase (phase-item:field-location-ref) and adding the loc-ref-item from the loop.

Then I created the same rule (except targeting the different terms) for Adding Phase to Structure Type. So I'll have 3 seperate rules, there is probably a way to combine them, but I'm psyched to get this far!

Here is the export:

{ "rules_location_to_phase" : {
"LABEL" : "Add Location To Phase",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : {
  "node_update--pay_item" : { "bundle" : "pay_item" },
  "node_insert--pay_item" : { "bundle" : "pay_item" }
},
"IF" : [
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_phases_pi" } }
],
"DO" : [
  { "LOOP" : {
      "USING" : { "list" : [ "node:field-phases-pi" ] },
      "ITEM" : { "phase_item" : "Phase item" },
      "DO" : [
        { "list_add" : {
            "list" : [ "phase-item:field-pay-items-loc" ],
            "item" : [ "node" ],
            "unique" : "1"
          }
        },
        { "LOOP" : {
            "USING" : { "list" : [ "node:field-location-pi" ] },
            "ITEM" : { "loc_ref_item" : "Location Ref item" },
            "DO" : [
              { "drupal_message" : { "message" : "Loc-ref-item:name value:[loc-ref-item:name]\r\n\r\nPhase Item Name Value: [phase-item:name]" } },
              { "list_add" : {
                  "list" : [ "phase-item:field-location-ref" ],
                  "item" : [ "loc-ref-item" ],
                  "unique" : "1"
                }
              }
            ]
          }
        }
      ]
    }
   }
  ]
 }
}
  • Formidable! So in the end you solved it in a way I had started to think also (I wasn't sure yet, which is why I kept asking). The only thing left is to mark your own answer as accepted, no? – Pierre.Vriens Feb 6 '18 at 19:49

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.