Skip to content

Commit b0525ef

Browse files
committed
API Add getSourceList() method to searchable dropdowns
This can be useful for checking the current class the dropdown holds, similar to `getSourceObject()` on a tree dropdown
1 parent edb828f commit b0525ef

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Forms/SearchableDropdownTrait.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public function getSearchContext(): ?SearchContext
182182
if ($this->searchContext) {
183183
return $this->searchContext;
184184
}
185-
if ($this->sourceList) {
186-
$dataClass = $this->sourceList->dataClass();
185+
if ($this->getSourceList()) {
186+
$dataClass = $this->getSourceList()->dataClass();
187187
/** @var DataObject $obj */
188188
$obj = $dataClass::create();
189189
return $obj->getDefaultSearchContext();
@@ -265,7 +265,7 @@ public function setIsSearchable(bool $isSearchable): static
265265
*/
266266
public function getSource(): array
267267
{
268-
return $this->getListMap($this->sourceList);
268+
return $this->getListMap($this->getSourceList());
269269
}
270270

271271
public function Field($properties = [])
@@ -286,6 +286,15 @@ public function setSource($source): static
286286
return $this;
287287
}
288288

289+
/**
290+
* Get the underlying list that represents the source for this dropdown.
291+
* To set, use setSource().
292+
*/
293+
public function getSourceList(): ?DataList
294+
{
295+
return $this->sourceList;
296+
}
297+
289298
/**
290299
* Get the field to use for the label of the option
291300
* This field is also used for searching if that field exists in the database and search context is not being used
@@ -499,10 +508,10 @@ private function getDefaultSchemaValue()
499508

500509
private function getOptionsForSearchRequest(string $term): array
501510
{
502-
if (!$this->sourceList) {
511+
if (!$this->getSourceList()) {
503512
return [];
504513
}
505-
$dataClass = $this->sourceList->dataClass();
514+
$dataClass = $this->getSourceList()->dataClass();
506515
$labelField = $this->getLabelField();
507516
/** @var DataObject $obj */
508517
$obj = $dataClass::create();
@@ -530,14 +539,14 @@ private function getOptionsForSearchRequest(string $term): array
530539
private function getOptionsForSchema(bool $onlySelected = false): ArrayList
531540
{
532541
$options = ArrayList::create();
533-
if (!$this->sourceList) {
542+
if (!$this->getSourceList()) {
534543
return $options;
535544
}
536545
$values = $this->getValueArray();
537546
if (empty($values)) {
538547
$selectedValuesList = ArrayList::create();
539548
} else {
540-
$selectedValuesList = $this->sourceList->filterAny(['ID' => $values]);
549+
$selectedValuesList = $this->getSourceList()->filterAny(['ID' => $values]);
541550
}
542551
// SearchableDropdownField will have the getHasEmptyDefault() method from SingleSelectField
543552
// Note that SingleSelectField::getSourceEmpty() will not be called for the react-select component
@@ -552,7 +561,7 @@ private function getOptionsForSchema(bool $onlySelected = false): ArrayList
552561
if ($onlySelected) {
553562
$options = $this->updateOptionsForSchema($options, $selectedValuesList, $selectedValuesList);
554563
} else {
555-
$options = $this->updateOptionsForSchema($options, $this->sourceList, $selectedValuesList);
564+
$options = $this->updateOptionsForSchema($options, $this->getSourceList(), $selectedValuesList);
556565
}
557566
return $options;
558567
}
@@ -609,14 +618,14 @@ public function performReadonlyTransformation()
609618
// call to $this->getSource() which will load the entire DataList into memory which
610619
// causes issues with very large datasets and isn't needed when the field is read-only
611620
$field = call_user_func('SilverStripe\\Forms\\FormField::castedCopy', SearchableLookupField::class);
612-
$field->setSource($this->sourceList);
621+
$field->setSource($this->getSourceList());
613622
$field->setReadonly(true);
614623

615624
return $field;
616625
}
617626

618627
protected function getSourceValues()
619628
{
620-
return $this->sourceList->sort(null)->column('ID');
629+
return $this->getSourceList()->sort(null)->column('ID');
621630
}
622631
}

0 commit comments

Comments
 (0)