Merge branch '4.7' into 4

This commit is contained in:
Maxime Rainville 2021-01-20 12:57:01 +13:00
commit 54bdabd203
4 changed files with 35 additions and 1 deletions

View File

@ -812,10 +812,13 @@ class TreeDropdownField extends FormField
* Get the object where the $keyField is equal to a certain value * Get the object where the $keyField is equal to a certain value
* *
* @param string|int $key * @param string|int $key
* @return DataObject * @return DataObject|null
*/ */
protected function objectForKey($key) protected function objectForKey($key)
{ {
if (!is_string($key) && !is_int($key)) {
return null;
}
return DataObject::get($this->getSourceObject()) return DataObject::get($this->getSourceObject())
->filter($this->getKeyField(), $key) ->filter($this->getKeyField(), $key)
->first(); ->first();

View File

@ -149,12 +149,16 @@ class TreeMultiselectField extends TreeDropdownField
// Parse ids from value string / array // Parse ids from value string / array
$ids = []; $ids = [];
if (is_string($value)) { if (is_string($value)) {
$ids = preg_split("#\s*,\s*#", trim($value)); $ids = preg_split("#\s*,\s*#", trim($value));
} elseif (is_array($value)) { } elseif (is_array($value)) {
$ids = array_values($value); $ids = array_values($value);
} }
// Filter out empty strings
$ids = array_filter($ids);
// No value // No value
if (empty($ids)) { if (empty($ids)) {
return ArrayList::create(); return ArrayList::create();

View File

@ -8,7 +8,10 @@ use SilverStripe\Control\Session;
use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TreeDropdownField; use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Tests\HierarchyTest\TestObject; use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
class TreeDropdownFieldTest extends SapphireTest class TreeDropdownFieldTest extends SapphireTest
@ -246,4 +249,21 @@ class TreeDropdownFieldTest extends SapphireTest
$result $result
); );
} }
/**
* This is to test setting $key to an Object in the protected function objectForKey()
* This is to fix an issue where postgres will not fail gracefully when you do this
*/
public function testObjectForKeyObjectValue()
{
$form = Form::create();
$fieldList = FieldList::create();
$field = TreeDropdownField::create('TestTree', 'Test tree', File::class);
$fieldList->add($field);
$form->setFields($fieldList);
$field->setValue(DataObject::create());
// The following previously errored in postgres
$field->getSchemaStateDefaults();
$this->assertTrue(true);
}
} }

View File

@ -340,5 +340,12 @@ class TreeMultiselectFieldTest extends SapphireTest
[], [],
$field->getItems() $field->getItems()
); );
// Andle empty string none value
$field->setValue('');
$this->assertListEquals(
[],
$field->getItems()
);
} }
} }