diff --git a/forms/GroupedDropdownField.php b/forms/GroupedDropdownField.php index 6e24a5012..2d30956e5 100644 --- a/forms/GroupedDropdownField.php +++ b/forms/GroupedDropdownField.php @@ -56,32 +56,52 @@ class GroupedDropdownField extends DropdownField { public function Field($properties = array()) { - $options = ''; - foreach($this->getSource() as $value => $title) { - if(is_array($title)) { - $options .= ""; - foreach($title as $value2 => $title2) { - $disabled = ''; - if( array_key_exists($value, $this->disabledItems) - && is_array($this->disabledItems[$value]) - && in_array($value2, $this->disabledItems[$value]) ){ - $disabled = 'disabled="disabled"'; + $options = array(); + foreach($this->getSource() as $valueOrGroupTitle => $titleOrGroup) { + // Option group + if(is_array($titleOrGroup)) { + $groupOptions = array(); + foreach($titleOrGroup as $optionValue => $optionTitle) { + // Check sub-option disabled status + $disabled = false; + if( array_key_exists($valueOrGroupTitle, $this->disabledItems) + && is_array($this->disabledItems[$valueOrGroupTitle]) + && in_array($optionValue, $this->disabledItems[$valueOrGroupTitle]) ){ + $disabled = true; } - $selected = $value2 == $this->value ? " selected=\"selected\"" : ""; - $options .= "$title2"; + $groupOptions[] = array( + 'Value' => $optionValue, + 'Disabled' => $disabled, + 'Title' => $optionTitle, + 'Selected' => $optionValue == $this->value + ); } - $options .= ""; - } else { // Fall back to the standard dropdown field - $disabled = ''; - if( in_array($value, $this->disabledItems) ){ - $disabled = 'disabled="disabled"'; - } - $selected = $value == $this->value ? " selected=\"selected\"" : ""; - $options .= "$title"; + + $options[] = array( + 'Title' => $valueOrGroupTitle, + 'Options' => new ArrayList($groupOptions), + ); + } else { + // Single option + $disabled = in_array($valueOrGroupTitle, $this->disabledItems); + + $options[] = array( + 'Value' => $valueOrGroupTitle, + 'Disabled' => $disabled, + 'Title' => $titleOrGroup, + 'Selected' => $valueOrGroupTitle == $this->value + ); } } - return FormField::create_tag('select', $this->getAttributes(), $options); + // Render + $this->extend('onBeforeRender', $this); + $properties = array_merge($properties, array( + 'Options' => new ArrayList($options) + )); + return $this + ->customise($properties) + ->renderWith($this->getTemplates()); } public function Type() { diff --git a/templates/Includes/GroupedDropdownFieldOption.ss b/templates/Includes/GroupedDropdownFieldOption.ss new file mode 100644 index 000000000..69a34cd39 --- /dev/null +++ b/templates/Includes/GroupedDropdownFieldOption.ss @@ -0,0 +1,12 @@ +<% if $Options %> + + <% loop $Options %> + <% include GroupedDropdownFieldOption %> + <% end_loop %> + +<% else %> + +<% end_if %> diff --git a/templates/forms/GroupedDropdownField.ss b/templates/forms/GroupedDropdownField.ss new file mode 100644 index 000000000..827f362f2 --- /dev/null +++ b/templates/forms/GroupedDropdownField.ss @@ -0,0 +1,5 @@ + diff --git a/tests/forms/GroupedDropdownFieldTest.php b/tests/forms/GroupedDropdownFieldTest.php index e15b623b4..fa7701bc5 100644 --- a/tests/forms/GroupedDropdownFieldTest.php +++ b/tests/forms/GroupedDropdownFieldTest.php @@ -51,4 +51,22 @@ class GroupedDropdownFieldTest extends SapphireTest { $this->assertFalse($field->validate($validator)); } + public function testRendering() { + $field = GroupedDropdownField::create('Test', 'Testing', array( + "1" => "One", + "Group One" => array( + "2" => "Two", + "3" => "Three" + ), + "Group Two" => array( + "4" => "Four" + ) + )); + $body= $field->Field()->forTemplate(); + $this->assertContains('', $body); + $this->assertContains('', $body); + } + }