Skip to content

[5.x] Enable unselecting fieldtypes for forms #11559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,23 @@ 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()
{
FieldtypeRepository::makeSelectableInForms(self::handle());
}

public static function makeUnselectableInForms()
{
FieldtypeRepository::makeUnselectableInForms(self::handle());
}

public function categories(): array
{
return $this->categories;
Expand Down
16 changes: 13 additions & 3 deletions src/Fields/FieldtypeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class FieldtypeRepository
{
protected $madeSelectableInForms = [];
protected $selectableInForms = [];
private $fieldtypes = [];

public function preloadable()
Expand Down Expand Up @@ -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);
}
}
21 changes: 21 additions & 0 deletions tests/Fields/FieldtypeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions tests/Fields/FieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand Down