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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to disable the Recovery Mode in WordPress?WordPressHow to create a simple digital clock using JavaScript?JavaScriptWhat is the difference between float and double?MiscellaneousCreate a currency (dollar) to coins convertor in ReactReactHow to add Laravel to WordPress using Sage theme (and install Tailwind CSS)?PHPHow to create a Random Hex Color generator using JavaScript?JavaScript