Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I need an action which sets a user reference field to be empty. I've tried to do so with the "set a data value" action but I can
I'm trying to create a rule which will set a user reference field on a node to empty (so no user is referenced); and I would very definitely not like to delete the reference user entity.
Comment | File | Size | Author |
---|---|---|---|
#7 | entity_reference.patch | 2.57 KB | fago |
#6 | 1178052-entity-set-null.patch | 2.43 KB | klausi |
#2 | 1178052-entity-set-null.patch | 718 bytes | klausi |
#2 | 1178052-rules-set-null.patch | 410 bytes | klausi |
Comments
Comment #1
klausiAt the moment the "set a data value" action requires a value in the UI form, so we should probably set that value field to optional. Fago said he had already improved the form elements to return NULL if they are empty, so this should be easy. Will work on a patch.
Comment #2
klausiHere are two patches, one for Rules and one for Entity API. There was a problem with NULL entity values. Please test.
Comment #3
zabelc CreditAttribution: zabelc commentedThanks for the patch klausi; however, there seems to be a problem with the entity api patch. When I applied it, other "Set a Data Value" actions stopped working, and I received the following (possibly incidental notice) while editing a "User has role(s)" condition.
One other thought: would it make more sense to provide a two options: don't put a string to set the value to "" (empty string), or provide a constant "" or "" to set the value to null.
Comment #4
zabelc CreditAttribution: zabelc commentedFWIW, I accomplished what I needed to do by adding a custom PHP action which does something along the lines of the following:
Of course, in order for that to cause rules to update the Node in the DB, I had to create a "Set a Data Value" operation. It's a kludge, but it might give someone a better idea of how to proceed.
Comment #5
fagothanks, I've committed the rules fix.
@entity-API:
empty properties should be NULL, thus I think data should be NULL - as it is by default.
Still, we should only update the parent wrapper if something changed. Also we should cover this in the tests, e.g. by unsetting a node tag. Additionally, we should fix validate() to not allow NULL values in case a property is required.
Comment #6
klausiNew patch, also has a test. We need to update the parent wrapper on NULL values because the child might not have been loaded. So the ids are FALSE in any case and we would not detect a change.
The method validate() has not yet been fixed.
I verified the patch with a term reference field and a user reference field, both using Rules.
Comment #7
fagoThanks, I're-worked the check a bit so is simpler (but the same) + improved the comment to explain the reasoning, not what it does.
As the test-case is still fine, I've committed the attached patch + created #1203808: troubles with unsetting properties as follow-up for remaining issues.
Comment #9
kingswoodute CreditAttribution: kingswoodute commentedHi,
Did you get this to work? I need to use a rule to set an entity reference field to Null and I can't find a way to do it.
Any help would be hugely appreciated.