Skip to content

Draft API ​

FilamentCraft\Pages\SiteEditor also opens a page or a region as a draft you can read and change from code. It is meant for seeders, jobs, scripts and AI connectors that edit what is already on a site. To create pages see Creating Pages from Code.

php
use FilamentCraft\Pages\SiteEditor;

$editor = SiteEditor::for($site);
$draft = $editor->page('about');

page() takes a Template, its key or its slug. A string is matched against slugs first, so page('2024') opens the page with the slug 2024 before it tries a page with the key 2024. A page that belongs to another site throws Illuminate\Database\Eloquent\ModelNotFoundException. It also takes locale: (the site's default locale if left out) and strict:.

Authorisation ​

SiteEditor does not check who is calling, and it ignores the panel's section allow-list and sealed-blueprint rules. Resolve the site the user may edit first:

php
use FilamentCraft\Resolvers\TenancyResolver;

$site = app(TenancyResolver::class)->resolveAccessibleSite();

abort_unless($site, 403);

$draft = SiteEditor::for($site)->page($request->input('page'));

Or check your own policy before calling SiteEditor::for().

Reading ​

php
$editor->pages();            // list<PageSummary>, ordered by name
$draft->sections();          // every section in order
$draft->section($id);        // one section
$draft->schema('hero');      // setting ids, options and blocks of a section type

pages() returns PageSummary objects with id, name, slug, type, status, headRevisionId, publishedRevisionId, blueprintSlug, homepage, hasDraftChanges and url. Call toArray() for a JSON-friendly version.

A section is an array with id, type, settings, blocks, hidden and locked. An unknown id throws InvalidPageSpec with the code section_not_found.

schema() is the same data as php artisan filamentcraft:sections <slug>. Use it to learn which setting ids a section accepts before you write to it. An unknown type throws unknown_section_type.

Locales ​

A draft edits one locale. The default is the site's default locale.

php
$french = $draft->forLocale('fr');

forLocale() returns a clone that shares the same state, changes and base revision as the original. Edit either one and one save() writes both. The locale must be one the site has (unknown_locale).

Editing ​

Nothing is written until you save. Every method works on the current locale.

php
$id = $draft->addSection(['type' => 'hero', 'settings' => ['heading' => 'Hello']]);
$draft->addSection(['type' => 'faq'], index: 0);

$draft->updateSection($id, ['heading' => 'Hello again']);
$draft->updateSection($id, blocks: [['type' => 'item', 'settings' => ['title' => 'One']]]);
$draft->updateSection($id, hidden: true);

$draft->moveSection($id, 2);
$draft->removeSection($id);
  • addSection(array $section, ?int $index = null): string returns the new section id. The section uses the same format as the spec import. Leave index out to append. An id that already exists on the page is replaced by a new one.
  • updateSection(string $id, array $settings = [], ?array $blocks = null, ?bool $hidden = null) changes only the settings you pass. blocks replaces the whole block list when given and is left alone when null. hidden shows or hides the section.
  • moveSection(string $id, int $index) moves a section to a zero-based position. An index past either end is clamped to the first or last position.
  • removeSection(string $id) deletes the section.

A locked section (from a sealed blueprint) can still be updated, but moveSection() and removeSection() throw section_locked.

Warnings and strict mode ​

A section is checked the same way as in a spec. Problems that do not stop the save, such as an unknown setting id, collect on the draft:

php
$draft->warnings();   // list<SpecIssue>
$draft->changes();    // list<DraftChange>
$draft->isDirty();    // true when there are unsaved changes

Open the draft with strict: true and those warnings throw InvalidPageSpec instead.

changes() lists what happened since the last save. Each DraftChange has an operation (DraftOperation::Added, Updated, Moved or Removed), the sectionId, the section type and the locale.

Saving and publishing ​

php
$result = $draft->save(summary: 'Fix hero copy', by: $user);

$result->page;       // the Template
$result->revision;   // the new TemplateRevision, or null (see below)
$result->warnings;   // list<SpecIssue>

save() writes a new draft revision and moves the page head to it. It never publishes, so the live site does not change. A draft with no changes writes nothing and returns the current head. A page that has no head revision yet and no changes also writes nothing, and revision is null.

publish() is save() when there are changes, followed by publishHead():

php
$draft->publish(summary: 'Spring refresh', by: $user);

Publishing a page that has no head revision and no changes throws InvalidPageSpec (code invalid_spec, path sections) and writes nothing, even with force: true, because it would go live as a blank page. Add a section first.

Without summary, the history entry reads "Saved from code". Without by, the signed-in user is recorded.

After a save the draft keeps working: its base moves to the new revision, and you can keep editing and save again.

Stale drafts ​

A draft remembers the page head it was loaded from. If someone else saved the page in the meantime, save() and publish() throw FilamentCraft\Pages\Exceptions\StaleDraft so you never overwrite their work. The exception carries expected and actual (the revision ids).

php
use FilamentCraft\Pages\Exceptions\StaleDraft;

try {
    $draft->save();
} catch (StaleDraft $e) {
    $draft = $editor->page($draft->page());
}

Load the page again and redo the change, or overwrite with force: true:

php
$draft->save(force: true);

Regions ​

Header, footer and announcement bar are regions:

php
use FilamentCraft\Enums\RegionName;

$footer = SiteEditor::for($site)->region(RegionName::Footer);

$footer->updateSection($id, ['copyright' => '© 2026 Acme']);
$footer->save(by: $user);

RegionDraft has the same reading and editing methods as a page draft, including forLocale(). Regions have no drafts and no publish(). save() goes live immediately, the same as the editor. It throws StaleDraft when the region changed since you loaded it (force: true overwrites), and returns a RegionDraftResult with the region and warnings.

Events ​

FilamentCraft\Events\SiteEditorSaved fires once per save, after the write has committed. It fires only from PageDraft::save() and publish() and from RegionDraft::save(). createPage(), revisePage() and revisePageFromBlueprint() fire TemplateDraftSaved only. A page draft save fires both, so listeners you already have see these changes too.

PropertyValue
sitethe Site
targetthe Template or the Region
revisionthe new TemplateRevision, or null for a region
changeslist<DraftChange>
userIdthe user the change is recorded against, or null

A clean draft saves nothing and fires nothing.

Use it for audit logs or usage billing:

php
use FilamentCraft\Events\SiteEditorSaved;
use Illuminate\Support\Facades\Event;

Event::listen(SiteEditorSaved::class, function (SiteEditorSaved $event): void {
    AuditEntry::create([
        'site_id' => $event->site->getKey(),
        'user_id' => $event->userId,
        'changes' => array_map(fn ($change) => $change->toArray(), $event->changes),
    ]);
});

Example: an AI connector ​

Three tools for an MCP server or any function-calling model. Return SpecIssue::toArray() to the model, so it can read the code, the path and the message and fix its next call.

php
use FilamentCraft\Models\Site;
use FilamentCraft\Pages\Exceptions\InvalidPageSpec;
use FilamentCraft\Pages\Exceptions\StaleDraft;
use FilamentCraft\Pages\SiteEditor;
use FilamentCraft\Transfer\SpecIssue;

function listPages(Site $site): array
{
    return array_map(fn ($page) => $page->toArray(), SiteEditor::for($site)->pages());
}

function readPage(Site $site, string $page, string $locale): array
{
    return SiteEditor::for($site)->page($page, $locale)->sections();
}

function updateSection(Site $site, string $page, string $locale, string $sectionId, array $settings): array
{
    try {
        $draft = SiteEditor::for($site)->page($page, $locale, strict: true);
        $draft->updateSection($sectionId, $settings);
        $draft->save(summary: 'Updated by the assistant');
    } catch (InvalidPageSpec $e) {
        return ['ok' => false, 'issues' => array_map(fn (SpecIssue $issue) => $issue->toArray(), $e->errors)];
    } catch (StaleDraft $e) {
        return ['ok' => false, 'issues' => [['code' => 'stale_draft', 'path' => '', 'message' => $e->getMessage()]]];
    }

    return ['ok' => true];
}

The model never publishes. A person reviews the draft in the editor and publishes it there.