1

I have an entity that I import some data into, then create and save via entityMetadataWrapper->save().

When I looked on the form, it appeared that a default value that I set up for a field that I'm not importing was being saved, but later when I try to use that field, it's null in the code. If I go to my entity and save it, then the default value is saved and all is right with the world. Is there something special I need to do before save() to tell it to use my default value and actually save it?

1

The default value is used for the widget form only, it won't be automatically set if you use node_save or a metadata wrapper. You see it in the form when you edit the newly created entity because there's no value for that field, and Drupal dutifully fills in the default value. It doesn't update the value of that field on the entity, just populates the form input.

You'll need to populate it manually, but you can get the right value with the field API:

$instance = field_info_instance('node', 'field_foo', 'article');
$default_value = $instance['default_value'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.