diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 956f2aee44..113d1edd58 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -103,7 +103,11 @@ public function selectable(): bool public function selectableInForms(): bool { - return $this->selectableInForms ?: FieldtypeRepository::hasBeenMadeSelectableInForms($this->handle()); + if (FieldtypeRepository::selectableInFormIsOverriden($this->handle())) { + return FieldtypeRepository::hasBeenMadeSelectableInForms($this->handle()); + } + + return $this->selectableInForms; } public static function makeSelectableInForms() @@ -111,6 +115,11 @@ public static function makeSelectableInForms() FieldtypeRepository::makeSelectableInForms(self::handle()); } + public static function makeUnselectableInForms() + { + FieldtypeRepository::makeUnselectableInForms(self::handle()); + } + public function categories(): array { return $this->categories; diff --git a/src/Fields/FieldtypeRepository.php b/src/Fields/FieldtypeRepository.php index 6f8311ed05..0fe7eefa27 100644 --- a/src/Fields/FieldtypeRepository.php +++ b/src/Fields/FieldtypeRepository.php @@ -4,7 +4,7 @@ class FieldtypeRepository { - protected $madeSelectableInForms = []; + protected $selectableInForms = []; private $fieldtypes = []; public function preloadable() @@ -41,11 +41,21 @@ public function handles() public function makeSelectableInForms($handle) { - $this->madeSelectableInForms[] = $handle; + $this->selectableInForms[$handle] = true; + } + + public function makeUnselectableInForms($handle) + { + $this->selectableInForms[$handle] = false; } public function hasBeenMadeSelectableInForms($handle) { - return in_array($handle, $this->madeSelectableInForms); + return $this->selectableInForms[$handle] ?? false; + } + + public function selectableInFormIsOverriden($handle) + { + return array_key_exists($handle, $this->selectableInForms); } } diff --git a/tests/Fields/FieldtypeRepositoryTest.php b/tests/Fields/FieldtypeRepositoryTest.php index 85f5b5d8b3..5ea3ccaca6 100644 --- a/tests/Fields/FieldtypeRepositoryTest.php +++ b/tests/Fields/FieldtypeRepositoryTest.php @@ -62,6 +62,27 @@ public function it_throw_exception_when_finding_invalid_fieldtype() $this->expectExceptionMessage('Fieldtype [test] not found'); $this->repo->find('test'); } + + #[Test] + public function it_makes_fields_selectable_in_forms() + { + $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test-selectable')); + + $this->repo->makeSelectableInForms('test-selectable'); + $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test-selectable')); + $this->assertTrue($this->repo->selectableInFormIsOverriden('test-selectable')); + } + + #[Test] + public function it_makes_fields_unselectable_in_forms() + { + $this->repo->makeSelectableInForms('test-unselectable'); + $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test-unselectable')); + + $this->repo->makeUnselectableInForms('test-unselectable'); + $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test-unselectable')); + $this->assertTrue($this->repo->selectableInFormIsOverriden('test-unselectable')); + } } class FooFieldtype extends Fieldtype diff --git a/tests/Fields/FieldtypeTest.php b/tests/Fields/FieldtypeTest.php index 8ff3ca73e8..417ec8be91 100644 --- a/tests/Fields/FieldtypeTest.php +++ b/tests/Fields/FieldtypeTest.php @@ -555,16 +555,35 @@ public function it_can_make_a_fieldtype_selectable_in_forms() { $fieldtype = new class extends Fieldtype { - public static $handle = 'test'; + public static $handle = 'test-selectable'; + protected $selectableInForms = false; }; $this->assertFalse($fieldtype->selectableInForms()); - $this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); $fieldtype::makeSelectableInForms(); $this->assertTrue($fieldtype->selectableInForms()); - $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); + $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test-selectable')); + $this->assertTrue(FieldtypeRepository::selectableInFormIsOverriden('test-selectable')); + } + + #[Test] + public function it_can_make_a_fieldtype_unselectable_in_forms() + { + $fieldtype = new class extends Fieldtype + { + public static $handle = 'test-unselectable'; + protected $selectableInForms = true; + }; + + $this->assertTrue($fieldtype->selectableInForms()); + + $fieldtype::makeUnselectableInForms(); + + $this->assertFalse($fieldtype->selectableInForms()); + $this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test-unselectable')); + $this->assertTrue(FieldtypeRepository::selectableInFormIsOverriden('test-unselectable')); } }