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->addFacetFilter() (line 86 of 

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, &$form_state){

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

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


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


function createanode_form_submit($form, &$form_state) {

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

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

    $ewrapper->title->set('Test title');
    $ewrapper->body->set('A bunch of text about things that interest me');

    entity_save('node', $entity);
  • I could not reproduce the error described. Here is a sample script to use with drush scr gist.github.com/8825551 Commented Feb 5, 2014 at 15:05
  • Hmm hold on let me provide some more sample code. It might be the error is limited to select fields. Commented Feb 5, 2014 at 15:32
  • @barraponto Ok sorry about that. Sample code added to make it more clear. Commented Feb 5, 2014 at 15:40

1 Answer 1


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->field_area->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'])) {

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

if ($form_state['values']['field_area'] !== '_EMPTY') {
  • If either solution works for you, please edit my answer :D Commented Feb 5, 2014 at 15:52
  • Awesome works great. Shouldn't the second one be != though? (The problem also existed for text fields and solved it the same way.) Also is this an explanation? Default empty strings are changed to NULL by _form_validate this is then changed to an empty string by the Entity Wrapper. In the traditional node form the data is submitted by another method and NULL values are submitted directly, thus meaning the empty field never shows? Commented Feb 5, 2014 at 17:29
  • Yes, it should be !==, I'll fix it. And I think turning FALSE or NULL into an empty string is expected behavior coming from PHP. Commented Feb 5, 2014 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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