From a5fb2ecd0ec6d8cad63b231a89488d952017f5d0 Mon Sep 17 00:00:00 2001 From: Tofandel Date: Mon, 11 Mar 2024 22:02:58 +0100 Subject: [PATCH] Allow function in blocks() --- src/Facades/TwillBlocks.php | 2 +- src/Services/Forms/Fields/BaseFormField.php | 3 --- src/Services/Forms/Fields/BlockEditor.php | 10 +++++----- src/TwillBlocks.php | 8 ++++---- src/View/Components/Fields/BlockEditor.php | 4 ++-- tests/unit/Helpers/BlockHelpersTest.php | 8 ++++++++ 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Facades/TwillBlocks.php b/src/Facades/TwillBlocks.php index 76c06807a..dcd83093c 100644 --- a/src/Facades/TwillBlocks.php +++ b/src/Facades/TwillBlocks.php @@ -18,7 +18,7 @@ * @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP) * @method static CollectiongenerateListOfAllBlocks(bool $settingsOnly = false) * @method static CollectiongetListOfUsedBlocks() - * @method static CollectiongenerateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false) + * @method static CollectiongenerateListOfAvailableBlocks(array|callable $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = null, bool $defaultOrder = false) */ class TwillBlocks extends Facade { diff --git a/src/Services/Forms/Fields/BaseFormField.php b/src/Services/Forms/Fields/BaseFormField.php index 5bc6b114d..ed7b3dfa1 100644 --- a/src/Services/Forms/Fields/BaseFormField.php +++ b/src/Services/Forms/Fields/BaseFormField.php @@ -12,9 +12,6 @@ abstract class BaseFormField implements CanRenderForBlocks { use RenderForBlocks; - /** - * @var \A17\Twill\View\Components\Fields\TwillFormComponent - */ protected function __construct( protected string $component, protected ?string $name = null, diff --git a/src/Services/Forms/Fields/BlockEditor.php b/src/Services/Forms/Fields/BlockEditor.php index 6dc893d3c..342984cf9 100644 --- a/src/Services/Forms/Fields/BlockEditor.php +++ b/src/Services/Forms/Fields/BlockEditor.php @@ -6,10 +6,11 @@ class BlockEditor extends BaseFormField { - protected array $blocks = []; + /** @var callable|array */ + protected mixed $blocks = []; protected array $groups = []; - protected mixed $excludeBlocks = []; + protected mixed $excludeBlocks = null; protected bool $isSettings = false; @@ -50,7 +51,7 @@ public function isSettings(bool $isSettings = true): static /** * Default is all, but using this method you can limit the block types the field can use. */ - public function blocks(array $blocks): static + public function blocks(array|callable $blocks): static { // For backward compatibility, clear the list of excludeBlocks in case both ->excludeBlocks()->blocks() were called $this->excludeBlocks = []; @@ -59,7 +60,6 @@ public function blocks(array $blocks): static return $this; } - public function usingDefaultOrder(bool $usingDefaultOrder = true): static { $this->usingDefaultOrder = $usingDefaultOrder; @@ -67,7 +67,7 @@ public function usingDefaultOrder(bool $usingDefaultOrder = true): static return $this; } - public function getBlocks(): array + public function getBlocks(): mixed { return $this->blocks; } diff --git a/src/TwillBlocks.php b/src/TwillBlocks.php index c7111daa5..dada07e0c 100644 --- a/src/TwillBlocks.php +++ b/src/TwillBlocks.php @@ -398,11 +398,11 @@ function ($appBlock) use ($block) { } public function generateListOfAvailableBlocks( - ?array $blocks = null, + array|callable $blocks = null, ?array $groups = null, bool $settingsOnly = false, - array|callable $excludeBlocks = [], - bool $defaultOrder = false + array|callable $excludeBlocks = null, + bool $defaultOrder = false, ): Collection { $globalExcludeBlocks = $this->getGloballyExcludedBlocks(); @@ -443,7 +443,7 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo } ); if (! $defaultOrder) { - if (! empty($blocks)) { + if (! empty($blocks) && is_array($blocks)) { $blocks = array_flip($blocks); $finalList = $finalList->sortBy(fn(Block $block) => $blocks[$block->name] ?? $blocks[ltrim($block->componentClass, '\\')] ?? PHP_INT_MAX, SORT_NUMERIC); } diff --git a/src/View/Components/Fields/BlockEditor.php b/src/View/Components/Fields/BlockEditor.php index b4a888c3e..f33096079 100644 --- a/src/View/Components/Fields/BlockEditor.php +++ b/src/View/Components/Fields/BlockEditor.php @@ -14,8 +14,8 @@ public function __construct( bool $renderForBlocks = false, bool $renderForModal = false, // Component specific - public array $blocks = [], - public mixed $excludeBlocks = [], + public mixed $blocks = [], + public mixed $excludeBlocks = null, public array $groups = [], public bool $withoutSeparator = false, public ?string $group = null, diff --git a/tests/unit/Helpers/BlockHelpersTest.php b/tests/unit/Helpers/BlockHelpersTest.php index 9fc699080..8708cee10 100644 --- a/tests/unit/Helpers/BlockHelpersTest.php +++ b/tests/unit/Helpers/BlockHelpersTest.php @@ -88,6 +88,14 @@ public function testGenerateListOfAvailableBlocks() $this->assertCount(1, $available); $this->assertContains(AppBlock::class, $available); + + $available = TwillBlocks::generateListOfAvailableBlocks( + blocks: fn (Block $block) => $block->name == 'group-block2' ? true : ($block->source == Block::SOURCE_TWILL ? false : null) + )->pluck('componentClass'); + $this->assertCount(2, $available); + $this->assertEquals([AppBlock::class, GroupBlock2::class], $available->all()); + + TwillBlocks::setGloballyExcludedBlocks(); config(['twill.block_editor.block_rules.order' => ['group-block2', AppBlock::class, 'group-block']]);