Hi,

I've attached a patch which allows users to specify optgroup labels within the select component. Labels are specified in the following form:

optgroup_label|key|value

If you have optgroup's specified but are rendering as radios or checkboxes, the list is automatically flattened. I'm not sure if the label format is the best way to do it, so if you have any better ideas feel free to suggest them. Even if it takes a while for this to be rolled in, I'd like to have the format specified so I can implement it on a site. Otherwise, I'll roll another patch with updated documentation and Drupal 6 support.

Thanks!
--Andrew

Comments

deviantintegral’s picture

StatusFileSize
new1.83 KB
deviantintegral’s picture

StatusFileSize
new2.07 KB

I had a thought of a different way to do it; this is easier for users, but could break anyone who happens to use 'group' as a key:

group|Group 1
key|option
key|option
group|Group 2
key|option
key|option

To unset the grouping, to have selectable items at the root, you use.
group|

Any thoughts as two these two approaches would be appreciated.

quicksketch’s picture

This looks pretty good. I think I prefer the first approach more than the second, even though it results in more redundancy. Do you have a particular leniency towards one way or the other?

deviantintegral’s picture

The main reason for the second version was being able to specify options without keys:

group|Group 1
option
option
group|Group 2

etc...

My main concern is making it as clear as possible for non-technical users, and the 2nd way seems to be simpler to explain, and visually matches what will be rendered.

--Andrew

quicksketch’s picture

Ah, I see that makes a lot of sense. Do you think we could possibly devise another format instead of group|Name for creating groups? It doesn't seem like the word "group" is really necessary at all. Maybe something like this?

<Group 1>
option
option
<Group 2>
etc

The <word> syntax is relatively common in Drupal for special meanings e.g. <front> or <hidden>. Any user wanting to actually include brackets would need to do something like this:

|<empty>
hidden|<hidden>
<front>|<front>

So all of those should render with the brackets in place, only a single line with brackets and no pipe would be considered a group. What do you think?

deviantintegral’s picture

I like the brackets. I'll start hacking and will post a patch soon.

--Andrew

deviantintegral’s picture

StatusFileSize
new3.31 KB

Here's a version which uses <Group> syntax. Unfortunately, the only place brackets are allowed is for group names; in every other place, they are filtered for XSS in _webform_filter_values(). All of the calls could be replaced with ones which don't filter for XSS, but filtering seems like a good idea to me where we can. I don't think anything is exposed by not filtering the group tag, and I wasn't able to get any suspicious situations as we watch for the closing bracket on the preg_match.

This also breaks the array flatten out into it's own function.

--Andrew

quicksketch’s picture

Ah, well that's nice in a way. That means no one is currently using brackets because they get stripped out anyway :)

I'll review when I get a minute. Thanks for your patches!

quicksketch’s picture

Status: Needs review » Fixed

I rerolled for the latest CVS version, tidied up a little bit by consolidating _webform_flatten_options() into _webform_select_options(). I added a SimpleTest for this new feature also. I opened #324028: Add help text for using Optgroups in Select list after noticing that there's absolutely no description on how to actually use this feature outside of the inline PHP code.

deviantintegral’s picture

Awesome, thanks!

--Andrew

Chris Charlton’s picture

This is a feature I was thinking about coding up. Great to see OptGroups coming.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.