Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to have the 'true' option for a Boolean radio field output first in Drupal 7

Parent Feed: 

If you use a Boolean field with radio buttons in Drupal 7, the false radio button outputs before the true. This became an annoyance to us whilst working on mega-sized forms, so here's our fix. To fix this you'll need to create a custom module, and implement the hook hook_field_widget_form_alter().

The following code assumes your module is called 'example'. All the code does is reverse the order of the options whilst preserving the keys, simple!

/**
 * Implements hook_field_widget_form_alter().
 *
 * Have boolean fields output the true first.
 */
function example_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['type'] == 'list_boolean' && isset($element['#options'])) {
    $element['#options'] = array_reverse($element['#options'], TRUE);
  }
}

That's it!

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web