Yogesh Chauhan's Blog

How to remove N/A from Radio Button list in Drupal?

in Drupal on July 11, 2020

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: Drupal Forum


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
The Lending Club Analysis using Logistic Regression and Random Forest in RStudioMiscellaneousHow to use @supports rule in CSS?CSSHere’s what we can do with PHP date() functionPHPHow to concatenate variable with string in Swift?SwiftHow to avoid element shift on border change while hovering in CSS?CSSHow to list all PHP variables to debug the script?PHP