diff --git a/src/Forms/SearchableDropdownTrait.php b/src/Forms/SearchableDropdownTrait.php index 5931912f9d2..c692eaffe28 100644 --- a/src/Forms/SearchableDropdownTrait.php +++ b/src/Forms/SearchableDropdownTrait.php @@ -262,6 +262,10 @@ public function setIsSearchable(bool $isSearchable): static */ public function getSource(): array { + // Source will be unused when lazy-loading so just return an empty array + if ($this->getIsLazyLoaded()) { + return []; + } return $this->getListMap($this->sourceList); } diff --git a/tests/php/Forms/SearchableDropdownTraitTest.php b/tests/php/Forms/SearchableDropdownTraitTest.php index d6f3aae4cd4..4a2ad9ca815 100644 --- a/tests/php/Forms/SearchableDropdownTraitTest.php +++ b/tests/php/Forms/SearchableDropdownTraitTest.php @@ -217,4 +217,28 @@ public function testGetSchemaDataDefaults(): void $this->assertSame('My placeholder', $schema['placeholder']); $this->assertFalse($schema['searchable']); } + + public function provideGetSource(): array + { + return [ + 'lazyLoad' => [ + 'isLazyLoaded' => true, + 'expected' => 0, + ], + 'not lazyLoad' => [ + 'isLazyLoaded' => false, + 'expected' => 3, + ], + ]; + } + + /** + * @dataProvider provideGetSource + */ + public function testGetSource(bool $isLazyLoaded, int $expected): void + { + $field = new SearchableDropdownField('MyField', 'MyField', Team::get()); + $field->setIsLazyLoaded($isLazyLoaded); + $this->assertCount($expected, $field->getSource()); + } }