YogeshChauhan.com

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

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

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

12 URLSearchParams methods in JavaScriptJavaScriptFETCH clause in PostgreSQLPostgresHow to Use password_hash and password_verify to Secure Your User’s Data (Especially Passwords)?PHPSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’SwiftHow to solve “framework not found” error in Swift?SwiftRecursive WITH Queries in Postgres (Common Table Expressions)Postgres