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.

Files: 
CommentFileSizeAuthor
#7 entity_reference.patch2.57 KBfago
#6 1178052-entity-set-null.patch2.43 KBklausi
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-entity-set-null_0.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]
#2 1178052-entity-set-null.patch718 bytesklausi
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-entity-set-null.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]
#2 1178052-rules-set-null.patch410 bytesklausi
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-rules-set-null.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]

Comments

Category:support» bug

At 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.

Status:Active» Needs review
StatusFileSize
new410 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-rules-set-null.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]
new718 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-entity-set-null.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]

Here are two patches, one for Rules and one for Entity API. There was a problem with NULL entity values. Please test.

Thanks 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.

  • Notice: Trying to get property of non-object in entity_metadata_user_access() (line 539 of /home/patcms/public_html/sites/all/modules/entity/modules/callbacks.inc).
  • Notice: Trying to get property of non-object in entity_metadata_user_access() (line 539 of /home/patcms/public_html/sites/all/modules/entity/modules/callbacks.inc).

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.

Status:Needs review» Needs work

FWIW, I accomplished what I needed to do by adding a custom PHP action which does something along the lines of the following:

<?php
$current_node
->field_my_field = array();
?>

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.

Title:How to "Set a data value" to null/empty?Unsetting entity relationships does not work
Project:Rules» Entity API
Version:7.x-2.x-dev» 7.x-1.x-dev
Component:Rules Engine» Entity property wrapper

thanks, I've committed the rules fix.

@entity-API:

+    elseif (empty($data)){
+      $this->id = FALSE;
+      $this->data = FALSE;
+    }

empty properties should be NULL, thus I think data should be NULL - as it is by default.

+    elseif ($this->id === FALSE) {
+      $this->updateParent(NULL);
+    }

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.

Status:Needs work» Needs review
StatusFileSize
new2.43 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1178052-entity-set-null_0.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.
[ View ]

New 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.

Status:Needs review» Fixed
StatusFileSize
new2.57 KB

Thanks, 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.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Hi,

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.