silverstripe-framework/forms/GroupedDropdownField.php
Hayden Smith 4a5d9b03f8 Moved Sapphire module to open source path
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@39001 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-07-19 10:40:28 +00:00

30 lines
1023 B
PHP
Executable File

<?php
class GroupedDropdownField extends DropdownField {
/**
* @desc Returns a <select> tag containing all the appropriate <option> tags, with <optgroup> tags around the <option> tags as required
*/
function Field() {
if($extraClass = trim($this->extraClass())) {
$classAttr = "class=\"$extraClass\"";
}
foreach($this->source as $value => $title) {
if(is_array($title)) {
$options .= "<optgroup label=\"$value\">";
foreach($title as $value2 => $title2) {
$selected = $value2 == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value2\">$title2</option>";
}
$options .= "</optgroup>";
} else { // Fall back to the standard dropdown field
$selected = $value == $this->value ? " selected=\"selected\"" : "";
$options .= "<option$selected value=\"$value\">$title</option>";
}
}
$id = $this->id();
return "<select $classAttr name=\"$this->name\" id=\"$id\">$options</select>";
}
}
?>