1

I'm trying to customize a radios field to wrap some of the options into a div.

What the best way to do it?

I had a look to theme_radio but no success on it :( example:

<div id="part1">
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
</div>

<div id="part2">
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
</div>

<div id="part3">
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
  <div class="form-item form-type-radio form-item-SOME_ID">
    <input id="edit-SOME_ID" class="form-radio" type="radio" value="SOME_VALUE" name="SOME_NAME" /> 
    <label class="option" for="edit-bla-bla">MY LABEL</label>
  </div>
</div>

ANSWER:

See Djouuuuh answers +

$output .= drupal_render($element['element'][$i]);

if you do this one below it won't work with ajax...

$output .= theme('radio', $element['element'][$i]);
  • There is a similar question already: drupal.stackexchange.com/questions/23021/… Would it be useful for your case? – dinopmi Apr 24 '15 at 14:13
  • not really, in the example it adds it for all of them and not a wrapper containing couple of options only – GwenM Apr 24 '15 at 14:17
  • Can you please give an example of your desired output please? – Leolando Tan Apr 24 '15 at 14:37
  • I've edited the original post – GwenM Apr 24 '15 at 14:57
1

In my opinion you can achieve that with a theme function, specified like this:

$element['example'] = array(
  '#type' => 'checkboxes',
  '#title' => t('Example checkboxes'),
  '#options' => $options,
  '#theme' => array('custom_options'),
);

And then, you can write down the output just as you want here:

function theme_custom_options($element) {
  $output = '<div id="blah">';
  $output .= $element['#options'][0];
  $output .= $element['#options'][1];
  $output .= '</div><div id="blablah">';
  $output .= $element['#options'][2];
  $output .= $element['#options'][3];
  $output .= '</div>';
  // ETC...
  return $output;
}

Does it seem correct?

| improve this answer | |
  • Sorry I wrote "checkboxes" instead of "radios" of course... :( – Djouuuuh Apr 24 '15 at 15:03
  • I've tried to do this kind of thing but for obscure reasons the options where disappearing when I was returning the fucntion. (I even copy the theme_radios function into my custom one but same problem) :( – GwenM Apr 24 '15 at 15:04
  • Have you got Devel installed to debug? – Djouuuuh Apr 24 '15 at 15:05
  • yes but somehow when I dsm it doesn't show up all the time. Really annoying to debug – GwenM Apr 24 '15 at 15:06
  • That is wierd. So if you dsm $element in your custom theme function, nothing appears? – Djouuuuh Apr 24 '15 at 15:07

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.