In CCK version 6, non-required radio button fields add an extra "N/A" option. This is a deliberate decision made by the CCK module maintainers but sometimes this is not what you want.

Solution 1: Making it mandatory

When you make it mandatory the n/a won't show up, but if you don't want the field mandatory then there is a coding solution you can use.

Removing the n/a option means a user cannot empty the field. 'no value' is a valid value for a non-required field." But it won't always make sense from a user's perspective. 

Make the field required, AND add your own version of the N/A that makes sense in context: unknown, undecided, decline to answer, etc... And make it the default value.

The downside to this is that this answer would appear in the content (unlike when N/A is selected).

Solution 2: Add a PHP function

Here's a helpful snippet that can be adapted to remove this extra option on CCK radio button fields for your site.


/**
 * Implementation of hook_elements().
 *
 * This extends optionwidgets_elements() to add in additional processing.
 * Note that your module must be weighted higher than optionwidgets
 * for this code to take effect.
 */
function example_elements() {
  $type['optionwidgets_buttons']['#process'][] = 'remove_radio_na';
  return $type;
}

/**
 * Unset the N/A option on option widget radio buttons.
 */
function remove_radio_na($element) {
  unset($element['value']['#options']['']);
  return $element;
}

Solution 3

You can add a class definition of display:none, if you do not want it to be mandatory. In this way you can mange it in both node create/edit form and also in node view.

Solution 4: PHP function and a bit CSS


function _form_element($variables) {
  $element = $variables['element'];
  // Disable radio button N/A
  if ($element['#type'] == 'radio' && $element['#return_value'] === '_none') {
    $variables['element']['#attributes']['disabled'] = TRUE;
  }
  return theme_form_element($variables);
}

Add some CSS afterwards...


.form-radios .form-disabled {
  display: none;
}

Solution 5: hook_form_alter

After adding bellowed single line's code on your hook_form_alter "N/A" removed from Radio Buttons.


unset($form['field_name']['und']['#options']['_none']);

Credit goes to: https://www.drupal.org/forum

0 Comments

Leave a reply

Your email address will not be published. required fields are marked *