mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Added ListboxField->setDisabledItems() and setDefaultItems() (similar to CheckboxSetField API)
This commit is contained in:
parent
ea31e1798a
commit
5d565dcfa3
@ -41,6 +41,16 @@ class ListboxField extends DropdownField {
|
||||
*/
|
||||
protected $multiple = false;
|
||||
|
||||
/**
|
||||
* @var Array
|
||||
*/
|
||||
protected $disabledItems = array();
|
||||
|
||||
/**
|
||||
* @var Array
|
||||
*/
|
||||
protected $defaultItems = array();
|
||||
|
||||
/**
|
||||
* Creates a new dropdown field.
|
||||
*
|
||||
@ -61,43 +71,31 @@ class ListboxField extends DropdownField {
|
||||
* Returns a <select> tag containing all the appropriate <option> tags
|
||||
*/
|
||||
function Field($properties = array()) {
|
||||
if($this->multiple) {
|
||||
$this->name .= '[]';
|
||||
}
|
||||
|
||||
if($this->multiple) $this->name .= '[]';
|
||||
$options = array();
|
||||
|
||||
// We have an array of values
|
||||
if(is_array($this->value)){
|
||||
// Loop through and figure out which values were selected.
|
||||
|
||||
foreach($this->getSource() as $value => $title) {
|
||||
// Loop through the array of values to find out if this value is selected.
|
||||
$selected = "";
|
||||
foreach($this->value as $v){
|
||||
if($value == $v) {
|
||||
$selected = " selected=\"selected\"";
|
||||
break;
|
||||
}
|
||||
}
|
||||
$options[] = new ArrayData(array(
|
||||
'Title' => $title,
|
||||
'Value' => $value,
|
||||
'Selected' => $selected,
|
||||
'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)),
|
||||
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Listbox was based a singlular value, so treat it like a dropdown.
|
||||
foreach($this->getSource() as $value => $title) {
|
||||
$selected = $value == $this->value ? " selected=\"selected\"" : "";
|
||||
$options[] = new ArrayData(array(
|
||||
'Title' => $title,
|
||||
'Value' => $value,
|
||||
'Selected' => $selected,
|
||||
'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)),
|
||||
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
||||
return $this->customise($properties)->renderWith($this->getTemplate());
|
||||
}
|
||||
@ -214,7 +212,6 @@ class ListboxField extends DropdownField {
|
||||
|
||||
// Doesn't check against unknown values in order to allow for less rigid data handling.
|
||||
// They're silently ignored and overwritten the next time the field is saved.
|
||||
|
||||
parent::setValue($parts);
|
||||
} else {
|
||||
if(!in_array($val, array_keys($this->source))) {
|
||||
@ -233,4 +230,41 @@ class ListboxField extends DropdownField {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark certain elements as disabled,
|
||||
* regardless of the {@link setDisabled()} settings.
|
||||
*
|
||||
* @param array $items Collection of array keys, as defined in the $source array
|
||||
*/
|
||||
function setDisabledItems($items) {
|
||||
$this->disabledItems = $items;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Array
|
||||
*/
|
||||
function getDisabledItems() {
|
||||
return $this->disabledItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default selections, regardless of the {@link setValue()} settings.
|
||||
* Note: Items marked as disabled through {@link setDisabledItems()} can still be
|
||||
* selected by default through this method.
|
||||
*
|
||||
* @param Array $items Collection of array keys, as defined in the $source array
|
||||
*/
|
||||
function setDefaultItems($items) {
|
||||
$this->defaultItems = $items;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Array
|
||||
*/
|
||||
function getDefaultItems() {
|
||||
return $this->defaultItems;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
<select $AttributesHTML>
|
||||
<% control Options %>
|
||||
<option value="$Value"<% if Selected %> selected<% end_if %>>$Title</option>
|
||||
<option value="$Value"<% if Selected %> selected="selected"<% end_if %><% if Disabled %> disabled="disabled"<% end_if %>>$Title</option>
|
||||
<% end_control %>
|
||||
</select>
|
@ -28,6 +28,28 @@ class ListboxFieldTest extends SapphireTest {
|
||||
$this->assertNull($tag3xml[0]['selected']);
|
||||
}
|
||||
|
||||
function testFieldWithDisabledItems() {
|
||||
$articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
|
||||
$tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
|
||||
$tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
|
||||
$tag3 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag3');
|
||||
$field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
|
||||
$field->setMultiple(true);
|
||||
$field->setValue(null, $articleWithTags);
|
||||
$field->setDisabledItems(array($tag1->ID, $tag3->ID));
|
||||
|
||||
$p = new CSSContentParser($field->Field());
|
||||
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
|
||||
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
|
||||
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
|
||||
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
|
||||
$this->assertEquals('disabled', (string)$tag1xml[0]['disabled']);
|
||||
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
|
||||
$this->assertNull($tag2xml[0]['disabled']);
|
||||
$this->assertNull($tag3xml[0]['selected']);
|
||||
$this->assertEquals('disabled', (string)$tag3xml[0]['disabled']);
|
||||
}
|
||||
|
||||
function testSaveIntoNullValueWithMultipleOff() {
|
||||
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
|
||||
$field = new ListboxField('Choices', 'Choices', $choices);
|
||||
|
Loading…
Reference in New Issue
Block a user