Node empty fields behave badly when created with Entity Metadata Wrapper Problem

Creating a node with the Entity Metadata Wrapper causes two frustrating results if select fields are left blank. (using #empty_option from the FAPI) .

(My form to create a node is pretty much just a more complicated version of the [create a node in code][1] example).

Display Problem

Typically when a node is created and a select field isn't filled in, the field will not display at all. 

However when using the Entity Metadata Wrapper to create a node, if you then view the node the empty field titles still appear.

Indexing Problem

More frustratingly, if the node is indexed in this state it appears to store a blank string, which will appear as a blank option in a facet and throw the following error on ticking the blank facet.

Notice: Uninitialized string offset: 0 in SearchApiFacetapiTerm-&gt;addFacetFilter() (line 86 of 
/srv/www/mysite/public/sites/all/modules/search_api/contrib/search_api_facetapi/plugins/facetapi/query_type_term.inc).


The Question

Both of these problems, disappear if I edit the node and save it. So something is being called in this process, or creating a node normally that isn't called with the Entity Metadata Wrapper.

Has anyone seen this before? What could be being called in a normal node creation that's not being run with the entity metadata wrapper?

Sample Code

This is an extra code for a node with the select field field_area.

function createanode_form, &amp;$form_state){

   $area_field = field_info_field('field_area');
   $area_values = list_allowed_values($area_field);

   $form['field_area'] = array(
     '#type' =&gt; 'select',
     '#title' =&gt; t('Area'),
     '#options' =&gt; $area_values,
     '#empty_option' =&gt; t("-None-"),
   );

   .....

  $form['submit'] = array(
     '#type' =&gt; 'submit',
     '#value' =&gt; t('Submit'),
     '#weight' =&gt; 70,
  );

}

function createanode_form_submit($form, &amp;$form_state) {

    $values = array(
      'type' =&gt; 'job_advert',
      'uid' =&gt; $user-&gt;uid,
      'status' =&gt; 1,
      'comment' =&gt; 1,
      'promote' =&gt; 0,
    );
    $entity = entity_create('node', $values);

    $ewrapper = entity_metadata_wrapper('node', $entity);

    $ewrapper-&gt;title-&gt;set('Test title');
    $ewrapper-&gt;body-&gt;set('A bunch of text about things that interest me');
    $ewrapper-&gt;field_area-&gt;set($form_state['values']['field_area']);

    $ewrapper-&gt;save(true);
    entity_save('node', $entity);
}
When an #empty_option is defined, it will be added as a label for the empty option in a select list. The value can be defined as well, it is keyed as #empty_value. See the documentation for form_process_select for more details. You will see that for required fields, the default #empty_value is an empty string.

That value is filtered out in _form_validate. I think it is being unset, and then when you call $ewrapper-&gt;field_area-&gt;set() you are actually passing FALSE, NULL or something else that PHP lovingly converts to an empty string.

I think you could solve it like this:

if (!empty($form_state['values']['field_area'])) {
  $ewrapper-&gt;field_area-&gt;set($form_state['values']['field_area']);
}


Or if you manually set the #empty_value to '_EMPTY': 

if ($form_state['values']['field_area'] !== '_EMPTY') {
  $ewrapper-&gt;field_area-&gt;set($form_state['values']['field_area']);
}