From 817c20c24af6f16686f1376f3736a41839c9b94a Mon Sep 17 00:00:00 2001 From: Mischa Date: Sat, 4 Jul 2026 15:29:07 +0200 Subject: [PATCH] feat(post-form): inject active-trip parent server-side (U1) Derive the entry write target from site.active_trip at submit time in cache-on-save's onFormValidationProcessed handler, and fail closed (ValidationException) when no active trip is set. Removes the hardcoded pageconfig.parent that had to be hand-synced with active_trip. Refs R1, R2, AE2, KTD1. --- pages/02.post/post-form.md | 4 +- plugins/cache-on-save/cache-on-save.php | 50 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pages/02.post/post-form.md b/pages/02.post/post-form.md index c8431ea..16fc967 100644 --- a/pages/02.post/post-form.md +++ b/pages/02.post/post-form.md @@ -4,9 +4,9 @@ template: post-form access: site.login: true -# Keep in sync with active_trip in user/config/site.yaml +# Parent (write target) is injected server-side from site.active_trip by the +# cache-on-save plugin (onFormValidationProcessed) — no manual sync needed. pageconfig: - parent: '/trips/italy-2026-demo/dailies' slug_field: 'date,title' overwrite_mode: false diff --git a/plugins/cache-on-save/cache-on-save.php b/plugins/cache-on-save/cache-on-save.php index 3b91101..a89886a 100644 --- a/plugins/cache-on-save/cache-on-save.php +++ b/plugins/cache-on-save/cache-on-save.php @@ -1,6 +1,7 @@ ['onFormValidationProcessed', 0], 'onFormProcessed' => ['onFormProcessed', 0], ]; } + /** + * Server-authoritative active-trip parent injection. + * + * The post form no longer hardcodes `pageconfig.parent`; instead the write + * target is derived from `site.active_trip` at submit time and injected into + * the form data. add-page-by-form reads `$form->value()->toArray()['parent']` + * (add-page-by-form.php:521) and honours it over any pageconfig/header parent. + * + * Fail closed: if `active_trip` is unset/empty we throw a ValidationException + * so the `add_page` action never runs. Merely leaving `parent` unset is unsafe — + * with `pageconfig.parent` removed, add-page-by-form's `getParentPage('')` + * resolves to the /post page itself and the entry would silently land there. + */ + public function onFormValidationProcessed(Event $event): void + { + $form = $event['form']; + if (!$form || $form->getName() !== 'new-entry') { + return; + } + + $activeTrip = $this->grav['config']->get('site.active_trip'); + $activeTrip = is_string($activeTrip) ? trim($activeTrip) : ''; + + if ($activeTrip === '') { + throw new ValidationException('No active trip is set — cannot post an entry. Set site.active_trip first.'); + } + + $form->setData('parent', $this->resolveDailiesParent($activeTrip)); + } + + /** + * Normalise `active_trip` to its dailies container route. + * + * Accepts either a full route ("/trips/italy-2026-demo") or a bare slug + * ("italy-2026-demo") and returns "/trips//dailies". + */ + private function resolveDailiesParent(string $activeTrip): string + { + $trip = trim($activeTrip, '/'); + if (strpos($trip, 'trips/') !== 0) { + $trip = 'trips/' . $trip; + } + + return '/' . $trip . '/dailies'; + } + public function onFormProcessed(Event $event): void { $form = $event['form'];