mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #6677 from open-sausages/pulls/3/themed-grouped-dropdown
API Enable theming of GroupedDropdownField
This commit is contained in:
commit
a69ec2f847
@ -56,32 +56,52 @@
|
|||||||
class GroupedDropdownField extends DropdownField {
|
class GroupedDropdownField extends DropdownField {
|
||||||
|
|
||||||
public function Field($properties = array()) {
|
public function Field($properties = array()) {
|
||||||
$options = '';
|
$options = array();
|
||||||
foreach($this->getSource() as $value => $title) {
|
foreach($this->getSource() as $valueOrGroupTitle => $titleOrGroup) {
|
||||||
if(is_array($title)) {
|
// Option group
|
||||||
$options .= "<optgroup label=\"$value\">";
|
if(is_array($titleOrGroup)) {
|
||||||
foreach($title as $value2 => $title2) {
|
$groupOptions = array();
|
||||||
$disabled = '';
|
foreach($titleOrGroup as $optionValue => $optionTitle) {
|
||||||
if( array_key_exists($value, $this->disabledItems)
|
// Check sub-option disabled status
|
||||||
&& is_array($this->disabledItems[$value])
|
$disabled = false;
|
||||||
&& in_array($value2, $this->disabledItems[$value]) ){
|
if( array_key_exists($valueOrGroupTitle, $this->disabledItems)
|
||||||
$disabled = 'disabled="disabled"';
|
&& is_array($this->disabledItems[$valueOrGroupTitle])
|
||||||
|
&& in_array($optionValue, $this->disabledItems[$valueOrGroupTitle]) ){
|
||||||
|
$disabled = true;
|
||||||
}
|
}
|
||||||
$selected = $value2 == $this->value ? " selected=\"selected\"" : "";
|
$groupOptions[] = array(
|
||||||
$options .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
|
'Value' => $optionValue,
|
||||||
|
'Disabled' => $disabled,
|
||||||
|
'Title' => $optionTitle,
|
||||||
|
'Selected' => $optionValue == $this->value
|
||||||
|
);
|
||||||
}
|
}
|
||||||
$options .= "</optgroup>";
|
|
||||||
} else { // Fall back to the standard dropdown field
|
$options[] = array(
|
||||||
$disabled = '';
|
'Title' => $valueOrGroupTitle,
|
||||||
if( in_array($value, $this->disabledItems) ){
|
'Options' => new ArrayList($groupOptions),
|
||||||
$disabled = 'disabled="disabled"';
|
);
|
||||||
}
|
} else {
|
||||||
$selected = $value == $this->value ? " selected=\"selected\"" : "";
|
// Single option
|
||||||
$options .= "<option$selected value=\"$value\" $disabled>$title</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() {
|
public function Type() {
|
||||||
|
12
templates/Includes/GroupedDropdownFieldOption.ss
Normal file
12
templates/Includes/GroupedDropdownFieldOption.ss
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<% if $Options %>
|
||||||
|
<optgroup label="$Title.ATT">
|
||||||
|
<% loop $Options %>
|
||||||
|
<% include GroupedDropdownFieldOption %>
|
||||||
|
<% end_loop %>
|
||||||
|
</optgroup>
|
||||||
|
<% else %>
|
||||||
|
<option value="$Value.ATT"
|
||||||
|
<% if $Selected %> selected="selected"<% end_if %>
|
||||||
|
<% if $Disabled %> disabled="disabled"<% end_if %>
|
||||||
|
><% if $Title %>$Title.XML<% else %> <% end_if %></option>
|
||||||
|
<% end_if %>
|
5
templates/forms/GroupedDropdownField.ss
Normal file
5
templates/forms/GroupedDropdownField.ss
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<select $AttributesHTML>
|
||||||
|
<% loop $Options %>
|
||||||
|
<% include GroupedDropdownFieldOption %>
|
||||||
|
<% end_loop %>
|
||||||
|
</select>
|
@ -51,4 +51,22 @@ class GroupedDropdownFieldTest extends SapphireTest {
|
|||||||
$this->assertFalse($field->validate($validator));
|
$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('<option value="1"', $body);
|
||||||
|
$this->assertContains('<optgroup label="Group One">', $body);
|
||||||
|
$this->assertContains('<option value="3"', $body);
|
||||||
|
$this->assertContains('>Three</option>', $body);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user