PHP code example of iamgerwin / filament-flexible-content
1. Go to this page and download the library: Download iamgerwin/filament-flexible-content library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iamgerwin / filament-flexible-content example snippets
use IamGerwin\FilamentFlexibleContent\Forms\Components\FlexibleContent;
use App\Filament\Flexible\Layouts\HeroLayout;
use App\Filament\Flexible\Layouts\ContentLayout;
public static function form(Form $form): Form
{
return $form
->schema([
FlexibleContent::make('content')
->layouts([
HeroLayout::make(),
ContentLayout::make(),
])
]);
}
namespace App\Filament\Flexible\Layouts;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use IamGerwin\FilamentFlexibleContent\Layouts\Layout;
final class HeroLayout extends Layout
{
protected ?string $name = 'hero';
protected ?string $title = 'Hero Section';
protected function setUp(): void
{
parent::setUp();
$this->icon('heroicon-o-rectangle-group')
->fields([
TextInput::make('heading')
->
namespace App\Filament\Flexible\Presets;
use IamGerwin\FilamentFlexibleContent\Layouts\Preset;
use App\Filament\Flexible\Layouts\HeroLayout;
use App\Filament\Flexible\Layouts\ContentLayout;
final class PageBuilder extends Preset
{
public function register(): void
{
$this->addLayouts([
HeroLayout::make(),
ContentLayout::make(),
]);
}
}
FlexibleContent::make('content')
->layouts([/* ... */])
->minLayouts(1) // Minimum number of layouts
->maxLayouts(10) // Maximum number of layouts
->onlyLayouts(['hero']) // Limit to specific layouts
->collapsible() // Make blocks collapsible
->cloneable() // Allow cloning blocks
->reorderable() // Allow reordering blocks
->columnSpanFull() // Full width
// Show flexible content only when type is 'national'
FlexibleContent::make('content')
->dependsOn('type', fn ($get) => $get('type') === 'national')
->layouts([/* ... */])
// Multiple field dependencies
FlexibleContent::make('content')
->dependsOn(['type', 'status'], function ($get) {
return $get('type') === 'national' && $get('status') === 'published';
})
->layouts([/* ... */])
class ConditionalLayout extends Layout
{
protected function setUp(): void
{
parent::setUp();
// Only show this layout when scope is 'global'
$this->dependsOn('scope', fn ($get) => $get('scope') === 'global');
$this->fields([
TextInput::make('title')->
use IamGerwin\FilamentFlexibleContent\Casts\FlexibleContentCast;
class Page extends Model
{
protected $casts = [
'content' => FlexibleContentCast::class,
];
}
// Check layout type
if ($block->is('hero')) {
// Handle hero layout
}
// Access data
$heading = $block->get('heading');
$hasHeading = $block->has('heading');
// Access metadata
$order = $block->getMeta('order');
// Convert to array
$array = $block->toArray();