I'm trying to associate an image with each radio button in a radio group and not having any luck. I've searched Drupal.org with no luck, actually just found a lot of unanswered variations on my question. Is the way to do this so convoluted that it's been lost to the ages or is this simply not a feature? Could it be added so developers can use this kind of code:
// Create associated options array
$some_num = 3;
for ($i=0; $i<$some_num; $i++) {
$options[] = array(
'title' => $i,
'prefix' => '<div class="radio_image"><img src="/' . $i . '.png" />',
'suffix' => t('Element ') . $i . '</div>',
);
}
// Create Drupal "radios" form element
$form['form_name']['element_name'] = array(
'#type' => 'radios',
'#title' => t('Title'),
'#default_value' => 2,
'#options' => $options,
);
...or something like...
// Create Drupal "radios" form group with unique image/button association
$form['form_name']['element_name'] = array(
'#type' => 'radios',
'#title' => t('Title'),
'#default_value' => 0,
'#options' => array(t('Item 1'), t('Item 2'), t('Item 3')),
// Options are each given a unique image
'#option_prefixes' => array('<img src="item_1.png" />','<img src="item_2.png" />','<img src="item_3.png" />'),
'#option_suffixes' => array(),
// Radios grouping is wrapped in div classed 'radios_with_images'
'#prefix' => '<div class="radios_with_images">',
'#suffix' => '</div>',
);
What say the folks behind the curtain? Does this already work?
Comments
Comment #1
ashsc commentedI found something that works. Unfortunately it *IS* convoluted. At least more so than it seems it should be.
I am trying to style a CCK radio content type. I want each radio to have a unique picture assigned with it. Unfortunately I can't access $form['cck field'] directly and therefor I can't access $form['cck field']['cck field options'] either.
The solution I used was to create a custom module which has its own database table and uses hook_form_alter to add the radio buttons. The radios are *NOT* added with CCK. In this example I create a fieldset to encompass my radio buttons. To do this I needed three files in my module directory; my_module.module, my_module.info, and my_module.install. Here's the code for each file. I used the example_nodeapi.module to make this work...
my_module.module:
my_module.info:
my_module.install:
So, lots of code to make this work. Seems like it would be easier (at least from a Drupal site developer's standpoint) to have something like what's mentioned in my question. Maybe D7 will have this?
Comment #2
sunThis is possible in D7 now.
Comment #3
MvdVelde commentedMaybe the solution for Drupal 6 can be found in: http://drupal.org/node/772446
Comment #4
bel.inna commentedPlease, explain, how do this in D7?
Comment #5
Karthick_s commentedin D7 if you want to add img it is available in field settings itself