4

I have a "gender" (checkbox) option that I want to show on the user profile page (but not on the registration page). The options are "Male" (default) and "Female". Since I don't make the field required it is showing the "N/A" option. Drupal does this by design.

If I make the field required the "N/A" option will go away but then it will automatically show on the registration page which I don't want.

I was trying to hide it with CSS but I cannot see a way to just hide that checkbox and "N/A" text:

<div class="form-item form-type-radio form-item-field-gender-und">
<input id="edit-field-gender-und-none" class="form-radio" type="radio" value="_none" name="field_gender[und]">
<label class="option" for="edit-field-gender-und-none">N/A </label>
</div>
<div class="form-item form-type-radio form-item-field-gender-und">
<input id="edit-field-gender-und-0" class="form-radio" type="radio" checked="checked" value="0" name="field_gender[und]">
<label class="option" for="edit-field-gender-und-0">Male </label>
</div>
<div class="form-item form-type-radio form-item-field-gender-und">
<input id="edit-field-gender-und-1" class="form-radio" type="radio" value="1" name="field_gender[und]">
<label class="option" for="edit-field-gender-und-1">Female </label>
</div>

Help! ;-)

4

You can use jQuery to accomplish this

$("#edit-field-gender-und-none").remove();
  • I haven't used jQuery yet. Isn't it the same as adding this CSS: .edit-field-gender-und-none { display: none; } this doesn't seem to do anything – uwe Oct 18 '11 at 14:16
  • 1
    Here is a great write up on using JS in Drupal 7. drupal.org/node/756722 Specifically here is how you would implement (function ($) { $("#edit-field-gender-und-none").remove(); }(jQuery)); – Hansyg Oct 19 '11 at 17:35
  • Is there any REAL way to do this? Doing it after the fact with css or javascript means that there is a small chance for it to be visible as the javascript is loading and executing. – Kenny Wyland Aug 5 '12 at 20:28
  • Please don't do this. See kenorb's suggestion below. – aaronbauman Dec 11 '17 at 18:06
3

Another quick solution is to have Drupal add a form-disabled class to the N/A form-item by adding the following function to your template.php file.

function <theme_name>_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);
}

Then you can use CSS to do

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

I think a better way to acheive this would be to write a simple form_alter hook and unset() the option you don want from your field.

function YOURMOD_form_YOURFORM_alter(&$form, &$form_state) {
  unset($form['YOURFIELD']['und']['#options']['_none']);
}

in the example above, the field is a taxonomy reference field on an entity edit form, so there is the extra ['und'] - it doesnt have to be there under all ocasions. You may want to dpm($form) first, to see the structure of the form.

  • 1
    You really shouldn't hard code 'und' in there if you can avoid it. And if you must do it, use the constant LANGUAGE_NONE instead. – rooby Apr 24 '13 at 12:47
  • Works, but then I get the Notice: Use of undefined constant _none - assumed '_none' in MYMODULE_form_NODE_ID_form_alter() – leymannx Dec 3 '13 at 14:23
  • This doesn't work in D7 for fields api. – aaronbauman Dec 11 '17 at 18:05
1

In Drupal 7 try the following code:

/**
 * Implements hook_element_info_alter().
 */
function MODULENAME_element_info_alter(&$type) {
  $type['radios']['#process'][] = '_remove_radio_na';
  $type['select']['#process'][] = '_remove_radio_na';
}

/**
 * Removes the '_none' option from a form element.
 */
function _remove_radio_na($element) {
  unset($element['#options']['_none']);
  unset($element['_none']);
  return $element;
}

Make sure you clear caches, otherwise it won't work.

This is a better way than hiding results with CSS or JS/jQuery as empty values are removed before the output.

0

I do this:

jQuery("#edit-field-gender-und-none")[0].children[0].remove();

-1

One of the interesting Features of Drupal 7 is the Field API. Prior to Drupal 6, to make a custom field the Core Profile Module and Content Profile Modules are used. Now in Drupal 7 we can make any custom field to appear in User Registration Form. When Adding Custom Fields Such as Gender usually we use Radio Button Input. However there is a small Problem with the core module as it displays the third option N/A in the input. To Remove the N/A option the field needs to be set as mandatory. To Remove this option there is another way. Go to modules/field/modules/options/options.module and comment the the code $properties['empty_option'] = ‘option_none’ as shown below under buttons.

case ‘buttons’:
$properties = array(
‘filter_xss’ => TRUE,
);
// Add a ‘none’ option for non-required radio buttons.
if (!$required && !$multiple) {
//$properties['empty_option'] = ‘option_none’; 
}
break;

case ‘onoff’:
$properties = array(
‘filter_xss’ => TRUE,
);
break;
}

However the proper way to achieve it would be to override this function in custom module. But for a quick fix we can also follow this method. [blog]: http://yamafm.wordpress.com/ "click here for updates"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.