3

When rendering form type radios using drupal_get_form, each one of the radio's options can be exclusively selected, as the html name attribute is set to the parent's ('radios') id.

However I'm working on an ajax form module that integrates with mailchimp, and renders these form elements individually in isolation using drupal_render so they can be placed in a template.

My code to render radios is as follows:


$form_child = array (
  '#title' => 'Some Radios',
  '#weight' => '6',
  '#required' => false,
  '#default_value' => '',
  '#type' => 'radios',
  '#options' => array (
    'Option 1' => 'Option 1',
    'Option 2' => 'Option 2',
    'Option 3' => 'Option 3',
  ),
); // This is generated in my module

$form_child = form_process_radios($form_child);

$myhtml = drupal_render($form_child);

And here is what happens on my form: HTML output

In the browser

1

the function of the form_process_radios as given by the documentation is as follows

Expands a radios element into individual radio elements.

So the out put you are getting is correct as per the documentation.

So in your case you dont need to call the form_process_radios. You can directly call the drupal_render function.

  • 1
    When I just call drupal_render, I get no radios showing at all. – user588064 Jan 25 '13 at 18:53
  • Here's the html output f.cl.ly/items/2Z402T3K040N0C3e0u1J/… – user588064 Jan 25 '13 at 18:53
  • 1
    you should set the attribute by adding the the following to the folm element $form['#attributes'] = array('name' => 'nameofradio); and yes you have to pass it through the form_process_radios as well – Mohammed Shameem Jan 25 '13 at 19:06
  • This is not the expected behaviour of the form api however. Look at all the form api examples for radios api.drupal.org/api/drupal/… There is clearly something not working right here, and setting the name attribute manually would not work in my case. – user588064 Jan 25 '13 at 19:11
  • you could also try the theme_radios function – Mohammed Shameem Jan 25 '13 at 19:22
1

I solved this problem eventually by calling drupal_process_form in hook_form_alter. Not sure if this is correct, but it works well and no extra steps are needed to get form elements rendering correctly. Any follow up or advice is appreciated. I'm sure that drupal_process_form is called in the drupal_get_form render path, just not sure of what hook to render custom parts in various locations. Probably theme_hook for the form, but it works well now for my purposes.

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.