diff --git a/src/Fields/MultiValueCheckboxField.php b/src/Fields/MultiValueCheckboxField.php index ebe56fd..5f3afda 100755 --- a/src/Fields/MultiValueCheckboxField.php +++ b/src/Fields/MultiValueCheckboxField.php @@ -208,6 +208,25 @@ public function saveInto(DataObjectInterface $record) } } + /** + * Load the value from the dataobject into this field + * + * @param DataObject|DataObjectInterface $record + */ + public function loadFrom(DataObjectInterface $record) + { + $fieldName = $this->getName(); + if (empty($fieldName) || empty($record)) { + return; + } + + if ($record->hasField($fieldName)) { + $value = $record->$fieldName; + + parent::setValue($value); + } + } + /** * Return the CheckboxSetField value as a string * selected item keys. diff --git a/tests/MultiValueCheckboxFieldTest.php b/tests/MultiValueCheckboxFieldTest.php new file mode 100644 index 0000000..c0c292d --- /dev/null +++ b/tests/MultiValueCheckboxFieldTest.php @@ -0,0 +1,33 @@ +MVField = ['One', 'Two']; + $field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']); + $field->loadFrom($obj); + $this->assertEquals('One,Two', $field->dataValue()); + } + + public function testSetValue() + { + $field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']); + $field->setValue(['One', 'Two']); + $this->assertEquals('One,Two', $field->dataValue()); + $obj = new MultiValueFieldTest_DataObject(); + $obj->MVField = ['Three', 'Four']; + $field->setValue('', $obj); + $this->assertEquals('Three,Four', $field->dataValue()); + } +}