klisica/filament-builder-blocks

Flexible builder blocks generator for Filament

v1.0.0 2024-05-31 12:48 UTC

This package is auto-updated.

Last update: 2024-09-08 20:16:58 UTC


README

πŸ—οΈ A Simpler Web CMS Builder for Laravel Filament

Latest Version on Packagist GitHub Code Style Action Status Total Downloads GitHub License

Create, manage & customize

By re-using Filament's Builder Input this package enables you to create custom section blocks as PHP classes (i.e. DefaultHeader.php) enabling you to use all features supported in Filament.

Each section block uses his own blade view file (i.e. default-header.blade.php) with support for dynamic data binded in PHP classes.

Another great helper functions that are ready-to-use:

  • renderSections(...) - Returns a fully formatted HTML code for each section,
  • cleanup(...) - Cleans unused attributes and values on store and update methods on filaments Create and Edit pages.

Installation

  1. Require the package via composer:
composer require klisica/filament-builder-blocks
  1. Install it to publish the config file:
php artisan filament-builder-blocks:install
  1. Open the config/filament-builder-blocks.php file and set the path value to root destination where you'll have you PHP classes (or leave it as it is).

  2. Run make section command to create your first example section class with the blade view file:

php artisan make:section Hero

Default folder structure example

Main section Hero.php will be displayed in builder dropdown, while child sections ExampleHero.php and AdvancedHero.php will be displayed as toggle buttons.

β”œβ”€β”€ app
β”‚   β”œβ”€β”€ Sections
β”‚   β”‚   β”œβ”€β”€ Header
β”‚   β”‚   β”‚   β”œβ”€β”€ ExampleHero.php
β”‚   β”‚   β”‚   β”œβ”€β”€ AdvancedHero.php
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ Hero.php

Creating layouts for each section block component.

β”œβ”€β”€ resources
β”‚   β”œβ”€β”€ views
β”‚   β”‚   β”œβ”€β”€ sections
β”‚   β”‚   β”‚   β”œβ”€β”€ example-hero.blade.php
β”‚   β”‚   β”‚   β”œβ”€β”€ advanced-hero.blade.php

Note

To be sure that on running the cleanup() helper your data won't be remove use the content. prefix on make input methods. This is used a handler to avoid storing inputs that you still need to show for descpritive purposes (i.e. Placeholder component). Take the ExampleHero.php as example:

class ExampleHero extends AbstractSectionItemProvider
{
    public function getFieldset(): Fieldset
    {
        return Fieldset::make($this->getName())
            ->schema([
                Placeholder::make('contact_links')->columnSpanFull(),  // Will get cleared out.
                TextInput::make('content.heading'),    // Will keep on save methods.
            ]);
    }
}

Example for adding cleanup on some Filaments Resource Edit Page:

protected function mutateFormDataBeforeSave(array $data): array
{
    return (new FilamentBuilderBlocks)->cleanup($data);
}

Rendering components

  1. Build sections in controller:
$sections = (new FilamentBuilderBlocks)->renderSections(
    sections: $pages->content,    // Page sections stored in content column
    wrappingSections: $layout->content    // Layout sections stored in content column (includes the `yield` field),
    configs: ['page' => $page, 'layout' => $layout]    // Can be whatever you need to bind in `blade.php` files
);

return view('dynamic')->with('sections', $sections);
  1. Display them in a dynamic.blade.php (or whatever you name it) file:
@foreach($sections as $section)
    {!! $section !!}
@endforeach

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.