Skip to content

Creating Pages from Code ​

Blueprints seed a site when it is created. FilamentCraft\Pages\SiteEditor is for everything after that: adding a page to a site that already exists, from a seeder, a demo account, a queued job or an AI connector, without opening the editor.

Every call writes a draft. Nothing here ever publishes a page, so the live site only changes when you say so.

php
use FilamentCraft\Pages\SiteEditor;

$editor = SiteEditor::for($site);

Each call returns a PageDraftResult with the page (a Template), the new revision and a list of warnings.

From a registered blueprint ​

Pass a registered blueprint slug, a blueprint class name or an instance:

php
$result = SiteEditor::for($site)->createPageFromBlueprint('event-home', by: $request->user());

$result->page->slug;      // 'event-home'
$result->revision->summary; // 'From blueprint: Home'

The page is created in draft with the blueprint's sections for every locale the site has. A blueprint marked isHomepage() becomes the site homepage only while the site has none; an existing homepage is never replaced. That means a draft page from an isHomepage() blueprint can take the empty homepage slot before anything is published.

Pass summary: to set the history entry; without it the entry reads "From blueprint: Home". The call signature is createPageFromBlueprint($blueprint, ?string $summary = null, bool $strict = false, ?Authenticatable $by = null).

Revising a page ​

revisePageFromBlueprint() saves the blueprint's sections as a new draft revision of an existing page. Pass the Template, its key or its slug (a string is matched against slugs first). A published page stays live until you publish the new draft.

php
$result = SiteEditor::for($site)->revisePageFromBlueprint(
    page: 'event-home',
    blueprint: 'event-home',
    summary: 'Spring refresh',
);

Without summary, the history entry reads "From blueprint: Home". The signature is revisePageFromBlueprint($page, AbstractBlueprint|string|null $blueprint = null, ?string $summary = null, bool $strict = false, ?Authenticatable $by = null, ?array $locales = null). PHP cannot force named arguments, but use blueprint: and locales: by name so the call reads clearly. A page that belongs to another site throws Illuminate\Database\Eloquent\ModelNotFoundException.

Leave blueprint out and the page's own blueprint is used. A page that was not created from a blueprint throws InvalidPageSpec with the code no_blueprint.

The page's settings (colour scheme and the like) are always kept from the current draft.

One locale at a time ​

Pass locales: to replace only those languages' sections. Every other language and the page settings stay exactly as they are in the draft, and only the listed languages are checked. Each locale must be one the site has (unknown_locale, path locales.0), and an empty list throws invalid_spec.

php
SiteEditor::for($site)->revisePageFromBlueprint($page, locales: ['fr']);

Seed a new language ​

Fill a language the moment it is added to the site:

php
use FilamentCraft\Events\SiteLocaleAdded;
use FilamentCraft\Pages\SiteEditor;
use Illuminate\Support\Facades\Event;

Event::listen(SiteLocaleAdded::class, function (SiteLocaleAdded $event): void {
    $site = $event->site;

    foreach ($site->templates()->whereNotNull('blueprint_slug')->get() as $page) {
        SiteEditor::for($site)->revisePageFromBlueprint($page, locales: [$event->locale]);
    }
});

If you queue the listener, pass the site and page ids and fetch them again inside the job.

Revising from a spec ​

revisePage() saves a spec as a new draft revision of an existing page. It takes sections, translations or both (at least one), and replaces the listed locales' sections; a locale you leave out keeps its sections, and the page settings stay untouched. name, slug and type throw invalid_spec: rename or retype a page in the editor.

php
SiteEditor::for($site)->revisePage('about', [
    'translations' => ['nl' => [['type' => 'hero', 'settings' => ['heading' => 'Hallo']]]],
], summary: 'Dutch copy');

The signature is revisePage($page, array $spec, ?string $summary = null, bool $strict = false, ?Authenticatable $by = null). To change single sections instead of replacing a list, use the Draft API.

From a spec ​

createPage() takes a plain array, the same section format as the JSON spec import:

php
$result = SiteEditor::for($site)->createPage([
    'name' => 'About',
    'slug' => 'about',
    'sections' => [
        ['type' => 'hero', 'settings' => ['heading' => 'Hello']],
    ],
    'translations' => [
        'nl' => [
            ['type' => 'hero', 'settings' => ['heading' => 'Hallo']],
        ],
    ],
]);

It accepts only name, slug, type, sections and translations. type defaults to a normal page. translations is a map of locale to a list of sections, and each locale must be one the site has other than the default locale; the default locale's sections go in sections. The history entry reads "Saved from code" unless you pass summary:. The signature is createPage(array $spec, ?string $summary = null, bool $strict = false, ?Authenticatable $by = null).

name and slug follow the same rules as a page in the JSON spec import: a name up to 190 characters, and a slug up to 190 characters made of letters, numbers, -, _ and / (services/web-design is fine). The slug is stored as given, minus leading and trailing slashes.

Any other key throws, including seo and homepage, which stay in the editor. publish throws when it is truthy.

Warnings and errors ​

Problems come in two kinds:

  • Errors throw FilamentCraft\Pages\Exceptions\InvalidPageSpec and nothing is written: an unknown section type, an unknown block type, an unknown section preset, an unknown blueprint slug or a blueprint class that can't be built, a missing name, slug or sections, unknown spec keys, a bad type, an invalid or too long name or slug, translations that is not a map of locale to a list of sections, a locale the site lacks, a translation for the site's default locale. $e->errors holds a list of SpecIssue objects.
  • Warnings are returned on $result->warnings and the page is still saved: an unknown setting id, an unknown block setting id, or a slug that was already taken (the page is saved with a suffix, and the warning names it).

Every error and warning is a FilamentCraft\Transfer\SpecIssue with three parts: a code (a SpecIssueCode enum), a path to the offending part of the spec, and a human message. $issue->toArray() returns all three as plain values, and casting it to a string gives the message. $e->messages() returns just the messages as a list of strings, and $result->warningMessages() does the same for warnings.

CodeMeaning
invalid_specThe spec is malformed: a missing or unknown key, a bad name, slug or type
unknown_section_typeThe section type is not registered
invalid_sectionA section is not a valid section array
unknown_presetThe section preset does not exist
unknown_block_typeThe block type is not one the section accepts
unknown_settingThe setting id is not one the section or block declares
invalid_optionA select setting got a value that is not one of its options
block_limitThe section has more blocks than it allows
unregistered_sectionA blueprint names a section that is not registered
unknown_localeThe site does not have that locale
invalid_translationstranslations for a locale is not a list of sections
slug_takenThe slug was taken, so the page was saved with a suffix
unknown_blueprintNo blueprint with that slug, or the class can't be built
no_blueprintThe page was not created from a blueprint
section_not_foundThe section id does not exist in that locale
section_lockedThe section is locked and cannot be moved or removed

Whether a code is thrown or returned depends on the call. Without strict, unknown_setting and slug_taken come back as warnings.

Pass strict: true to turn setting warnings into errors, so a typo in a setting id fails instead of being silently ignored. A taken slug is never rejected, in strict mode or not.

php
try {
    SiteEditor::for($site)->createPage($spec, strict: true);
} catch (InvalidPageSpec $e) {
    report($e->errors);
}

Publishing ​

Review the draft, then publish it yourself:

php
$result->page->publishHead();

TemplateDraftSaved fires once for each of createPageFromBlueprint(), createPage() and revisePageFromBlueprint(), right after the draft is saved. Each call commits its own transaction before the event fires. If you wrap the call in your own transaction, the event still fires immediately, before your outer transaction commits, because it is a plain event() and not dispatched after commit. Listeners you already have on draft saves see these changes too.

Authorisation ​

SiteEditor does not check who is calling. Resolve the site the user is allowed to edit first, then call it. SiteEditor is for trusted host code: it does not apply the panel's section allow-list or sealed-blueprint rules, so check permissions before calling.

Page keys work with integer, UUID and ULID keys alike. A slug made only of digits is read as a page id when you pass it to revisePageFromBlueprint() under integer keys, so pass the Template itself, or use a slug with at least one letter. To record who made the change, pass by: with the user; without it the signed-in user is recorded.

Editing sections ​

To change the sections of a page that already exists, see the Draft API.