-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Readonly transformation of lazy-loaded searchable dropdown
- Loading branch information
johannes.hammersen
authored and
johannes.hammersen
committed
Jul 2, 2024
1 parent
3c97f8a
commit f23bea6
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms; | ||
|
||
use SilverStripe\Core\Convert; | ||
use SilverStripe\ORM\ArrayLib; | ||
use SilverStripe\ORM\DataObjectInterface; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use SilverStripe\ORM\ArrayList; | ||
use SilverStripe\ORM\DataList; | ||
|
||
/** | ||
* Read-only complement of {@link SearchableDropdownField} and {@link SearchableMultiDropdownField}. | ||
* | ||
* Shows the "human value" of the SearchableDropdownField for the currently selected | ||
* values. | ||
*/ | ||
class LazyLoadedLookupField extends LookupField | ||
{ | ||
private ?DataList $sourceList = null; | ||
|
||
/** | ||
* To retain compatibility with ancestor getSource() this returns an array of only the selected values | ||
*/ | ||
public function getSource(): array | ||
{ | ||
$values = $this->getValueArray(); | ||
if (empty($values) || $this->sourceList === null) { | ||
$selectedValuesList = ArrayList::create(); | ||
} else { | ||
$selectedValuesList = $this->sourceList->filterAny(['ID' => $values]); | ||
} | ||
return $this->getListMap($selectedValuesList); | ||
} | ||
|
||
/* | ||
* @param mixed $source | ||
*/ | ||
public function setSource($source): static | ||
{ | ||
// Setting to $this->sourceList instead of $this->source because SelectField.source | ||
// docblock type is array|ArrayAccess i.e. does not allow DataList | ||
|
||
if ($source instanceof DataList) { | ||
$this->sourceList = $source; | ||
} else { | ||
$this->sourceList = null; | ||
} | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters