2

I've written the following update hook to add a field to a content type. In an ideal world I'd like that field to be a single on/off checkbox. However, the code I've written outputs three radio buttons - with options for Yes and No, as you'd expect - but one for NA, too. I don't really want my users to have an NA option. Any idea how to achieve this? I'm guessing it's either the widget or display attributes, but I don't know where to find a list of what options are available to me.


Update:

Marcel's suggestion worked just fine - it doesn't provide a checkbox, but it removes the NA option, and that's good enough for me. Adapted code is below - I've also made some changes to the rest of it that ensure the title and summary text are displayed properly in the editor UI:

$field = array(                                       
  'field_name' => 'field_maxy8',    
  'type' => 'list_boolean',     
  'settings' => array(
    'text_processing' => 0,
  ),
  'widget' => array('type' => 'options_onoff'), 
  'settings' => array(
    'allowed_values' => array(
      '0' => 'No',
      '1' => 'Yes',
    ),
  ) 
);

$field = field_create_field($field);

$instance = array(
  'field_name' => 'field_maxy8',
  'required' => 'true', 
  'label' => 'Maxy 8',
  'description' => st('Maxy 8'),
  'entity_type' => 'node',
  'bundle' => 'article',
  'display' => array(
    'teaser' => array(
      'label' => 'hidden',
      'type' => 'hidden',
    ),
    'default' => array(
      'label' => 'hidden',
      'type' => 'hidden',
    ),
  ),
);

field_create_instance($instance);

In order to use this you'll need to:

1) Put it in a <modulename>.install file

2) Put it in a hook_update_n function

3) Save it, and run UPDB through Drush

  • Set it to required TRUE? – Marcel May 16 '14 at 18:34
  • Thanks @Marcel - that didn't quite get the result I wanted, but it will do the job nicely. For anyone else reading, I made a couple of changes to this code that make the whole thing a bit nicer - I've updated the code in the main query. – jamesdesq May 21 '14 at 10:37
5

This is how you create a single boolean checkbox field and field instance: I have excluded checking whether the field or instance exists, for brevity.

field_create_field(array(
  'field_name'  => "my_bool",
  'type'        => 'list_boolean',
  'cardinality' => 1,
  'settings'    => array(
    'allowed_values' => array( 
      /*
       * The docs say that this is optional, but without it,
       * the value is never saved to the datbase (tested with Drupal 7.28)
       */
      0 => '',
      1 => '',
    ),
  ),
));

field_create_instance(array(
  'field_name'  => "my_bool",
  'entity_type' => 'taxonomy_term',             // or 'node'
  'bundle'      => 'my_vocabulary_machine_name, // or 'my_content_type'
  'label'       => t('My boolean checkbox'),
  'required'    => false,                       // because not ticking a checkbox omits the value from $_POST
  'display'     => array(
    'default' => array(
      'type' => 'hidden',                       // optional. I want to hide it
    ),
  ),
  'widget'      => array(
    'type'     => 'options_onoff',              // gives you a checkbox
    'settings' => array(
      'display_label' => 1,                     // uses 'My boolean checkbox' as the label next to the actual checkbox
    ),
  ),
));

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.