Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By Budrick on
How do I group options in optgroup for select lists with forms api?
How do I group options in optgroup for select lists with forms api?
Comments
Solved
optgroup in Drupal 5.0
Seems that "option" structure changed in version 5.
So, the code becomes a little more complex:
Sorry for my ignorance, but
Sorry for my ignorance, but I do not understand forms API. I've looked at documentation but still don't get it. Can someone simply tell me in what file to insert this snippet of code? (I'm using Drupal 5.2 btw)
Also, just to make sure, if I understand it well, this code allows you to put parent categories in < optgroup > ?
Thanks.
Anyone? I figured that I
Anyone? I figured that I have to create my own module, with this code included in it.... I don't have any experience with creating modules though. Anyone willing to help? Thanks.
I think you create a file
I think you create a file called optgroup.module with the code posted above and another file called optgroup.info with :
Does it works for Drupal 6.12
Does it works for Drupal 6.12 .... ??
------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)
Drupal 6.x code
Actual d6 code...
Optgroups are simple in d6...
Yep. Working example for
Yep. Working example for inside modulename_form_alter:
replace modulename with whatever your module is called.
why not use str_repeat?
Rather than the _modulename_spaces function, why not just use
Formerly SkidNCrashwell. Changed my username to reflect my Twitter handle.
what is "ptid"?
What is ptid in your code?
By the looks of it, the
By the looks of it, the taxonomy id of the parent of the current taxonomy item. This has nothing to do with actually using optgroups, the example is creating optgroups from the structure of a taxonomy, so that drags in a lot of taxonomy stuff.
grouping taxonomy terms in a select list drupal 6 form api
This only seems to work with select lists, not with radios. Basically, you create a nested array. This is example is hard-coded. You could abstract it if you needed it to be portable.
this code will feed the '#options' parameter an array that looks like this:
Drupal 7 Example
Here's a form_alter function to accomplish this in a two-tier taxonomy structure. It outputs an array like the previous D6 example.
Awesome, thanks!
Awesome, thanks!
A better version of above snippet
What's the implementation of this Gik000
Gik, how would you implement your better version? Is it contained within hook_form_alter as the prior block of code??
Implementation sample
You can implement it like this:
@Gik000 Thank you for share.
Thank you, that is what I was
Thank you, that is what I was looking for!
Implementation sample worked for me too, using after_build
This sample code worked great for my purpose as well, Thanks.
I had to call it from _after_build#. I think this is because some other modules ( hierarchical select, possibly, in my case ) are also rewriting the #options array.
And that get_optgroup_by_taxonomy function is identical to the previous example.