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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communitySuppose 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.
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.
array()
or NULL
can fail, because the field can't be empty. This is different from normal $node
field assignment, where you can programmatically save an empty required field (it just won't save via Drupal's own UI). In this case, a workaround is array(N)
, where N is the ID of a non-existent yet referenced entity. Note that it will save with that ID, so your data is arguably "broken" in a relational sense; but it shouldn't affect the theme layer if you're doing all the right things there (e.g. using Display Suite or Panels).
– J-P
Mar 9 '15 at 10:30
$w->field_allowed_regions->set(array(null));
is the only option that worked for my multi-value taxonomy reference field.
– Incredible
Nov 21 '16 at 14:38
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.)
NULL
value.
– Alex Skrypnyk
Mar 1 '16 at 23:35
$product_display->field_product = array($product_id);
– Interdruper
Mar 27 '16 at 19:42
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');
Another solution for this problem could be EntityMetadataWrapper::clear
$entity_wrapper->field->clear()
EntityStructureWrapper::clear()
orEntityValueWrapper::clear()
methods perhaps? – Clive♦ Mar 27 '13 at 14:52::clear
directly is not equivalent to setting the field to an empty value, as it does not callupdateParent
in the way callingset
with an empty value does. Amongst other things,updateParent
ensures that thesetter callback
defined in the property info (see drupalcontrib.org/api/drupal/…) is called. – Alice Heaton Mar 28 '13 at 12:29