Files
landing-api/app/Filament/Resources/Pages/Schemas/PageForm.php
Josep Piguillem 3897113313 Initial commit: Pireon Admin — headless admin panel and public API
Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer
collective, backed by the Pireon OAuth identity server. The public site is a
separate, server-rendered application consuming /api/public/v1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 17:55:27 +02:00

177 lines
8.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Resources\Pages\Schemas;
use App\Enums\PageStatus;
use App\Filament\Resources\Pages\PageResource;
use App\Filament\Support\TranslatableTabs;
use App\Models\Page;
use App\Rules\UniqueTranslatedSlug;
use App\Support\DuplicateTitles;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class PageForm
{
public static function configure(Schema $schema): Schema
{
return $schema->components([
Section::make(__('panel.pages.sections.content'))
->description(__('panel.pages.sections.content_hint'))
->schema([
TranslatableTabs::make(fn (string $locale, bool $isDefault) => [
TextInput::make("title.{$locale}")
->label(__('panel.pages.fields.title'))
->required($isDefault)
->maxLength(255)
->columnSpanFull()
->live(onBlur: true)
->afterStateUpdated(function (?string $state, ?Model $record, callable $get, callable $set) use ($locale, $isDefault) {
if (blank($get("slug.{$locale}"))) {
$set("slug.{$locale}", Str::slug((string) $state));
}
// Only the language content is actually
// written in matters here: a near-duplicate
// in a language nobody is looking at right
// now would be a warning about the wrong
// thing.
if ($isDefault) {
DuplicateTitles::warnIfSimilar(
Page::class,
PageResource::class,
$state,
$locale,
$record?->getKey(),
);
}
}),
TextInput::make("slug.{$locale}")
->label(__('panel.pages.fields.slug'))
->required($isDefault)
->maxLength(255)
->columnSpanFull()
->rules(fn ($record) => [new UniqueTranslatedSlug(Page::class, $record?->id)]),
Textarea::make("excerpt.{$locale}")
->label(__('panel.pages.fields.excerpt'))
->rows(2)
->maxLength(500)
->columnSpanFull(),
RichEditor::make("body.{$locale}")
->label(__('panel.pages.fields.body'))
->columnSpanFull(),
]),
]),
Section::make(__('panel.pages.sections.publishing'))
->columns(2)
->schema([
Select::make('status')
->label(__('panel.pages.fields.status'))
->options(PageStatus::class)
->default(PageStatus::Draft)
->required()
->live()
// Editing content and pushing it live are separate
// permissions, so the status field itself is locked
// for users who may only write.
->disabled(fn () => Auth::user()?->cannot('pages.publish'))
->dehydrated(),
DateTimePicker::make('published_at')
->label(__('panel.pages.fields.published_at'))
->seconds(false)
->helperText(__('panel.pages.fields.published_at_hint')),
// The date nobody remembers on their own. A notice about
// something that already happened stays up until somebody
// trips over it.
DateTimePicker::make('unpublish_at')
->label(__('panel.pages.fields.unpublish_at'))
->helperText(__('panel.pages.fields.unpublish_at_hint'))
->seconds(false)
->after('published_at')
->disabled(fn () => Auth::user()?->cannot('pages.publish'))
->dehydrated(),
Select::make('category_id')
->label(__('panel.pages.fields.category'))
->relationship('category', 'name->'.config('locales.default'))
->searchable()
->preload(),
Select::make('author_id')
->label(__('panel.pages.fields.author'))
->relationship('author', 'name')
->searchable()
->preload()
->default(fn () => Auth::id())
->visible(fn () => Auth::user()?->can('users.view_any')),
FileUpload::make('featured_image')
->label(__('panel.pages.fields.featured_image'))
->image()
// Explicit: the default disk is private, and the
// frontend runs on another host and has to be able to
// fetch what you upload.
->disk(config('media.disk'))
->directory('pages')
->imageEditor()
// So the description field appears as soon as there is
// something to describe.
->live()
->columnSpanFull(),
// Alt text lives with the record rather than with the
// file, because the same image can need a different
// description depending on where it is used.
TranslatableTabs::make(fn (string $locale) => [
TextInput::make("featured_image_alt.{$locale}")
->label(__('panel.pages.fields.featured_image_alt'))
->helperText(__('panel.pages.fields.featured_image_alt_hint'))
->maxLength(255)
->columnSpanFull(),
])
->visible(fn (callable $get) => filled($get('featured_image')))
->columnSpanFull(),
Toggle::make('is_featured')
->label(__('panel.pages.fields.is_featured')),
]),
Section::make(__('panel.pages.sections.seo'))
->description(__('panel.pages.sections.seo_hint'))
->collapsed()
->schema([
TranslatableTabs::make(fn (string $locale) => [
TextInput::make("meta_title.{$locale}")
->label(__('panel.pages.fields.meta_title'))
->maxLength(60)
->columnSpanFull(),
Textarea::make("meta_description.{$locale}")
->label(__('panel.pages.fields.meta_description'))
->rows(2)
->maxLength(160)
->columnSpanFull(),
], label: __('panel.pages.sections.seo')),
]),
]);
}
}