From 61ef78606d8e61f23bbe2f61c77508e7bcff7916 Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 11 Mar 2025 11:17:01 -0400 Subject: [PATCH 1/4] Enable unselecting fieldtypes for forms --- src/Fields/Fieldtype.php | 5 +++++ src/Fields/FieldtypeRepository.php | 7 +++++++ tests/Fields/FieldtypeRepositoryTest.php | 19 +++++++++++++++++++ tests/Fields/FieldtypeTest.php | 19 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 956f2aee44..a3f78a3b93 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -111,6 +111,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..089f14e045 100644 --- a/src/Fields/FieldtypeRepository.php +++ b/src/Fields/FieldtypeRepository.php @@ -44,6 +44,13 @@ public function makeSelectableInForms($handle) $this->madeSelectableInForms[] = $handle; } + public function makeUnselectableInForms($handle) + { + if ($this->hasBeenMadeSelectableInForms($handle)) { + $this->madeSelectableInForms = array_diff($this->madeSelectableInForms, [$handle]); + } + } + public function hasBeenMadeSelectableInForms($handle) { return in_array($handle, $this->madeSelectableInForms); diff --git a/tests/Fields/FieldtypeRepositoryTest.php b/tests/Fields/FieldtypeRepositoryTest.php index 85f5b5d8b3..7591e02951 100644 --- a/tests/Fields/FieldtypeRepositoryTest.php +++ b/tests/Fields/FieldtypeRepositoryTest.php @@ -62,6 +62,25 @@ 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')); + + $this->repo->makeSelectableInForms('test'); + $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test')); + } + + #[Test] + public function it_makes_fields_unselectable_in_forms() + { + $this->repo->makeSelectableInForms('test'); + $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test')); + + $this->repo->makeUnselectableInForms('test'); + $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test')); + } } class FooFieldtype extends Fieldtype diff --git a/tests/Fields/FieldtypeTest.php b/tests/Fields/FieldtypeTest.php index 8ff3ca73e8..3a5a9328c7 100644 --- a/tests/Fields/FieldtypeTest.php +++ b/tests/Fields/FieldtypeTest.php @@ -566,6 +566,25 @@ public function it_can_make_a_fieldtype_selectable_in_forms() $this->assertTrue($fieldtype->selectableInForms()); $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); } + + #[Test] + public function it_can_make_a_fieldtype_unselectable_in_forms() + { + $fieldtype = new class extends Fieldtype + { + public static $handle = 'test'; + }; + + $fieldtype::makeSelectableInForms(); + + $this->assertTrue($fieldtype->selectableInForms()); + $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); + + $fieldtype::makeUnselectableInForms(); + + $this->assertFalse($fieldtype->selectableInForms()); + $this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); + } } class TestFieldtype extends Fieldtype From b5ca8744bec36cb54388b9d45016d1a15c85b5dd Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 11 Mar 2025 11:22:56 -0400 Subject: [PATCH 2/4] Coding with style --- tests/Fields/FieldtypeRepositoryTest.php | 2 +- tests/Fields/FieldtypeTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fields/FieldtypeRepositoryTest.php b/tests/Fields/FieldtypeRepositoryTest.php index 7591e02951..955ee18ac8 100644 --- a/tests/Fields/FieldtypeRepositoryTest.php +++ b/tests/Fields/FieldtypeRepositoryTest.php @@ -67,7 +67,7 @@ public function it_throw_exception_when_finding_invalid_fieldtype() public function it_makes_fields_selectable_in_forms() { $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test')); - + $this->repo->makeSelectableInForms('test'); $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test')); } diff --git a/tests/Fields/FieldtypeTest.php b/tests/Fields/FieldtypeTest.php index 3a5a9328c7..2a94c2b875 100644 --- a/tests/Fields/FieldtypeTest.php +++ b/tests/Fields/FieldtypeTest.php @@ -576,7 +576,7 @@ public function it_can_make_a_fieldtype_unselectable_in_forms() }; $fieldtype::makeSelectableInForms(); - + $this->assertTrue($fieldtype->selectableInForms()); $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); From d3e19710d9fbc424e2cc6d62a9aafcb4a13a090c Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 11 Mar 2025 15:03:46 -0400 Subject: [PATCH 3/4] Fixing make unselectable default selectable fields --- src/Fields/Fieldtype.php | 6 +++++- src/Fields/FieldtypeRepository.php | 16 ++++++++++------ tests/Fields/FieldtypeRepositoryTest.php | 16 +++++++++------- tests/Fields/FieldtypeTest.php | 16 ++++++++-------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index a3f78a3b93..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() diff --git a/src/Fields/FieldtypeRepository.php b/src/Fields/FieldtypeRepository.php index 089f14e045..da88560c3e 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,18 +41,22 @@ public function handles() public function makeSelectableInForms($handle) { - $this->madeSelectableInForms[] = $handle; + $this->selectableInForms[$handle] = true; } public function makeUnselectableInForms($handle) { - if ($this->hasBeenMadeSelectableInForms($handle)) { - $this->madeSelectableInForms = array_diff($this->madeSelectableInForms, [$handle]); - } + $this->selectableInForms[$handle] = false; } public function hasBeenMadeSelectableInForms($handle) { - return in_array($handle, $this->madeSelectableInForms); + dump([$handle, $this->selectableInForms]); + 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 955ee18ac8..5ea3ccaca6 100644 --- a/tests/Fields/FieldtypeRepositoryTest.php +++ b/tests/Fields/FieldtypeRepositoryTest.php @@ -66,20 +66,22 @@ public function it_throw_exception_when_finding_invalid_fieldtype() #[Test] public function it_makes_fields_selectable_in_forms() { - $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test')); + $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test-selectable')); - $this->repo->makeSelectableInForms('test'); - $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test')); + $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'); - $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test')); + $this->repo->makeSelectableInForms('test-unselectable'); + $this->assertTrue($this->repo->hasBeenMadeSelectableInForms('test-unselectable')); - $this->repo->makeUnselectableInForms('test'); - $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test')); + $this->repo->makeUnselectableInForms('test-unselectable'); + $this->assertFalse($this->repo->hasBeenMadeSelectableInForms('test-unselectable')); + $this->assertTrue($this->repo->selectableInFormIsOverriden('test-unselectable')); } } diff --git a/tests/Fields/FieldtypeTest.php b/tests/Fields/FieldtypeTest.php index 2a94c2b875..417ec8be91 100644 --- a/tests/Fields/FieldtypeTest.php +++ b/tests/Fields/FieldtypeTest.php @@ -555,16 +555,17 @@ 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] @@ -572,18 +573,17 @@ public function it_can_make_a_fieldtype_unselectable_in_forms() { $fieldtype = new class extends Fieldtype { - public static $handle = 'test'; + public static $handle = 'test-unselectable'; + protected $selectableInForms = true; }; - $fieldtype::makeSelectableInForms(); - $this->assertTrue($fieldtype->selectableInForms()); - $this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); $fieldtype::makeUnselectableInForms(); $this->assertFalse($fieldtype->selectableInForms()); - $this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test')); + $this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test-unselectable')); + $this->assertTrue(FieldtypeRepository::selectableInFormIsOverriden('test-unselectable')); } } From 6e5894e9c74c46d43b6d1d44b7df52112abb8cdf Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 11 Mar 2025 15:09:18 -0400 Subject: [PATCH 4/4] Remove dump - oops --- src/Fields/FieldtypeRepository.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Fields/FieldtypeRepository.php b/src/Fields/FieldtypeRepository.php index da88560c3e..0fe7eefa27 100644 --- a/src/Fields/FieldtypeRepository.php +++ b/src/Fields/FieldtypeRepository.php @@ -51,7 +51,6 @@ public function makeUnselectableInForms($handle) public function hasBeenMadeSelectableInForms($handle) { - dump([$handle, $this->selectableInForms]); return $this->selectableInForms[$handle] ?? false; }