How to Remove N/A Option in Radio Button Field Drupal 7

By

December 5, 2011Drupal3 Comments

One of the interesting Features of Drupal 7 is the Field API. Prior to Drupal 6, to make a custom field the Core Profile Module and Content Profile Modules are used. Now in Drupal 7 we can make any custom field to appear in User Registration Form.

When Adding Custom Fields Such as Gender usually we use Radio Button Input. However there is a small Problem with the core module as it displays the third option N/A in the input. To Remove the N/A option the field needs to be set as mandatory.

To Remove this option there is another way. Go to modules/field/modules/options/options.module and comment the
the code $properties[‘empty_option’] = ‘option_none’ as shown below under buttons.

 case 'buttons':
      $properties = array(
        'filter_xss' => TRUE,
      );
      // Add a 'none' option for non-required radio buttons. Removes the Annoying N/A Option Shown in Radio Button Select
      if (!$required && !$multiple) {
    //    $properties['empty_option'] = 'option_none';
      }
      break;

Note: Proper way to achieve it would be to override this function in custom module. But for a Quick Fix we can also follow this method, as we are just commenting a single line.

Tags:
  • http://viperwolfplayer.com lauraine2012

    Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

  • Ran Bar-Zik

    Messing with Drupal’s core is bad idea, the most simple way to do it is with using hook_form_alter and do something like that:

    //remove all N/A options from the menu

    foreach ($form as $k => $v) {

    if(is_array($form[$k]) && isset($form[$k][‘und’][‘#options’][‘_none’])) {

    unset($form[$k][‘und’][‘#options’][‘_none’]);

    }

    }

    • http://mydons.com/author/ramesh/ Ramesh

      I really appreciate your point of view. Thanks for your suggestion. As per this example we are not writing any code core, just a single line commented out as a
      quick fix.