Move records through a status workflow
Offer explicit transition buttons, persist the status and react to actual changes. Protect transitions with server checks and field permissions.
Complete example: acme_approval_status
Purpose (translated from upstream README)
Adapt (translated from upstream README)
Files relative to the system root:
├── README.md├── fixtures/│ └── native.module.bake.json├── menu.bake.json├── modules/│ └── acme_requests/│ ├── acme_requests.detail.json│ ├── acme_requests.index.json│ └── acme_requests.module.bake.json├── preview/│ └── sample.bake.json├── roles.bake.json├── tests/│ ├── acme_request_approved.test.workflow.json│ ├── acme_request_denied.test.workflow.json│ └── acme_request_unchanged.test.workflow.json├── translations/│ ├── de.json│ ├── en.json│ └── translations.bake.json└── workflows/ └── acme_request_approval.workflow.bake.jsonREADME.md
# acme_approval_status
Anträge / Requests. Antrag speichern, dann per Layout-Knopf einreichen. Der Statuswechsel draft → open benachrichtigt den Genehmiger mit approve/deny-Aktionen und Checkpoint. Die Antwort speichert die Entscheidung; event/update vergleicht mit $original.status und informiert den Antragsteller. Nur die Rolle acme_approver darf das Statusfeld schreiben.
## Dateien
- `modules/`: Schema sowie Detail- und Index-Layouts.- `menu.bake.json`: Navigation (in ein vorhandenes Menü übernehmen).- `translations/`: deutsche/englische Labels und Bakery-Ressource.- `workflows/`, `tests/`: ausführbare Graphen und isolierte Business-Tests mit Fixtures.- `preview/`: optionale Bakery-Beispieldaten; nur in einer Vorschau anwenden.
## Anpassen
Alle `acme_*`-Modul-, Workflow-, Event- und Ressourcen-Identifier gemeinsam umbenennen; Referenzen, Layoutpfade, Filter, Übersetzungsschlüssel und Tests mitändern. Weitere Felder in Schema, Layout und beiden Sprachen ergänzen. Dateien relativ zur Systemwurzel kopieren. Native Module aus `fixtures/` nur ergänzen, wenn sie fehlen; bestehende Definitionen zusammenführen. Menüeinträge und Vorschaudaten sind optional. Varianten separat prüfen, nicht zusätzlich zur Hauptdefinition anwenden.
Prüfen: `php bakery plan <system>`, `php bakery apply <system>`, dann `php bakery system test-run <system>`. In diesem Repository führt `AcmeFeatureExamplesTest` dieselben Plan-/Apply-Schritte und die Workflow-Tests in der wegwerfbaren PHPUnit-Systemdatenbank aus. UI-Darstellung und reine Client-Aktionen sind damit nicht geprüft.
`roles.bake.json` vergibt Feldrechte: `fields.status.write`, nicht `update`. Rollen sind additiv: Eine weitere weitreichende Rolle kann die Einschränkung aufheben. Antragsteller/Genehmiger im Formular wählen und Rollen an echte Benutzer vergeben. Der Knopf setzt einen gespeicherten Entwurf voraus; der Webhook prüft Rollen und den Ausgangsstatus. `action/save` löst Update-Events aus; Tests benötigen `effects.save_events: allow`. Keine veraltete `show_in_button_bar`-Option. In-App-Nachrichten sind hier bewusst englisch; UI-Labels sind de/en.fixtures/native.module.bake.json
[ { "resource_module": "users", "import": true, "resource": { "identifier": "users", "type": "users", "title": "name", "fields": [ { "identifier": "name", "type": "text" }, { "identifier": "email", "type": "email" }, { "identifier": "password", "type": "password" } ] } }, { "resource_module": "roles", "import": true, "resource": { "identifier": "roles", "type": "roles", "title": "name", "fields": [ { "identifier": "name", "type": "text" }, { "identifier": "slug", "type": "text" }, { "identifier": "modules", "type": "json" } ] } }]menu.bake.json
[ { "resource_menu": "acme_approval_status", "resource": { "name": "ACME Inc.", "elements": [ "acme_requests" ] } }]modules/acme_requests/acme_requests.detail.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "field_group", "identifier": "general", "options": { "fields": [ "name", "status", "employee", "approver" ] } }, { "type": "buttons", "identifier": "transitions", "options": { "buttons": [ { "identifier": "acme_submit_request", "title": "buttons.acme_submit_request", "style": "primary", "event": { "action": "workflow", "event": "acme_submit_request", "module": "acme_requests" }, "options": { "recipes": { "display": "this.status == \"draft\"" } } } ] } } ] } ] } ] } ]}modules/acme_requests/acme_requests.index.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "resource_table", "identifier": "records", "options": { "module": "acme_requests" } } ] } ] } ] } ]}modules/acme_requests/acme_requests.module.bake.json
[ { "resource_module": "acme_requests", "resource": { "identifier": "acme_requests", "title": "name", "fields": [ { "identifier": "name", "type": "text" }, { "identifier": "status", "type": "choice", "options": { "values": [ "draft", "open", "approved", "denied" ], "default": "draft" } }, { "identifier": "employee", "type": "select", "options": { "references": "users" } }, { "identifier": "approver", "type": "select", "options": { "references": "users" } } ], "layouts": { "detail": "/modules/acme_requests/acme_requests.detail", "index": "/modules/acme_requests/acme_requests.index" } } }]preview/sample.bake.json
[ { "resource_entity": "acme_preview_request", "resource": { "module": "acme_requests", "fields": { "name": "ACME Inc. sample request", "status": "draft" } } }]roles.bake.json
[ { "resource_entity": "acme_requester", "resource": { "module": "roles", "fields": { "name": "ACME Inc. requester", "slug": "acme_requester", "modules": { "acme_requests": { "create": true, "read": true, "update": true, "delete": false, "fields": { "name": true, "employee": true, "approver": true, "status": { "read": true, "write": false } } }, "users": { "read": true, "fields": { "name": true, "email": true } } } } } }, { "resource_entity": "acme_approver", "resource": { "module": "roles", "fields": { "name": "ACME Inc. approver", "slug": "acme_approver", "modules": { "acme_requests": { "create": true, "read": true, "update": true, "delete": false, "fields": { "name": true, "employee": true, "approver": true, "status": { "read": true, "write": true } } }, "users": { "read": true, "fields": { "name": true, "email": true } } } } } }]tests/acme_request_approved.test.workflow.json
{ "identifier": "acme_request_approved", "workflow": "acme_request_approval", "fixtures": [ { "type": "user", "alias": "employee", "email": "employee@example.invalid", "roles": [ "acme_requester" ], "fields": { "name": "ACME Requester" } }, { "type": "user", "alias": "approver", "email": "approver@example.invalid", "roles": [ "acme_approver" ], "fields": { "name": "ACME Approver" } }, { "type": "entity", "module": "acme_requests", "alias": "request", "fields": { "name": "ACME Inc. request", "status": "draft", "employee": "fixture:employee", "approver": "fixture:approver" } } ], "trigger": { "type": "dispatch_sync", "entry": "acme_submit_request", "data": { "prototype": "fixture:request" }, "assertions": [ { "type": "notification_sent", "recipient": "approver@example.invalid", "count": 1 } ] }, "assertions": [ { "type": "field_equals", "module": "acme_requests", "selector": { "id": "fixture:request.id" }, "field": "status", "value": "approved" }, { "type": "notification_sent", "recipient": "employee@example.invalid", "message_contains": "approved", "count": 1 }, { "type": "field_editable", "module": "acme_requests", "field": "status", "as": "fixture:approver" }, { "type": "field_readonly", "module": "acme_requests", "field": "status", "as": "fixture:employee" } ], "actor": "fixture:employee", "effects": { "save_events": "allow" }, "steps": [ { "type": "respond", "element": "notifyApprover", "state": "approve", "as": "fixture:approver" } ]}tests/acme_request_denied.test.workflow.json
{ "identifier": "acme_request_denied", "workflow": "acme_request_approval", "fixtures": [ { "type": "user", "alias": "employee", "email": "employee@example.invalid", "roles": [ "acme_requester" ], "fields": { "name": "ACME Requester" } }, { "type": "user", "alias": "approver", "email": "approver@example.invalid", "roles": [ "acme_approver" ], "fields": { "name": "ACME Approver" } }, { "type": "entity", "module": "acme_requests", "alias": "request", "fields": { "name": "ACME Inc. request", "status": "draft", "employee": "fixture:employee", "approver": "fixture:approver" } } ], "trigger": { "type": "dispatch_sync", "entry": "acme_submit_request", "data": { "prototype": "fixture:request" }, "assertions": [ { "type": "notification_sent", "recipient": "approver@example.invalid", "count": 1 } ] }, "assertions": [ { "type": "field_equals", "module": "acme_requests", "selector": { "id": "fixture:request.id" }, "field": "status", "value": "denied" }, { "type": "notification_sent", "recipient": "employee@example.invalid", "message_contains": "denied", "count": 1 }, { "type": "field_editable", "module": "acme_requests", "field": "status", "as": "fixture:approver" }, { "type": "field_readonly", "module": "acme_requests", "field": "status", "as": "fixture:employee" } ], "actor": "fixture:employee", "effects": { "save_events": "allow" }, "steps": [ { "type": "respond", "element": "notifyApprover", "state": "deny", "as": "fixture:approver" } ]}tests/acme_request_unchanged.test.workflow.json
{ "identifier": "acme_request_unchanged", "workflow": "acme_request_approval", "fixtures": [ { "type": "user", "alias": "employee", "email": "employee@example.invalid", "roles": [ "acme_requester" ], "fields": { "name": "ACME Requester" } }, { "type": "user", "alias": "approver", "email": "approver@example.invalid", "roles": [ "acme_approver" ], "fields": { "name": "ACME Approver" } }, { "type": "entity", "module": "acme_requests", "alias": "request", "fields": { "name": "ACME Inc. request", "status": "draft", "employee": "fixture:employee", "approver": "fixture:approver" } } ], "trigger": { "type": "update_resource", "entity": "fixture:request", "fields": { "name": "ACME Renamed" }, "dispatch_workflow": false }, "assertions": [ { "type": "notification_not_sent", "recipient": "employee@example.invalid" }, { "type": "notification_not_sent", "recipient": "approver@example.invalid" } ], "actor": "fixture:employee"}translations/de.json
{ "modules": { "acme_requests": { "title": "Anträge", "fields": { "name": "Name", "status": "Status", "employee": "Antragsteller", "approver": "Genehmiger" }, "tabs": { "general": "Allgemein" }, "choice": { "status": { "draft": "Entwurf", "open": "Offen", "approved": "Genehmigt", "denied": "Abgelehnt" } } } }, "buttons": { "acme_submit_request": "Einreichen" }}translations/en.json
{ "modules": { "acme_requests": { "title": "Requests", "fields": { "name": "Name", "status": "Status", "employee": "Requester", "approver": "Approver" }, "tabs": { "general": "General" }, "choice": { "status": { "draft": "Draft", "open": "Open", "approved": "Approved", "denied": "Denied" } } } }, "buttons": { "acme_submit_request": "Submit" }}translations/translations.bake.json
[ { "resource_translations": "de", "resource": { "identifier": "de", "code": "de_DE", "name": "Deutsch", "dir": "ltr", "provider": "deDE", "data": "${file('translations/de.json')}" } }, { "resource_translations": "en", "resource": { "identifier": "en", "code": "en_US", "name": "English", "dir": "ltr", "provider": "enUS", "data": "${file('translations/en.json')}" } }]workflows/acme_request_approval.workflow.bake.json
[ { "resource_workflow": "acme_request_approval", "resource": { "identifier": "acme_request_approval", "title": "ACME Inc. — Request approval", "entry": "acme_submit_request", "elements": [ { "name": "acme_submit_request", "type": "event/webhook", "options": { "module": "acme_requests", "public": false, "roles": [ "acme_requester", "acme_approver" ] }, "set": { "request": "prototype:" }, "to": { "default": { "isDraft": [ "default" ] } } }, { "name": "isDraft", "type": "flow/if", "options": { "condition": "$request.status == \"draft\"" }, "to": { "default": { "submitStatus": [ "default" ] } } }, { "name": "submitStatus", "type": "op/set", "options": { "recipes": { "status": "\"open\"" } }, "in": { "default": "$request" }, "to": { "default": { "saveSubmission": [ "default" ] } } }, { "name": "saveSubmission", "type": "action/save", "options": { "triggerEvents": true }, "to": {} }, { "name": "requestUpdated", "type": "event/update", "options": { "module": "acme_requests" }, "in": {}, "set": { "request": "default:", "original": "original:" }, "to": { "default": { "statusChanged": [ "default" ] } } }, { "name": "notifyApprover", "type": "action/notify", "options": { "notification": { "title": "ACME Inc. — Request request", "message": "Please approve or deny this request.", "recipients": "$request.approver", "entity": "$request", "type": "info", "actions": { ".recipe": "[{\"action\": \"approve\", \"title\": \"Approve\"}, {\"action\": \"deny\", \"title\": \"Deny\"}]" } } }, "in": {}, "set": { "decision": "action:" }, "to": { "action": { "reloadRequest": [ "default" ] } } }, { "name": "reloadRequest", "type": "source/entities", "options": { "module": "acme_requests", "first": true, "filters": [ [ { "left": { "type": "field", "value": "id" }, "operator": "=", "right": { "type": "recipe", "value": "$request.id" } } ] ] }, "in": {}, "set": {}, "to": { "default": { "setDecision": [ "default" ] } } }, { "name": "setDecision", "type": "op/set", "options": { "recipes": { "status": "$decision == \"approve\" ? \"approved\" : \"denied\"" } }, "in": {}, "set": {}, "to": { "default": { "saveDecision": [ "default" ] } } }, { "name": "saveDecision", "type": "action/save", "options": { "triggerEvents": true }, "in": {}, "set": {}, "to": {} }, { "name": "statusChanged", "type": "flow/if", "options": { "condition": "$request.status != $original.status" }, "in": {}, "set": {}, "to": { "default": { "isSubmission": [ "default" ] } } }, { "name": "notifyEmployee", "type": "action/notify", "options": { "notification": { "title": "ACME Inc. — Request decision", "message": { ".recipe": "\"Your request was \" + $request.status + \".\"" }, "recipients": "$request.employee", "entity": "$request", "type": "info" } }, "in": {}, "set": {}, "to": {} }, { "name": "isSubmission", "type": "flow/if", "options": { "condition": "$request.status == \"open\"" }, "to": { "default": { "notifyApprover": [ "default" ] }, "else": { "notifyEmployee": [ "default" ] } } } ], "async": false, "events": [ { "identifier": "acme_submit_request", "type": "webhook", "module": "acme_requests" }, { "identifier": "requestUpdated", "type": "update", "module": "acme_requests", "async": false } ] } }]Source: brezel/api@572da72238ad1aab7b60287c898eaede2c08a876
Agents: system_authoring_command reference kind=feature query=acme_approval_status
How it works
Section titled “How it works”The example moves a saved request from draft to open and then approved or denied. The layout button invokes a guarded webhook. Update processing compares against $original.status, so unchanged saves do not send another notification. Role permissions prevent ordinary requesters from writing the status directly.
Test it
Section titled “Test it”Copy the example files relative to your system root. Merge existing native modules, menus and roles; rename all related identifiers together. Apply preview data only in a disposable preview. Run the plan, apply and tests against that preview:
php bakery plan <system>php bakery apply <system>php bakery system test-run <system>Run the approve/deny and unchanged cases. Exercise each allowed and forbidden transition with non-root actors. Set effects.save_events: allow to observe update events; verify field write permissions and checkpoint responses.
Other ways
Section titled “Other ways”- Recommended for rejecting invalid transitions:
beforeUpdate/beforeSavewithaction/validate. Validation works on ordinary saves; mutating values in these events does not. The exact custom validation recipe needs a target-system test. - Recommended for UI convenience: hide unavailable transitions or make fields read-only with option recipes. Keep server checks.
- Recommended for conditional required values: field
options.rules, e.g.required_if; rules do not implement a whole transition graph. - Recommended for a board display with a relation-backed column: Kanban. Its detailed options remain unverified here; a choice status alone is not the documented relation-backed column.
Reference
Section titled “Reference”Update events · Field permissions · Workflow tests · Assertions