Skip to content
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

Builder Blocks are instantiated during registration #141

Open
Z3d0X opened this issue Feb 22, 2024 · 2 comments
Open

Builder Blocks are instantiated during registration #141

Z3d0X opened this issue Feb 22, 2024 · 2 comments

Comments

@Z3d0X
Copy link
Owner

Z3d0X commented Feb 22, 2024

Problem

This getName() method is called during block registration

public static function getName(): string
{
return static::getBlockSchema()->getName();
}

$this->pageBlocks->put($pageBlock::getName(), $pageBlock);

Potential Solution

A potential viable solution might be to Explicitly set a name like in Layouts

protected static ?string $name;
public static function getName(): string
{
return static::$name;
}

make:page-block could auto to generate $name.

This would be a breaking a change, since existing application would need to explicitly set the $name.

Other ideas are welcome

@Z3d0X
Copy link
Owner Author

Z3d0X commented Feb 22, 2024

This might also be the root cause of #122

@Voltra
Copy link
Collaborator

Voltra commented Jan 8, 2025

In one of my personal projects I use the following as my base class for blocks:

// [...]

namespace App\Filament\Fabricator;

// [...]

abstract class PageBlock extends \Z3d0X\FilamentFabricator\PageBlocks\PageBlock
{
    public const ID = 'undefined';

    public const ICON = 'heroicon-o-plus';

    final public static function getBlockSchema(): Block
    {
        $block = static::defineBlock(
            Block::make(static::getBlockId())
                ->label(fn () => __('blocks.'.static::getBlockId()))
                ->icon(static::getBlockIcon())
        );

        return PageBlockWrapper::wrap($block);
    }

    abstract public static function defineBlock(Block $block): Block;

    public static function mutateData(array $data): array
    {
        return $data;
    }

    public static function getBlockId(): string
    {
        return static::ID;
    }

    public static function getBlockIcon(): string
    {
        return static::ICON;
    }

    #[\Override]
    public static function getName(): string
    {
        return static::getBlockId();
    }
}

Something similar might be an interesting approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants