2

I have a field so-called "Status" in my custome contact type. It is a List (integer) and widget type is "Radio buttons". I added two values for allowed values list:

 0|Sold
 1|Available

Default value is "Available". But Drupal lists this as below:

 () N/A
 () Sold
 () Available

Why does Drupal add this unnecessary "N/A" to the radio buttons? I encountered this problem since D6. Now, I'm using the latest version 7.14. I'm still seeing this.

I know I can remove the "N/A" by writing a hook from a custom module. Nonetheless, I don't want to write additional codes for this kind of small thing. When I use "Select list", it is ok with an option "-None-", but the radio button is ideally suited for the two options.

How can I remove it without installing additional module? Am I missing some functionalities from Drupal core interface?

0

7 Answers 7

2

When your field is required, no N/A options should show up (and you said you need this field to be required).
To show you an example,

  1. I created a new content type named "First test content type" (with the machine name first_test_content_type)
  2. Went to "Manage fields", and added a field with radio buttons (with the machine name field_test_radio_buttons)
  3. Added the mentioned options:

    0|Sold
    1|Available
    
  4. I checked the "Required field" checkbox (this is the key for not seeing N/A as a separate option!!)

  5. Made "Available" the default option
  6. So, on /admin/structure/types/manage/first-test-content-type/fields/field_test_radio_buttons, the following shows up: SCREENSHOT
  7. When adding a new "First test content type" content, I can see the following radio buttons (click on the image to see the whole page):
    Test radio buttons
  8. So this is the result.
2
  • 1
    I overlooked this because I see the N/A in the field configuration page.
    – Sithu
    Aug 3, 2012 at 11:10
  • @SithuKyaw: ah, I see, yes, it really shows up when editing the field settings, but when selecting another value as default, it won't show up on the user interface. Anyway, I'm glad your problem is solved!
    – Sk8erPeter
    Aug 3, 2012 at 12:02
3

Something like this should help.

(function($) {
  $('.form-radio[value=_none]').parent().hide();
})(jQuery);

http://drupal.org/node/1623360

2

If your field is not required and not multiple then there's no way to remove the N/A without writing some custom code I'm afraid.

It's hard-coded into the _options_properties() function:

// Add a 'none' option for non-required radio buttons.
if (!$required && !$multiple) {
  $properties['empty_option'] = 'option_none';
}

Your only choice is to write a small form alter hook unfortunately; this was obviously a design decision made by the core developers when they were implementing that module's functionality. I'd imagine the justification would be UX-based but I don't know for sure.

2
  • I think Drupal should hide or show it upon field required or optional. I'm expecting this feature in later core releases. Do you know where can I report this?
    – Sithu
    Jul 25, 2012 at 9:02
  • 1
    I think the core issue queue would be the best place to start
    – Clive
    Jul 25, 2012 at 9:13
1

It's implemented that way, because otherwise users don't have any way to deselect a radio button, and for some (government) sites that's a requirement.

To change it use:

  • a form_alter in a custom module
  • javascript inside your theme to hide the N/A option.
  • theme_options_none
4
  • My field is required, thus the N/A is not in deed necessary. No need to deselect it. Any way, I voted you.
    – Sithu
    Jul 25, 2012 at 8:58
  • @SithuKyaw: wait, is your field really required? So is the "Required field" checked when you go to admin/structure/types/manage/this-is-your-test-content-type/fields/field_this_is_your_field_with_radio_buttons? Please show us a screenshot about that! In case you have "Required field" checked, the N/A shouldn't show up - just tested it, and I don't have any N/A options!
    – Sk8erPeter
    Aug 1, 2012 at 15:38
  • @Sk8erPeter: Thanks, you helped me! Will you post this as an answer or will I post by myself.
    – Sithu
    Aug 3, 2012 at 2:48
  • @SithuKyaw: you're welcome, I'm glad then. I posted a separate answer with many screenshots.
    – Sk8erPeter
    Aug 3, 2012 at 10:22
0
unset($form['fieldname'][$form['fieldname']['#language']]['#options']['_none']);

should do it in a form_alter hook. replace "fieldname" with your field's name. this should work for all select fields no matter what widget is used.

cheers, j

0

You can remove this "N/A" radio button option for all fields, or just specific fields, using the following hook_field_widget_WIDGET_TYPE_form_alter() implemention:

<?php 

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function modulename_field_widget_options_buttons_form_alter(&$element, &$form_state, $context) {
  if (!isset($context['field']) || !isset($context['instance'])) {
    return;
  }
  $field = $context['field'];
  $field_instance = $context['instance'];

  if (!isset($field['field_name']) || !isset($field_instance['entity_type'])) {
    return;
  }
  $field_name = $field['field_name'];
  $entity_type = $field_instance['entity_type'];

  if (isset($element['#type'])
      && $element['#type'] === 'radios'
      && isset($element['#options'])
      && is_array($element['#options'])
  ) {
    $options =& $element['#options'];
    unset($options['_none']);
  }
}

?>

To limit this to change only selected fields, check on the $field_name and $entity_type.

-1

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.
if (!$required && !$multiple) {
//$properties['empty_option'] = ‘option_none’; 
}
break;

case ‘onoff’:
$properties = array(
‘filter_xss’ => TRUE,
);
break;
}

However the 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. [blog]: http://yamafm.wordpress.com/ "click here for updates"

Not the answer you're looking for? Browse other questions tagged or ask your own question.