1

I am trying my hand at theming form output and am stuck on radios. I am attempting to remove the div that wraps a set of radios,

div.form-item > div.form-radios > div.form-type-radio > input[type="radio"]+label
                ^^^^^^^^^^^^^^^ <- Remove that

I have a sample form:

function no_name_form($form, &$form_submit) {
    $form['radios'] = array(
        '#type'    => 'radios',
        '#title'   =>'Question text',
        '#options' => array(
            '1' => t('yes'),
            '0' => t('no')
        ),
        '#theme'   => 'no_name_radios',
    );
    return $form;
}

and I register the theme functions for both the radios group and individual radio buttons:

function no_name_theme($existing, $type, $theme, $path) {
    return array(
        'no_name_radios' => array(
            'arguments' => array('element' => NULL),
        ),
        'no_name_radio' => array(
            'arguments' => array('element' => NULL),
        ),
    );
}

Then I define the theme functions:

function theme_no_name_radios($variables) {
    $element = $variables['element'];
    $attributes = array();
    if (isset($element['#id'])) {
        $attributes['id'] = $element['#id'];
    }
    $attributes['class'] = 'form-radios test';
    if (!empty($element['#attributes']['class'])) {
        $attributes['class'] .= ' ' . implode(' ', $element['#attributes']['class']);
    }
    if (isset($element['#attributes']['title'])) {
        $attributes['title'] = $element['#attributes']['title'];
    }
    return '<div' . drupal_attributes($attributes) . '>' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
}

function theme_no_name_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']) . ' />';
}

When I use even the stock code from Drupal 7 to build the theme functions, the radios come out blank, and at this point none of the code has changed.

Ultimately, I want to end up with this:

div.form-item div.form-type-radio > input[type="radio"]+label

I am not sure where I went wrong.

2

The div.form-radios is set by the theme wrapper of the radios. Use this code to prevent the theme wrapper from adding the additional div:

function no_name_form($form, &$form_submit) {
  $form['radios'] = array(
    '#type'    => 'radios',
    '#title'   =>'Question text',
    '#options' => array(
        '1' => t('yes'),
        '0' => t('no')
    ),
    '#theme'   => 'no_name_radios',
    '#theme_wrappers' => array(),
  );
  return $form;
}

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.