By matt v. on
Is it possible using FormAPI to assign attributes to a single radio button within a "radios" set? I've tried the following, inside a custom hook_form_alter function:
$form['field_smoking']['key']['#options']['I smoke daily.']['#attributes'] = array('class' => 'switch-smoker');
But it doesn't seem to be working. What am I missing?
Comments
...
A 'radios' element gets expanded to individual 'radio' elements at some stage. The form_alter stage is too early and this expansion hasn't yet taken place. The theming stage, however, is late enough, so there you do have access to the individual 'radio' element. But there's a stage even earlier than the theming stage: #after_build. You can install a handler for this stage in form_alter:
that worked
mooffie,
Thanks a lot! I hadn't run across #after_build before. That was just what I needed.
I've tried this method for
I've tried this method for adding additional attributes to the options of a select field. Here's what I did:
Output of $element (part):
My after_build function. I'm sure it's called.
Somehow it's not working. Thanks for any help!
...
You can't. Drupal doesn't support attributes for options.
(However, you can #theme the selectbox yourself to draw it entirely, then you'd be able to do whatever you want.)
Yes, it could be useful to be able to attach CSS classes to individual options, via FAPI, e.g. to color them differently. You should open a feature request.
Thanks mooffie for your
Thanks mooffie for your answer. Can you tell me which theme function is responsible for drawing selects / options?
IMHO IE is not able to interpret classes or anything else (like disable="disable"). In my case I've to provide a select box where some options are not available for selecting but have to be displayed. So I started with disabled. After I found out it's not working in IE I've found a JS snippet which does something like that:
As you can see, I've to add for every single option a state value. Thanks again for your help.
...
But that's only part of the story. I understand that you want to write some JS code that simulates disabled options. If I were you I'd do everything on the JS side --instead of maintaining two pieces of code.
But are you sure IE doesn't support 'class'? (if it supports 'style', it sure does support 'class') Because if it doesn't, you won't be able to paint the options in grey to give the impression they're disabled.
Devel or D5 hack
If you're using Drupal 6, the Devel module provides a way to find what function is responsible for formatting a particular piece of content. For Drupal 5, there's a trick detailed here.
--
Drupal Theme Developer’s Cheat Sheet | 45 Screencasts to Get You Kicking Ass with Drupal
Mmh, I don't know if I can
Mmh, I don't know if I can theme it. There are two functions: function theme_select() and function form_select_options(). I thought I can only overwrite theme functions like theme_select(). Or am I wrong? I've tried to copy the form_select_options() function to my template.php and renamed the function to phptemplate_form_select_options() but nothing happened.
EDIT: I've found a solution in JavaScript (jquery):
Works great
Thanks a lot for this solution. That works great! My first time using #after_build :)
Another alternative for your reference
I tried, use '#after_build' is OK.
There another alternative way, I tested in drupal 7, post here for reference:
We can add attributes to options
Drupal FAPI does not support this feature but we can add attributes to options by slightly modifying form.inc file.
You only need to change following line in form.inc file.
with this
You can use like it.