0

I need to create several groups of radio buttons, grouped by a concept (collapsible) but the radio buttons must have the same name so that the form only sends one selected option among all the groups

Group 1
() Radio 1
() Radio 2
() Radio 3
Group 2
() Radio 4
() Radio 5
Group 3
() Radio 6
() Radio 7

I am developing a shipping method of commerce_shipping This form is for choose a shipping service details, therefore I only generate a part of the form of the checkout page in the function that defines the details of the service, using the commerce_shipping API. I test to set the #name property for all radio buttons but I lose the form tree, and I need to keep it so that another function can receive the data. Being inside commerce_shipping I do not have absolute control either.

The question is: What Form API elements should I use?

I tested

$form['group_1']  = array (
  '#type' => 'fieldset',
  '#title' => t('Group 1:'),
  '#container' => TRUE,
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,  
);
$form['group_1']['radios'][] = array (
  '#title' =>  'Radio 1',
  '#type'=>'radio',
  '#return_value' => '1',
);

But ther resulting radio button name is group_1][radios][0] and the next is group_1][radios][1], as the name is different then they are not part of the same selection

2
  • You can control the name of an element with #name, and Drupal won't validate duplicates or anything like that. Might be enough just to use that depending on your needs
    – Clive
    Nov 15, 2017 at 15:48
  • I explain in my question: I test to set the #name property for all radio buttons but I lose the form tree, and I need to keep it, so that another function can receive the data of this form. Being inside commerce_shipping I do not have absolute control either. I do not want to put the rest of the form's names in #name because they are parts that do not depend on me.
    – ferriol
    Nov 16, 2017 at 22:26

1 Answer 1

0

Every group is a fieldset

  $form['pickup_group']['term_'.$tid] = array (
    '#type' => 'fieldset',
    '#title' => $pickupWrapper->field_taxonomy->name->value(),
    '#container' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,  
  );

Every radio

  $form['pickup_group']['term_'.$tid]['shipping_pickup_point'][] = array (
    '#title' =>  drupal_render($renderable_node),
    '#type'=>'radio',
    '#return_value' => $pickupWrapper->getIdentifier(),
    '#parents' => array('commerce_shipping','service_details','shipping_pickup_point'),
  );

I solved adding the array of parents and itself in #parents on every radio, I don't like so much because I don't have control about this parents. My code doesn't generate this parents it comes from the commerce_shipping API. But works...

Your Answer

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

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