I am using Forms API to create a select box. This works great. However, I need to set an HTML attribute for each of the options generated by the #options element.
I know I can set HTML attributes using the #attributes element, but this sets the attribute for the select box, not the individual entries.
I can create the entries like this:
$options['http://www.drupal.org'] = 'My Favorite Site';
$options['http://www.php.net'] = 'My Most Frequented Site';
Then I simply pass $options into the #options portion of the Forms API like so:
'#options' => $options,
I can also set HTML attributes to the whole select box using:
'#attributes' => array('class' => 'levelx');
But like I said, this would add a class attribute to the select box, not the individual entries.
Does anyone know how I can add an attribute to the individual selections per their creation using Forms API?
I tried this in a naive attempt:
$options['http://www.drupal.org']['#attributes'] = 'class=levelx';
Thanks.
Comments
Override theme_select
Override theme_select (http://api.drupal.org/api/function/theme_select/6) in your theme
Mainly you will have to write your own version of http://api.drupal.org/api/function/form_select_options/6
So I think what you are
So I think what you are saying is that I need to run through my code to build the form as I normally do, then right before I do return $form;, I need to pass the form element of type 'select' through a theme() function, and then do return $form;?
The theme() function will do what I instruct it to do by overriding the form_select_options() function?
This is just slightly over my head, but I think I can reach if I stretch a little, so if it's alright, let me see how far I can get and then come back with follow up questions.
Actually the theme function
Actually the theme function is called as part of building the form.
Right right, I meant the
Right right, I meant the theme_select() function per my overriding it.
Thanks, still working on it and reading up to get to where I need to get or get stuck at which point I'll be back
Here is where I am so far...
I overrode the theme_select() function by adding it to my theme's template.php file like so:
This means that instead of calling the original theme_select() function found in form.inc, it will call this one (right now it does the same thing).
As you can see, it does call the form_select_options() function on the last line.
I need to write my own version of that function. Is there a way to override that function like I did my theme_select() function?
You can simply call a
You can simply call a different function where form_select_options() is called.
Thanks!!
I just want to say thanks for your guidance.
I was able to do what I wanted based in great part to your guidance.
For those that want to do this in the future here is how I did it:
1) Override the theme_select() function by adding a new function to your theme's template.php file and replace the word "theme" in the declaration of the function with the name of your theme. For example, if youa re using the Garland theme, the new name of the function would be garland_select() as the example below will show. The code should initially be an exact copy of the actual theme_select() function which you can get from the API reference guide (api.drupal.org).
2) Create a new version of the form_select_options() function within the same template.php file. You can call it myform_select_options. The code should initially be an exact copy of the actual form_select_options() function which you can get from the API reference guide (api.drupal.org).
3) Look at the code in the function you created in step one and instead of calling the original form_select_options on the last line, have it call your new one (myform_select_options).
4) Modify the new myform_select_options to build each entry of the select box to your liking.
Below are the functions that I used to accomplish this. Note that all I wanted to do what add "class="levelx"" to each option where x was the depth level. In another function using Forms API, I add "-" (hyphens) to symbolize depth and be displayed more deeply to the user, so I just went in and counted "-" with the PHP function substr_count() to let me know what level the entries are found in and then divide by 2 since I added two "-" every time I needed to display an entry at a deeper level. Don't worry about this too much since this is my own need. You will modify the new myform_select_options() function to do what YOU need it to do.
Here they are (if you copy and paste them, do not include the php tags at the beginning and end of each example since those should already exist in your templete.php file, I added them for enabling color coding):
Just below is the overridden theme_select() function in my theme's template.php file. Note the new garland name when it is declared. This is just an example that uses the garland theme. You must replace this with your theme's name (step one above). Also note that the last line calls my new version of the form_select_options() function called myform_select_options() which I show you below.
Just below is the custom form_select_options() function in my theme's template.php file. Note that I only modified the instruction just before "return $options;" because that is what I needed to affect. You may be looking to modify something else.
Thanks again to the folks above who helped to guide me.
there is a key error in post
there is a key error in post abowe
if u want to have classes for all levels of hierarchy then u have to have
"myform_select_options" called on all parts of the same function and in code aboe default function is called "form_select_options" and then this code doesnt work and is useless. use this instead
and finally u have all the classes u need, ufff spent 2 hours on this, strange that nobody ever tought dropdown for taxonomy would be themed, this is pretty hard to do and it should be easier in drupal.
Adriadrop Drupal development
How to activate the new function
The theme registry has to be rebuilt ones to take effect.
To do that, just put the following line in your template.php, reload a page and remove it.
http://api.drupal.org/api/function/drupal_rebuild_theme_registry/6
Pls help , I am getting stuck
My goal is to add title="sometext" into my form.
So below is the html output I am able to achieve based on your post. I am using d7. Please check and advice what I am doing wrong.
As you can see title is there but the value is wrong. Below is my form and the functions which I had to override.
My form:
My Theme functions.
cheers,
vishal
redcrackle.com
sorry...
it's a very old post, but exactly what i'm looking for. and i haven't been able to make it run...
My mytheme_select function is never called. Is there something special to do with drupal 7 ?
up... please !
up... please !
After 13 years still it does
After 13 years still it does not supported :(
I have be born to be mankind