YogeshChauhan . com

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

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

A Quick Comparison of JOIN and Subquery in SQLSQL/MySQLWhat is Git?MiscHow states work in React?React3 Ways we can create URLSearchParams Objects in JavaScriptJavaScriptDefault Values in SCSS (Sass)SCSSHow to render lists inside a component in React?React