0

Trying to manipulate a form that can be a select / radios or text field based on the user configuration...

If the user wants to use radios for the form... then my module will render the using standard drupal render system.

However when I use theme('radios',$variables['element']) it doesn't work at all... the output is:

<div id="some-id" class="form-radios"></div>

However if I use: drupal_render_children($variables) it works perfectly...

Any clues on why theme('radios') behave differently as the rest of the form API items?

This is the code I'm using:

  $form['radio_test'] = array(
    '#type' => 'radios',
    '#title' => 'test',
    '#options' => array( 'a', 'b', 'c' ),
  );

  dpm(theme('radios',$form['radio_test']));

And this is the output

<div class="form-radios"></div>
4
  • 1
    Are you using Drupal 6, or Drupal 7? In which function are you using that code (a form builder, or another function)?
    – avpaderno
    Commented Jan 4, 2013 at 20:16
  • This is D7. I assume it doesn't really matter where is executed. As long as it run under a drupal environment... the output of function theme should be a formatted radio form? Commented Jan 5, 2013 at 18:00
  • I was asking where the code is executed because you don't normally call theme('radios') for a form element you right defined.
    – avpaderno
    Commented Jan 5, 2013 at 18:19
  • Furthermore, to alter the type of a form element, you just change its #type, and eventually other properties; you don't need to call theme() or drupal_render(). When the form is rendered from Drupal, drupal_render() will be called.
    – avpaderno
    Commented Jan 6, 2013 at 1:25

1 Answer 1

1

A radios form field uses a process function (form_process_radios()) which is the function that expand the form element, and add its child elements (the single radio elements a radios element is made of).
When you call theme(), that process function is not called, and those child elements are not added.

theme_radios($variables) executes the following code.

  $element = $variables['element'];
  $attributes = array();
  if (isset($element['#id'])) {
    $attributes['id'] = $element['#id'];
  }
  $attributes['class'] = 'form-radios';
  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>';

As $element['#children'] is not set, what you get from theme('radios') is an empty <div> element with some CSS classes, and attributes.

Differently, when drupal_render() is called:

  • Any #pre_render, #process, #theme_wrappers, and #post_render function is called
  • Any child element is rendered
2
  • I tried your suggestion ( element=>form[] ) and analysed theme_radios before as well. And this is where my question is pointing... theme_radios does not theme the options... it just add a <div> wrapper to the #children property... This doesn't make any sense to me and is not consistent with the rest of the theme_ELEMENTS. Any clue why? Commented Jan 5, 2013 at 23:23
  • What theme_radios does is print a bunch of 'radio' elements out with wrappers etc. The actual radio buttons are themed in theme_radio, which is more comparible to others like theme_textfield. As this answer says, the individual radios elements have to first be built before they are sent to theme_radios, like you can see in form_process_radios.
    – rooby
    Commented Jan 27, 2014 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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