4.7.x form.inc theme_radio($element)
5.x form.inc theme_radio($element)
6.x form.inc theme_radio($element)
7.x form.inc theme_radio($variables)

Returns HTML for a radio button form element.

Note: The input "name" attribute needs to be sanitized before output, which is currently done by passing all attributes to drupal_attributes().

Parameters

$variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #required, #return_value, #value, #attributes, #title, #description

Related topics

1 theme call to theme_radio()
system_element_info in modules/system/system.module
Implements hook_element_info().

File

includes/form.inc, line 2960
Functions for form and batch generation and processing.

Code

function theme_radio($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'radio';
  element_set_attributes($element, array(
    'id',
    'name',
    '#return_value' => 'value',
  ));
  if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
    $element['#attributes']['checked'] = 'checked';
  }
  _form_set_class($element, array(
    'form-radio',
  ));
  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

Comments

Mykola Dolynskyi’s picture

if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
----
this is wrong, because is default_value is false and option with key 0 is present - then 0 is selected by default

Mykola Dolynskyi’s picture

should be like this

function theme_radio($variables) {
$element = $variables['element'];
$element['#attributes']['type'] = 'radio';
element_set_attributes($element, array('id', 'name', '#return_value' => 'value'));

$valattr = isset($element['#value']) ? '#value' : '#return_value';
$defvalattr = isset($element['#value']) ? '#value' : '#default_value';

if(isset($element[$defvalattr]) && $element[$defvalattr] !== FALSE) {
if (isset($element['#return_value']) && $element[$valattr] !== FALSE && (isset($element[$defvalattr]) && ($element[$defvalattr] == $element['#return_value']))) {
$element['#attributes']['checked'] = 'checked';
}
}
_form_set_class($element, array('form-radio'));

return '';
}