Skip to content

Commit 42cf2a9

Browse files
committed
FIX: Add support for multi dimensional source arrays in LookupField (open/6132)
1 parent 7181089 commit 42cf2a9

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

forms/LookupField.php

+41-7
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
<?php
2+
23
/**
34
* Read-only complement of {@link DropdownField}.
4-
* Shows the "human value" of the dropdown field for the currently selected value.
5+
*
6+
* Shows the "human value" of the dropdown field for the currently selected
7+
* value.
8+
*
59
* @package forms
610
* @subpackage fields-basic
711
*/
812
class LookupField extends DropdownField {
913

14+
/**
15+
* @var boolean $readonly
16+
*/
1017
protected $readonly = true;
1118

1219
/**
1320
* Returns a readonly span containing the correct value.
21+
*
22+
* @param array $properties
23+
*
24+
* @return string
1425
*/
1526
public function Field($properties = array()) {
1627
$source = $this->getSource();
1728

18-
1929
// Normalize value to array to simplify further processing
20-
$values = (is_array($this->value) || is_object($this->value)) ? $this->value : array(trim($this->value));
30+
if(is_array($this->value) || is_object($this->value)) {
31+
$values = $this->value;
32+
} else {
33+
$values = array(trim($this->value));
34+
}
2135

2236
$mapped = array();
37+
2338
if($source instanceof SQLMap) {
24-
foreach($values as $value) $mapped[] = $source->getItem($value);
39+
foreach($values as $value) {
40+
$mapped[] = $source->getItem($value);
41+
}
2542
} else if($source instanceof ArrayAccess || is_array($source)) {
43+
$source = ArrayLib::flatten($source);
44+
2645
foreach($values as $value) {
27-
if(isset($source[$value])) $mapped[] = $source[$value];
46+
if(isset($source[$value])) {
47+
$mapped[] = $source[$value];
48+
}
2849
}
2950
} else {
3051
$mapped = array();
@@ -39,7 +60,11 @@ public function Field($properties = array()) {
3960

4061
if($mapped) {
4162
$attrValue = implode(', ', array_values($mapped));
42-
if(!$this->dontEscape) $attrValue = Convert::raw2xml($attrValue);
63+
64+
if(!$this->dontEscape) {
65+
$attrValue = Convert::raw2xml($attrValue);
66+
}
67+
4368
$inputValue = implode(', ', array_values($values));
4469
} else {
4570
$attrValue = "<i>(none)</i>";
@@ -51,17 +76,26 @@ public function Field($properties = array()) {
5176
"\" value=\"" . $inputValue . "\" />";
5277
}
5378

79+
/**
80+
* @return LookupField
81+
*/
5482
public function performReadonlyTransformation() {
5583
$clone = clone $this;
84+
5685
return $clone;
5786
}
5887

88+
/**
89+
* @return string
90+
*/
5991
public function Type() {
6092
return "lookup readonly";
6193
}
6294

6395
/**
64-
* Override parent behaviour by not merging arrays.
96+
* Override parent behavior by not merging arrays.
97+
*
98+
* @return array
6599
*/
66100
public function getSource() {
67101
return $this->source;

tests/forms/LookupFieldTest.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @package framework
45
* @subpackage tests
@@ -79,5 +80,35 @@ public function testArrayValueWithSqlMapSource() {
7980
$f->Field()
8081
);
8182
}
82-
83+
84+
public function testWithMultiDimensionalSource() {
85+
$choices = array(
86+
"Non-vegetarian" => array(
87+
0 => 'Carnivore',
88+
),
89+
"Vegetarian" => array(
90+
3 => 'Carrots',
91+
),
92+
"Other" => array(
93+
9 => 'Vegan'
94+
)
95+
);
96+
97+
$f = new LookupField('test', 'test', $choices);
98+
$f->setValue(3);
99+
100+
$this->assertEquals(
101+
'<span class="readonly" id="test">Carrots</span><input type="hidden" name="test" value="3" />',
102+
$f->Field()
103+
);
104+
105+
$f->setValue(array(
106+
3, 9
107+
));
108+
109+
$this->assertEquals(
110+
'<span class="readonly" id="test">Carrots, Vegan</span><input type="hidden" name="test" value="3, 9" />',
111+
$f->Field()
112+
);
113+
}
83114
}

0 commit comments

Comments
 (0)