mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX: Add support for multi dimensional source arrays in LookupField (open/6132)
This commit is contained in:
parent
718108969b
commit
42cf2a95bd
@ -1,30 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read-only complement of {@link DropdownField}.
|
* Read-only complement of {@link DropdownField}.
|
||||||
* Shows the "human value" of the dropdown field for the currently selected value.
|
*
|
||||||
|
* Shows the "human value" of the dropdown field for the currently selected
|
||||||
|
* value.
|
||||||
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
* @subpackage fields-basic
|
* @subpackage fields-basic
|
||||||
*/
|
*/
|
||||||
class LookupField extends DropdownField {
|
class LookupField extends DropdownField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var boolean $readonly
|
||||||
|
*/
|
||||||
protected $readonly = true;
|
protected $readonly = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a readonly span containing the correct value.
|
* Returns a readonly span containing the correct value.
|
||||||
|
*
|
||||||
|
* @param array $properties
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function Field($properties = array()) {
|
public function Field($properties = array()) {
|
||||||
$source = $this->getSource();
|
$source = $this->getSource();
|
||||||
|
|
||||||
|
|
||||||
// Normalize value to array to simplify further processing
|
// Normalize value to array to simplify further processing
|
||||||
$values = (is_array($this->value) || is_object($this->value)) ? $this->value : array(trim($this->value));
|
if(is_array($this->value) || is_object($this->value)) {
|
||||||
|
$values = $this->value;
|
||||||
|
} else {
|
||||||
|
$values = array(trim($this->value));
|
||||||
|
}
|
||||||
|
|
||||||
$mapped = array();
|
$mapped = array();
|
||||||
|
|
||||||
if($source instanceof SQLMap) {
|
if($source instanceof SQLMap) {
|
||||||
foreach($values as $value) $mapped[] = $source->getItem($value);
|
|
||||||
} else if($source instanceof ArrayAccess || is_array($source)) {
|
|
||||||
foreach($values as $value) {
|
foreach($values as $value) {
|
||||||
if(isset($source[$value])) $mapped[] = $source[$value];
|
$mapped[] = $source->getItem($value);
|
||||||
|
}
|
||||||
|
} else if($source instanceof ArrayAccess || is_array($source)) {
|
||||||
|
$source = ArrayLib::flatten($source);
|
||||||
|
|
||||||
|
foreach($values as $value) {
|
||||||
|
if(isset($source[$value])) {
|
||||||
|
$mapped[] = $source[$value];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$mapped = array();
|
$mapped = array();
|
||||||
@ -39,7 +60,11 @@ class LookupField extends DropdownField {
|
|||||||
|
|
||||||
if($mapped) {
|
if($mapped) {
|
||||||
$attrValue = implode(', ', array_values($mapped));
|
$attrValue = implode(', ', array_values($mapped));
|
||||||
if(!$this->dontEscape) $attrValue = Convert::raw2xml($attrValue);
|
|
||||||
|
if(!$this->dontEscape) {
|
||||||
|
$attrValue = Convert::raw2xml($attrValue);
|
||||||
|
}
|
||||||
|
|
||||||
$inputValue = implode(', ', array_values($values));
|
$inputValue = implode(', ', array_values($values));
|
||||||
} else {
|
} else {
|
||||||
$attrValue = "<i>(none)</i>";
|
$attrValue = "<i>(none)</i>";
|
||||||
@ -51,17 +76,26 @@ class LookupField extends DropdownField {
|
|||||||
"\" value=\"" . $inputValue . "\" />";
|
"\" value=\"" . $inputValue . "\" />";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return LookupField
|
||||||
|
*/
|
||||||
public function performReadonlyTransformation() {
|
public function performReadonlyTransformation() {
|
||||||
$clone = clone $this;
|
$clone = clone $this;
|
||||||
|
|
||||||
return $clone;
|
return $clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function Type() {
|
public function Type() {
|
||||||
return "lookup readonly";
|
return "lookup readonly";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override parent behaviour by not merging arrays.
|
* Override parent behavior by not merging arrays.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getSource() {
|
public function getSource() {
|
||||||
return $this->source;
|
return $this->source;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @package framework
|
* @package framework
|
||||||
* @subpackage tests
|
* @subpackage tests
|
||||||
@ -79,5 +80,35 @@ class LookupFieldTest extends SapphireTest {
|
|||||||
$f->Field()
|
$f->Field()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWithMultiDimensionalSource() {
|
||||||
|
$choices = array(
|
||||||
|
"Non-vegetarian" => array(
|
||||||
|
0 => 'Carnivore',
|
||||||
|
),
|
||||||
|
"Vegetarian" => array(
|
||||||
|
3 => 'Carrots',
|
||||||
|
),
|
||||||
|
"Other" => array(
|
||||||
|
9 => 'Vegan'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$f = new LookupField('test', 'test', $choices);
|
||||||
|
$f->setValue(3);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<span class="readonly" id="test">Carrots</span><input type="hidden" name="test" value="3" />',
|
||||||
|
$f->Field()
|
||||||
|
);
|
||||||
|
|
||||||
|
$f->setValue(array(
|
||||||
|
3, 9
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<span class="readonly" id="test">Carrots, Vegan</span><input type="hidden" name="test" value="3, 9" />',
|
||||||
|
$f->Field()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user