Hi,

while writing a module to extend user profiles, I came across the problem that list_boolean fields I added programmatically which use the options_onoff type widget, did not save their state when I submitted the form. Checkboxes remained unchecked even though checked before submitting.
After some digging I found out that programmatically added fields via field_create_field() and field_create_instance() don´t have any values set for the on_value and off_value. When editing the field, one can set individual values or leave them empty. When leaving them empty, drupal will assign 1 as value for the on_value and 0 the for off_value.
Still, I want to preset these settings so everything is ready to go when the module is enabled so there is no need to touch the field settings manually.
After some more digging, I found out that the on_value and off_value can be set like this when assigning the allowed_values:

$field = array(
  'some_field' => array(
    'field_name' => 'some_field',
    'type'       => 'list_boolean',
    'cardinality' => 1,
    'settings' => array(
      'allowed_values' => array(0 => 0, 1 => 1), //add the default values for the checkbox
    ),
  ),
);

$instance = array(
  'some_field' => array(
    'field_name' => 'some_field',
    'label' => $t('My Label'),
    'required' => true,
    'widget' => array(
      'type' => 'options_onoff',
      'settings' => array('display_label' => 1),
      'weight' => 8,
    ),
    'settings' => array(
      'user_register_form' => 1,
    ),
  ),
);

I think it would be worth to add this piece of information to the documentation.
In addition, would it not make sense to set the on_value and off_value for a boolean field and widget type per default to 0 and 1 when not set in the configuration array?

Comments

mwallenberg’s picture

Thank you for this info, it saved me a lot of hours. I agree that this should be added to the documentation.

Sneakyvv’s picture

I took me some hours of debugging as well. But thanks to this post I didn't have to find the correct array key for the settings.

I certainly agree that there should be more documentation on the settings of the option widget!

fourmind’s picture

I agree, the field creation could be better documented for the available settings. Having them all in one place would be useful, rather than having to do a trial and error to figure a lot of this out.

Strange that without setting the default in this way, you cannot update the field when creating a node programmatically.

But thanks for this post. It helped save me some time and is much appreciated.

richardtmorgan’s picture

I spent the best part of an hour finding this post - the fieldAPI documentation was not helpful.
Many thanks - I completely agree that there should be per field type documentation.

marcoscano’s picture

+1. Thanks for posting

hamrant’s picture

+1. Thanks for posting

leymannx’s picture

$field = array(
  'field' => array(
    'field_name'  => 'MYFIELD',
    'label'       => 'Set as default',
    'type'        => 'list_boolean',
    // It's mandatory to add at least two empty values here,
    // Drupal will do the rest then:
    'settings' => array(
      'allowed_values' => array(
        '',
        '',
      ),
    ),
  ),
  'instance' => array(
    'field_name'  => 'MYFIELD',
    'entity_type' => 'node',
    'bundle'      => 'MYBUNDLE',
    'label'       => 'Set as default',
    'widget'      => array(
      'type'      => 'options_onoff',
      'settings'  => array(
        'display_label' => TRUE,
      ),
    ),
    'display'     => array(
      'default'   => array(
        'label'   => 'hidden',
        'type'    => 'hidden',
      ),
    ),
  ),
);