Skip to content

Commit

Permalink
Allow function in blocks()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Mar 11, 2024
1 parent 11697ac commit 33b059f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Facades/TwillBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP)
* @method static Collection<Block>generateListOfAllBlocks(bool $settingsOnly = false)
* @method static Collection<Block>getListOfUsedBlocks()
* @method static Collection<Block>generateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false)
* @method static Collection<Block>generateListOfAvailableBlocks(array|callable $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = null, bool $defaultOrder = false)
*/
class TwillBlocks extends Facade
{
Expand Down
3 changes: 0 additions & 3 deletions src/Services/Forms/Fields/BaseFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/Services/Forms/Fields/BlockEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = [];
Expand All @@ -59,15 +60,14 @@ public function blocks(array $blocks): static
return $this;
}


public function usingDefaultOrder(bool $usingDefaultOrder = true): static
{
$this->usingDefaultOrder = $usingDefaultOrder;

return $this;
}

public function getBlocks(): array
public function getBlocks(): mixed
{
return $this->blocks;
}
Expand Down
10 changes: 5 additions & 5 deletions src/TwillBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -416,7 +416,7 @@ public function generateListOfAvailableBlocks(
return null;
};
$finalList = $this->generateListOfAllBlocks($settingsOnly)->filter(
function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlocks, $matchBlock) {
function (Block $block) use ($blocks, $groups, $excludeBlocks, $allowBlocks, $globalExcludeBlocks, $matchBlock) {

Check failure on line 419 in src/TwillBlocks.php

View workflow job for this annotation

GitHub Actions / lint

Anonymous function has an unused use $allowBlocks.

Check failure on line 419 in src/TwillBlocks.php

View workflow job for this annotation

GitHub Actions / lint

Undefined variable: $allowBlocks
if ($matchBlock($excludeBlocks, $block)) {
return false;
}
Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/View/Components/Fields/BlockEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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,
public ?string $trigger = null,
public bool $isSettings = false,
public bool $usingDefaultOrder = false,
public mixed $allowBlocks = null,
) {
parent::__construct(
name: $name,
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Helpers/BlockHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
Expand Down

0 comments on commit 33b059f

Please sign in to comment.