This is a little hard to troubleshoot since there is no documentation on the subject that I could find. It was my understanding that when creating group lists with CCK that the HTML group opt could be set using one of the following methods.

In allowed values list:

<Most Common>
Australia | Australia
Canada | Canada
Ireland | Ireland
NewZealand | New Zealand
SouthAfrica | South Africa
UnitedKingdom | United Kingdom
USA | United States of America

In PHP code:

$array['South-America']['Argentina'] = "Argentina";
$array['South-America']['Bolivia'] = "Bolivia";
$array['South-America']['Brazil'] = "Brazil";
$array['South-America']['Chile'] = "Chile";
$array['South-America']['Colombia'] = "Colombia";
$array['South-America']['Ecuador'] = "Ecuador";
$array['South-America']['FrenchGuiana'] = "French Guiana";
$array['South-America']['Guyana'] = "Guyana";
$array['South-America']['Paraguay'] = "Paraguay";
$array['South-America']['Peru'] = "Peru";
$array['South-America']['Suriname'] = "Suriname";
$array['South-America']['Uruguay'] = "Uruguay";
$array['South-America']['Venezuela'] = "Venezuela";

return $array;

Neither of these methods work. Is there an undocumented solution, missing feature or is this a bug?

CommentFileSizeAuthor
#10 optgroup.patch1.29 KBarski
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KarenS’s picture

Status: Active » Fixed

CCK does not handle list creation in D7, that is handled by core. I don't know if core will handle that or not. At any rate, it is not a CCK bug, it is probably a core support question.

carlmcdade’s picture

Status: Fixed » Active

So the fact that this is directed towards "CCK in core" note the keyword "core" is incorrect? Please do not mark it as fixed without giving a complete answer to the issue.

KarenS’s picture

Status: Active » Fixed

There is nothing else I can say, lists are handled by core, not CCK. The component 'CCK in core' was for issues related to moving CCK to core, which has already been done, not for providing core support.

Status: Fixed » Closed (fixed)

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

marcvangend’s picture

Karen, sorry for coming back to an old and closed issue, but it seems to me that CCK can make optgroups in select lists possible.

I just ran a quick test with a list field, using CCK to enter the allowed values as a nested PHP array like this:

return array(
  'OptGroup A' => array(
    1 => 'Option 1',
    2 => 'Option 2',
  ),
  'OptGroup B' => array(
    3 => 'Option 3',
    4 => 'Option 4',
  ),
  'OptGroup C' => array(
    5 => 'Option 5',
    6 => 'Option 6',
    7 => 'Option 7',
  ),
);

As it turns out, if I set the $flatten variable to false in cck_allowed_values_php() when the form is built, the list field renders the optgroups perfectly.

Would you consider committing a patch for this feature?

arski’s picture

Component: CCK in core » General
Status: Closed (fixed) » Active

This is by no means fixed and never was. Maybe the OP posted this in the wrong component (or the wrong module), but then the issue should be reassigned appropriately, and not closed.

@marcvangend: thanks for the spot - do you know of any way to make this work without editing the CCK code? I wonder if one can set something like $field_value['settings']['allowed_values_function_parameters']. Either way a patch would be much appreciated!

Cheers

arski’s picture

Hmm, by the way, have you tested creating a node with flatten=FALSE ? I seem to be getting an error message that my optgroup select field is an "illegal value". Or is that just me?

marcvangend’s picture

Re #7: I know that error, it has to do with the way the Drupal Form API handles nested form elements. I have been testing this with a debugger, so I could set $flatten to FALSE or TRUE whenever I wanted to. If you do not flatten the array when building the form, but you do flatten when validating, it works. Once I figured that out, I stopped my research and posted #5 :-)

arski’s picture

ooh.. so how do you make that happen, to flatten on build but not on validate? (without a debugger) For now, I would be happy to even edit the CCK code as I'm building a prototype, hoping that this will get patched up later.

Cheers

arski’s picture

Project: Content Construction Kit (CCK) » Drupal core
Version: 7.x-2.x-dev » 7.x-dev
Component: General » field system
Status: Active » Needs review
FileSize
1.29 KB

Hmm, following your comments marcvangend, I've created a patch to the list.module file inside the core's Field module that does exactly that - flatten when validating, and not when not. It seems that the $flatten parameter is otherwise never used by core.. however, I'm not sure if there are other default_values functions that don't $flatten in them at all and might hence break because of this..

Damien Tournoud’s picture

Title: D7- CCK optgroup tag in Select lists does not work or is missing » Support grouping in allowed values
Version: 7.x-dev » 8.x-dev
Category: bug » feature
Status: Needs review » Needs work

Triaging. This is actually a feature request, as grouping in allowed values has never been a feature of the list module. Let's see what we can do for Drupal 8.

arski’s picture

mmh, ignore that patch, it's actually quite pointless. Trying to find out some other way to make it work..

And, seriously? With all the new and "fancy" features of D7, something as simple and basic as optgroups wasn't even ever considered? I don't know if this should be directed at the list module or the CCK module that used to do this, but that just can't be serious imo - not being able to support some of the most common html tags has to be considered a bug (again, I don't know enough to argue it's a bug in this module, but of some part of Drupal for sure).

arski’s picture

OK, doing all of this through hook_form_alter now. I still find it quite lame that D7 can't properly support optgroups neither out of the box nor with the help of CCK.

marcvangend’s picture

Category: feature » bug

I beg to differ with #11. Optgroups have been a feature of CCK for a long time. In 2009, CCK maintainer marcus_petrux closed #583880-1: Select fields with group names? as fixed, saying "Support for optgroups was recently added to CCK. However, you need to provide the list of allowed values using PHP code."

I'm not saying that this must be in core, but it definitely is a regression, ie. a bug. I think that Drupal 7.x and CCK 7.x have a shared responsibility to help this get fixed. Let's show arski some support here.

yched’s picture

There was support for this in CCK, through the use of "custom php code providing a (nested) array of allowed values".

When moving to "fields in core", it was decided that CCK's various "custom php code in a textarea" features were out of scope for core. We made sure that the functionality would still able to be added from contrib. The code in options.module (select widgets) does support the case of field types returning a nested array in their hook_options_list().

So what is missing is the possibility specify optgroups in List allowed values:
- either through custom php code : the supporting code has been left in the D7 branch of cck.module - it's now the only code in there, and has been pretty much abandoned since then. It should be moved to a dedicated contrib project, if anyone is up to it. Neither Karen nor I intend to maintain it, but, admittedly, we did fail doing a proper job of communicating on this to find volunteers to take over :-/
- either through the existing "one value per line" textarea, with some additional custom syntax. That would definitely qualify as a feature request, though.

jibran’s picture

Title: Support grouping in allowed values » Support grouping in allowed values for select list
Category: bug » feature
jibran’s picture

Simon Georges’s picture

carlmcdade’s picture

When you close or designate an issue as being duplicated then take priority with the oldest known occurance which is the other thread. You can see this easily by either looking at the date or the node id of the issue.

Arbitrarily closing issues without showing the age and switching Drupal versions is more confusing than helpful.

Simon Georges’s picture

As both other issues had working patches and were already linked between each other, I thought it was better to have them staying open instead of this one, forcing people to redo the patches to have them here. I apologize if it was not the proper action to do here. Should I reopen this one and close the other ones?