How do you clear a field value with entity_metadata_wrapper()?

by joachim   Last Updated June 14, 2015 01:03 AM

Suppose I have an entity with a field field_foo on it and I want to clear that field's value.

What should I pass to $wrapper->set()?

I have tried NULL and array() and both produce an error message.

Tags : entities


Answers 4


The problem is that you must set an empty value that is compatible with the data type of your field. If you do not, you will get an exception raised. Passing NULL or array() when a string is expected will therefore raise an error.

Another thing to keep in mind is that the data you pass will also depend on whether your field is a single value, a multi-value field, or a field with multiple properties.

If your field is a single value (and thus the wrapper for the field is an instance of EntityValueWrapper) then you should assign it an empty value compatible with the data type in question. For instance the two following methods are equivalent:

$wrapper->title = '';
$wrapper->title->set('');

However the following three examples will raise an exception, because the data types are not compatible with the title field:

$wrapper->title->set();
$wrapper->title->set(NULL);
$wrapper->title->set(array());

If your field is a field with multiple properties (for instance a formatted text field, which defined both a value and format property) and thus an instance of EntityStructureWrapper, then array() or NULL will be the correct empty value. So you can do the following:

$wrapper->field_formatted_text = array();
$wrapper->field_formatted_text = NULL;

But in that case, passing an empty string would have raised an error. Note that you could have chosen to make the value property empty instead, in which case a string is the correct data type:

$wrapper->field_formatted_text->value = '';

Finally, if your field is a multi-value field (and thus your wrapper is an instance of EntityListWrapper) then array or NULL are the correct empty values, and the following three lines are equivalent:

$wrapper->field_example_multiple->set();
$wrapper->field_example_multiple = array();
$wrapper->field_example_multiple = NULL;

Note: Calling the clear method on the wrappers is not equivalent to setting the field to an empty value. When the field is set to an empty value, it then calls EntityMetadataWrapper::updateParent on the field's parent wrapper. This ensures amongst other things that the setter callback defined by hook_entity_property_info is called. Calling clear does not do that.

Alice Heaton
Alice Heaton
March 28, 2013 11:10 AM

Further to other answers and comments, if the field is multiple and required, as previously noted you cannot use $wrapper->field_example_multiple->set(), $wrapper->field_example_multiple->set(NULL) nor even $wrapper->field_example_multiple->set(array()), but instead you can use the following if you want to clear the field of all its values: $wrapper->field_example_multiple->set(array(NULL)) In fact, this works whether or not the multiple-value field is set to 'required', and so I'd recommend always using this, to ensure that your code is robust.

(Of course, if the field is 'required' then you perhaps shouldn't be completely clearing it anyway, but your code might be doing this as a preliminary step to deleting the whole entity or something similar, so there are times when it might just be legitimate.)

Martin Q
Martin Q
August 04, 2015 14:58 PM

It appears that the complexities listed in the other comments are only relevant to a required field. If the field is not required, then this should be pretty simple:

$wrapper->field_foo = NULL;

You can use the wrapper to check the properties of the field:

$properties = $wrapper->getPropertyInfo();
$field_required = !empty($properties['field_foo']['required']);

Depending on the context, you can also just get the properties of the one field using:

$wrapper->getPropertyInfo('field_foo');
Giles B
Giles B
October 17, 2015 19:10 PM

Another solution for this problem could be EntityMetadataWrapper::clear

$entity_wrapper->field->clear()

Mouneer
Mouneer
January 14, 2016 14:24 PM

Related Questions




How to create a custom template for entities?

Updated April 07, 2015 03:03 AM

Drupal 8 Custom Entities from a UI?

Updated July 14, 2015 15:03 PM

Content type subtypes with no bundles or clientside

Updated April 10, 2015 19:03 PM