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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

klausi’s picture

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.

klausi’s picture

Status: Active » Needs review
FileSize
410 bytes
718 bytes

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

zabelc’s picture

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.

zabelc’s picture

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:

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

fago’s picture

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.

klausi’s picture

Status: Needs work » Needs review
FileSize
2.43 KB

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.

fago’s picture

Status: Needs review » Fixed
FileSize
2.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.

kingswoodute’s picture

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.