diff --git a/forms/LookupField.php b/forms/LookupField.php index 82a5d52ea..17b8427ab 100644 --- a/forms/LookupField.php +++ b/forms/LookupField.php @@ -1,30 +1,51 @@ getSource(); - // 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(); + if($source instanceof SQLMap) { - foreach($values as $value) $mapped[] = $source->getItem($value); - } else if($source instanceof ArrayAccess || is_array($source)) { 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 { $mapped = array(); @@ -39,7 +60,11 @@ class LookupField extends DropdownField { if($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)); } else { $attrValue = "(none)"; @@ -51,17 +76,26 @@ class LookupField extends DropdownField { "\" value=\"" . $inputValue . "\" />"; } + /** + * @return LookupField + */ public function performReadonlyTransformation() { $clone = clone $this; + return $clone; } + /** + * @return string + */ public function Type() { return "lookup readonly"; } /** - * Override parent behaviour by not merging arrays. + * Override parent behavior by not merging arrays. + * + * @return array */ public function getSource() { return $this->source; diff --git a/tests/forms/LookupFieldTest.php b/tests/forms/LookupFieldTest.php index 2e81ad8e1..9884cd7bc 100644 --- a/tests/forms/LookupFieldTest.php +++ b/tests/forms/LookupFieldTest.php @@ -1,4 +1,5 @@ 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( + 'Carrots', + $f->Field() + ); + + $f->setValue(array( + 3, 9 + )); + + $this->assertEquals( + 'Carrots, Vegan', + $f->Field() + ); + } }