From 6e10acf6cd61954bdfda4fe5936beb369a5fce1a Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Wed, 16 Nov 2016 17:04:54 -0600 Subject: [PATCH] ENHANCEMENT setEmptyString option on EditableDropdown closes #17 --- .../editableformfields/EditableDropdown.php | 26 +++++++++++- tests/EditableDropdownTest.php | 42 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/EditableDropdownTest.php diff --git a/code/model/editableformfields/EditableDropdown.php b/code/model/editableformfields/EditableDropdown.php index a576c1a..195bd4a 100755 --- a/code/model/editableformfields/EditableDropdown.php +++ b/code/model/editableformfields/EditableDropdown.php @@ -1,12 +1,15 @@ 'Boolean', + 'EmptyString' => 'Varchar(255)', + ); + /** * @return FieldList */ @@ -21,6 +29,18 @@ class EditableDropdown extends EditableMultipleOptionField { $fields = parent::getCMSFields(); + $fields->addFieldToTab( + 'Root.Main', + CheckboxField::create('UseEmptyString') + ->setTitle('Set default empty string') + ); + + $fields->addFieldToTab( + 'Root.Main', + TextField::create('EmptyString') + ->setTitle('Empty String') + ); + $fields->removeByName('Default'); return $fields; @@ -35,6 +55,10 @@ class EditableDropdown extends EditableMultipleOptionField ->setFieldHolderTemplate('UserFormsField_holder') ->setTemplate('UserFormsDropdownField'); + if ($this->UseEmptyString) { + $field->setEmptyString(($this->EmptyString) ? $this->EmptyString : ''); + } + // Set default $defaultOption = $this->getDefaultOptions()->first(); if ($defaultOption) { diff --git a/tests/EditableDropdownTest.php b/tests/EditableDropdownTest.php new file mode 100644 index 0000000..1562821 --- /dev/null +++ b/tests/EditableDropdownTest.php @@ -0,0 +1,42 @@ +filter('UseEmptyString', true)->first()) { + $dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); + + $dropdown->UseEmptyString = true; + $dropdown->EmptyString = 'My Default Empty String'; + $dropdown->write(); + } + + $field = $dropdown->getFormField(); + $this->assertEquals($field->getEmptyString(), 'My Default Empty String'); + + $alternateDropdown = $this->objFromFixture('EditableDropdown', 'department-dropdown'); + $formField = $alternateDropdown->getFormField(); + $this->assertFalse($formField->getHasEmptyDefault()); + + $alternateDropdown->UseEmptyString = true; + $alternateDropdown->write(); + $this->assertEquals($formField->getEmptyString(), ''); + + } + +}