1

I have a rule in drupal that populates an entity reference in a parent node with a child node.

Then I created a new rule that is applied "before content is saved" that will remove the child node from the old parent node if one wants to move it to a different parent node.

And it works as expected UNLESS one chooses -none- in the select list. How do I make it remove the child reference when none is selected. I tried Data value is empty and that doesn't work.

Here is an export of my current rule:

{ "rules_remove_job_from_client" : {
"LABEL" : "Remove Job From Client",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_presave--job" : { "bundle" : "job" } },
"IF" : [
  { "NOT data_is" : {
      "data" : [ "node-unchanged:field-client" ],
      "value" : [ "node:field-client" ]
    }
  },
  { "node_is_of_type" : {
      "node" : [ "node:field-client" ],
      "type" : { "value" : { "clients" : "clients" } }
    }
  }
],
"DO" : [
  { "list_remove" : {
      "list" : [ "node-unchanged:field-client:field-all-jobs" ],
      "item" : [ "node-unchanged" ]
    }
  },
  { "drupal_message" : { "message" : "Is this firing" } }
]
}
}
1

Make sure to add a Rules condition Entity has field (related to your field field-client as shown in your first Rules Condition). For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in it.

If that doesn't help, you may want to do some Rules debugging, as explained in the answer to "How can I display the value of a variable inside a condition?".

  • Thank you @pierre-vriens for pointing me again in the right direction! I think I got it. – Dab0416 Apr 30 '18 at 18:32
  • Ha! I get your point. :) Yes you did provide the answer to the question. – Dab0416 May 1 '18 at 3:04
1

I split it into 2 rules, they may be able to be combined but I haven't tested it yet.

The first rule is as above and takes care of switching the job from one client to another. The second rule has a condition that checks if the unsaved field-client had a reference to a job and if the new saved field is now empty.

If this is true then it will remove the old job from the client job reference list.

Here is the export:

{ "rules_remove_job_from_client_if_empty" : {
"LABEL" : "Remove Job From Client If Empty",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_presave--job" : { "bundle" : "job" } },
"IF" : [
  { "entity_has_field" : {
      "entity" : [ "node-unchanged:field-client" ],
      "field" : "field_all_jobs"
    }
  },
  { "data_is_empty" : { "data" : [ "node:field-client" ] } }
],
"DO" : [
  { "drupal_message" : { "message" : "Data Value is empty!" } },
  { "list_remove" : {
      "list" : [ "node-unchanged:field-client:field-all-jobs" ],
      "item" : [ "node-unchanged" ]
    }
  }
]
}
}
  • So "Entity has field" (as in my answer) seems to be the proper Condition to check, right? With that, all that's needed is to verify if it is empty also. – Pierre.Vriens Apr 30 '18 at 19:13

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.