PHP code example of vortechron / filament-block-editor
1. Go to this page and download the library: Download vortechron/filament-block-editor 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/ */
vortechron / filament-block-editor example snippets
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\Page;
use Vortechron\FilamentBlockEditor\Filament\Concerns\InteractsWithPageBuilder;
use Vortechron\FilamentBlockEditor\Rendering\RenderContext;
class BuildPage extends Page
{
use InteractsWithPageBuilder;
use InteractsWithRecord;
protected static string $resource = PageResource::class;
public function mount(int|string $record): void
{
$this->record = $this->resolveRecord($record);
$this->authorizePageBuilder();
}
protected function getPageBuilderValue(): array
{
return $this->record->content ?? []; // any format: it is upgraded for the builder
}
protected function savePageBuilderValue(array $value): void
{
// ['version' => 2, 'sections' => [...]], already validated, cleaned and guarded
$this->record->update(['content' => $value]);
}
protected function renderPageBuilderPreview(array $sections): string
{
// Your real page view, drawn with the builder's marks so a click picks a section.
return view('pages.show', [
'page' => $this->record,
'sections' => $sections,
'context' => $this->getPageBuilderPreviewContext(), // forBuilder, isPreview, your render values
])->render();
}
protected function getPageBuilderBackUrl(): ?string
{
return PageResource::getUrl();
}
// Runs before every save, preview and upload (Livewire calls can be sent
// without opening the page). Call it from mount() too.
protected function authorizePageBuilder(): void
{
abort_unless(PageResource::canEdit($this->record), 403);
}
}
protected function isPageBuilderDeveloper(): bool { return auth()->user()->is_developer; } // locks, custom CSS
protected function getPageBuilderKit(): ?array { return Kit::normalize(setting('kit')); } // null: KitSource's current kit
protected function savePageBuilderKit(array $kit): void { setting(['kit' => $kit]); } // the design panel
protected function savePageBuilderSection(array $section, string $title, bool $synced): array { /* ... */ return ['ok' => true, 'id' => $id, 'template' => null]; }
protected function getPageBuilderSiteInfo(): ?array { /* groups of Fields and their values */ }
protected function savePageBuilderSiteInfo(array $values): void { /* validate and save */ }
protected function withPageBuilderSiteInfo(array $values, Closure $draw): string { return $draw(); } // preview with unsaved shop info
protected function getPageBuilderSyncedSections(): array { return []; } // [['id', 'title', 'editUrl', 'section']]
protected function getPageBuilderParts(): array { return []; } // [['key' => 'header', 'title', 'editUrl']]
protected function getPageBuilderKind(): string { return 'page'; } // an Enums\DocumentKind value
protected function getPageBuilderDynamicValues(): array { return []; } // more values for this builder only
protected function getPageBuilderRenderValues(): array { return []; } // ['page.title' => 'Laman Utama'] for the preview
protected function getPageBuilderPages(): array { return []; } // [['title' => 'Stationery', 'url' => '/stationery']] for link fields, in menu order
use Vortechron\FilamentBlockEditor\Blocks\BlockRegistry;
use Vortechron\FilamentBlockEditor\Blocks\Field;
use Vortechron\FilamentBlockEditor\Blocks\OwnBlock;
app(BlockRegistry::class)->register(
OwnBlock::make('pricing')
->label('Pricing')
->icon('tag') // a lucide icon name
->fields([
Field::text('heading')->label('Heading')->default('Our packages'),
Field::list('plans')->label('Plans')->maxItems(4)->fields([
Field::text('name')->label('Name'),
Field::number('price')->label('Price'),
Field::toggle('popular')->label('Popular'),
]),
])
->view('blocks.pricing'), // gets $values, $block, $context
);
use Vortechron\FilamentBlockEditor\Rendering\Pager;
$pages = Pager::pages($items, perPage: 6, paged: $values['pages']); // only the first page when not paged
$html = array_map(fn (array $page): string => view('blocks.items-page', ['items' => $page])->render(), $pages);
echo view('filament-block-editor::pager', ['name' => Pager::name($block), 'pages' => $html]);
protected function getPageBuilderContentLocale(): string
{
return $this->record->locale;
}
protected function getPageBuilderLanguages(): array
{
return [
['locale' => 'ms', 'label' => 'Bahasa Melayu', 'url' => '/admin/pages/1/build', 'current' => true],
['locale' => 'en', 'label' => 'English', 'url' => null, 'current' => false],
];
}
public function createPageBuilderTranslation(string $locale): array
{
$this->authorizePageBuilder();
// Validate the locale against your enabled languages, check permission
// to create, find or create the counterpart draft, then return its builder.
return ['url' => $draftBuilderUrl];
}
bash
composer install && npm install
php bin/export-rules.php # after changing a limit in PHP: the builder's rules (builder/src/generated)
npm run build # builder, its stylesheet and the public page.css into resources/dist
npm test # builder tests (Vitest)
vendor/bin/phpunit # PHP tests
vendor/bin/pint --test
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.