Drupal - How do you clear a field value with entity_metadata_wrapper()?
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.
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.)
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');