diff --git a/src/Forms/LazyLoadedLookupField.php b/src/Forms/LazyLoadedLookupField.php new file mode 100644 index 00000000000..6e6d32e4920 --- /dev/null +++ b/src/Forms/LazyLoadedLookupField.php @@ -0,0 +1,51 @@ +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; + } +} diff --git a/src/Forms/SearchableDropdownTrait.php b/src/Forms/SearchableDropdownTrait.php index 97923f711d0..81caccb2b8f 100644 --- a/src/Forms/SearchableDropdownTrait.php +++ b/src/Forms/SearchableDropdownTrait.php @@ -586,4 +586,13 @@ private function updateOptionsForSchema( } return $options; } + + public function performReadonlyTransformation() + { + $field = FormField::castedCopy(LazyLoadedLookupField::class); + $field->setSource($this->sourceList); + $field->setReadonly(true); + + return $field; + } }