I just noticed when you create a new node, it will still save a record in the field's data table if any addressfield country defaults are set to anything other than "-None-"

So even if you set the default country to 'US' and then create a new node and change the country from 'US' to -None-, it will still save a record in the database showing US. If I then edit that node and set the country to -None-, it will correctly remove the row from the database. Very strange...

If there is a row in the database with just the Country set, it will cause Panels to output a label with a blank address on the node view.

If the addressfield default country is set to -None- and you create a new node, then everything works correctly. It doesn't save a record in the database.

Let me know if you need any additional information.

Comments

bojanz’s picture

Confirmed.

Oddly enough, if I edit the node, the default country is shown instead of "None", but if I set it to "None" this time, the save is correct, and the row disappears from the database. So it appears this bug is limited to the add form.

EDIT: This is coming from core:

/**
 * Default field 'insert' operation.
 *
 * Insert default value if no $entity->$field_name entry was provided.
 * This can happen with programmatic saves, or on form-based creation where
 * the current user doesn't have 'edit' permission for the field.
 */
function field_default_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  // _field_invoke() populates $items with an empty array if the $entity has no
  // entry for the field, so we check on the $entity itself.
  // We also check that the current field translation is actually defined before
  // assigning it a default value. This way we ensure that only the intended
  // languages get a default value. Otherwise we could have default values for
  // not yet open languages.
  if (empty($entity) || !property_exists($entity, $field['field_name']) ||
    (isset($entity->{$field['field_name']}[$langcode]) && count($entity->{$field['field_name']}[$langcode]) == 0)) {
    $items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
  }
}

However, according to the docblock, it should not trigger for users who intentionally chose the empty value.
Still, I can confirm it fires.

I'm not sure how to proceed, will need to sleep on it.

bojanz’s picture

Confirmed to be a core bug: #1253820: It's impossible to submit no value for a field that has a default value.

The issue lists a workaround which I will try to implement tomorrow.

gmclelland’s picture

Wow! your fast. Thanks for confirming, I'm glad it wasn't just me.

bojanz’s picture

I have confirmed that there is no fix for this, no workaround works for all cases.

My current thinking is to remove the "default values" fieldset completely, replace it with a "Default country" dropdown, and ask people to provide other defaults via plugins.
In progress...

bojanz’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.