Skip to content

Commit a5fb2ec

Browse files
committed
Allow function in blocks()
1 parent 11697ac commit a5fb2ec

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

src/Facades/TwillBlocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP)
1919
* @method static Collection<Block>generateListOfAllBlocks(bool $settingsOnly = false)
2020
* @method static Collection<Block>getListOfUsedBlocks()
21-
* @method static Collection<Block>generateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false)
21+
* @method static Collection<Block>generateListOfAvailableBlocks(array|callable $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = null, bool $defaultOrder = false)
2222
*/
2323
class TwillBlocks extends Facade
2424
{

src/Services/Forms/Fields/BaseFormField.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ abstract class BaseFormField implements CanRenderForBlocks
1212
{
1313
use RenderForBlocks;
1414

15-
/**
16-
* @var \A17\Twill\View\Components\Fields\TwillFormComponent
17-
*/
1815
protected function __construct(
1916
protected string $component,
2017
protected ?string $name = null,

src/Services/Forms/Fields/BlockEditor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
class BlockEditor extends BaseFormField
88
{
9-
protected array $blocks = [];
9+
/** @var callable|array */
10+
protected mixed $blocks = [];
1011
protected array $groups = [];
1112

12-
protected mixed $excludeBlocks = [];
13+
protected mixed $excludeBlocks = null;
1314

1415
protected bool $isSettings = false;
1516

@@ -50,7 +51,7 @@ public function isSettings(bool $isSettings = true): static
5051
/**
5152
* Default is all, but using this method you can limit the block types the field can use.
5253
*/
53-
public function blocks(array $blocks): static
54+
public function blocks(array|callable $blocks): static
5455
{
5556
// For backward compatibility, clear the list of excludeBlocks in case both ->excludeBlocks()->blocks() were called
5657
$this->excludeBlocks = [];
@@ -59,15 +60,14 @@ public function blocks(array $blocks): static
5960
return $this;
6061
}
6162

62-
6363
public function usingDefaultOrder(bool $usingDefaultOrder = true): static
6464
{
6565
$this->usingDefaultOrder = $usingDefaultOrder;
6666

6767
return $this;
6868
}
6969

70-
public function getBlocks(): array
70+
public function getBlocks(): mixed
7171
{
7272
return $this->blocks;
7373
}

src/TwillBlocks.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ function ($appBlock) use ($block) {
398398
}
399399

400400
public function generateListOfAvailableBlocks(
401-
?array $blocks = null,
401+
array|callable $blocks = null,
402402
?array $groups = null,
403403
bool $settingsOnly = false,
404-
array|callable $excludeBlocks = [],
405-
bool $defaultOrder = false
404+
array|callable $excludeBlocks = null,
405+
bool $defaultOrder = false,
406406
): Collection {
407407
$globalExcludeBlocks = $this->getGloballyExcludedBlocks();
408408

@@ -443,7 +443,7 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo
443443
}
444444
);
445445
if (! $defaultOrder) {
446-
if (! empty($blocks)) {
446+
if (! empty($blocks) && is_array($blocks)) {
447447
$blocks = array_flip($blocks);
448448
$finalList = $finalList->sortBy(fn(Block $block) => $blocks[$block->name] ?? $blocks[ltrim($block->componentClass, '\\')] ?? PHP_INT_MAX, SORT_NUMERIC);
449449
}

src/View/Components/Fields/BlockEditor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function __construct(
1414
bool $renderForBlocks = false,
1515
bool $renderForModal = false,
1616
// Component specific
17-
public array $blocks = [],
18-
public mixed $excludeBlocks = [],
17+
public mixed $blocks = [],
18+
public mixed $excludeBlocks = null,
1919
public array $groups = [],
2020
public bool $withoutSeparator = false,
2121
public ?string $group = null,

tests/unit/Helpers/BlockHelpersTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public function testGenerateListOfAvailableBlocks()
8888
$this->assertCount(1, $available);
8989
$this->assertContains(AppBlock::class, $available);
9090

91+
92+
$available = TwillBlocks::generateListOfAvailableBlocks(
93+
blocks: fn (Block $block) => $block->name == 'group-block2' ? true : ($block->source == Block::SOURCE_TWILL ? false : null)
94+
)->pluck('componentClass');
95+
$this->assertCount(2, $available);
96+
$this->assertEquals([AppBlock::class, GroupBlock2::class], $available->all());
97+
98+
9199
TwillBlocks::setGloballyExcludedBlocks();
92100

93101
config(['twill.block_editor.block_rules.order' => ['group-block2', AppBlock::class, 'group-block']]);

0 commit comments

Comments
 (0)