Merge pull request #6677 from open-sausages/pulls/3/themed-grouped-dropdown

API Enable theming of GroupedDropdownField
This commit is contained in:
Ingo Schommer 2017-03-06 14:48:29 +13:00 committed by GitHub
commit a69ec2f847
4 changed files with 76 additions and 21 deletions

View File

@ -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 .= "<optgroup label=\"$value\">";
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 .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
$groupOptions[] = array(
'Value' => $optionValue,
'Disabled' => $disabled,
'Title' => $optionTitle,
'Selected' => $optionValue == $this->value
);
}
$options .= "</optgroup>";
} 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 .= "<option$selected value=\"$value\" $disabled>$title</option>";
$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() {

View 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 %>&nbsp;<% end_if %></option>
<% end_if %>

View File

@ -0,0 +1,5 @@
<select $AttributesHTML>
<% loop $Options %>
<% include GroupedDropdownFieldOption %>
<% end_loop %>
</select>

View File

@ -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('<option value="1"', $body);
$this->assertContains('<optgroup label="Group One">', $body);
$this->assertContains('<option value="3"', $body);
$this->assertContains('>Three</option>', $body);
}
}