statikbe / laravel-filament-flexible-content-blocks

The Laravel Filament Flexible Content Blocks package helps you to easily create content in Filament for any model, with predefined or custom blocks, and foreach block an extendable Blade view component.
MIT License
114 stars 18 forks source link

Schema-less block types #30

Closed assertchris closed 8 months ago

assertchris commented 8 months ago

Forgive the template I've used, selecting anything other than "report a bug" leads to a 404 on GitHub. 🤷‍♂️

I find myself needing to make blocks that are placeholders with special meaning. For example, a client has requested I create a "separator" block that has no "content" but is just a placeholder so that the API can tell their app team to insert an app-specific separation between content.

The field looks like this:

Screenshot 2024-02-05 at 10 37 23

The way I have achieved this is as follows:

protected static function makeFilamentSchema(): array|Closure
{
    return [
        Textarea::make('collapsed')->view('components.content-block-editors.collapsed'),
    ];
}

This is from app/Filament/ContentBlocks/Separator.php

...And, in the view:

<script>
    var collapsible = document.currentScript.parentNode.parentNode.parentNode;
    collapsible.parentNode.removeChild(collapsible);
</script>

This is from resources/views/components/content-block-editors/collapsed.blade.php

I'm not a fan of this kind of JS approach but I can't think of any other way to "collapse" (without allowing expansion) of a custom block. I can imagine a public static function hasNoFields(): bool that the custom block could implement, which the renderer could use to omit the x-show=!isCollapsed div.

sten commented 8 months ago

Hey, @assertchris! Can you tell me what you are experiencing or try to achieve? Why do you need the JS code in the template? I thought it had to do with having a block without any really useful form fields.

So I quickly tried to implement a separator block:

<?php

namespace App\Filament\Blocks;

use Closure;
use Statikbe\FilamentFlexibleContentBlocks\ContentBlocks\AbstractContentBlock;

class SeparatorBlock extends AbstractContentBlock
{

    public static function getName(): string
    {
        return 'pubmus.separator';
    }

    public static function getIcon(): string
    {
        return 'heroicon-o-building-library';
    }

    public static function getLabel(): string
    {
        return 'Separator';
    }

    public static function getFieldLabel(string $field): string
    {
        return 'Separator';
    }

    protected static function makeFilamentSchema(): array|Closure
    {
        return [];
    }

    public function render()
    {
        return view('filament.blocks.separator');
    }

    public function getSearchableContent(): array
    {
        return [];
    }
}

and the blade component:

<div>
    <hr/>
</div>

For me it works.

assertchris commented 8 months ago

Sorry for not giving more context for this. The issue I was having (and why I added the JS) was: even when providing an empty form schema array, the container is still added and visible:

empty

The JS removes the empty container element. I am considering adding additional JS to remove the collapse/expand arrow if it's present.

The blade code wasn't for the frontend, it was for the filament UI.

sten commented 8 months ago

@assertchris That's related to the way the UI of the block works of the Filament Builder. Maybe you can post this as a filament issue.

I will close this, since it is not related to the functionality of this package. Sorry!

assertchris commented 8 months ago

Ok, cool. Thanks for the quick turn-around.