.template`): the blocks of the page of one record, edited with the same block editor * as a page. Whoever may manage the list (`.manage`) may change its layout; nobody else sees it. */ class TemplateController { public function show(string $resource): JsonResponse { $definition = $this->resource($resource); return response()->json($this->payload($definition)); } public function update(Request $request, string $resource): JsonResponse { $definition = $this->resource($resource); $allowed = Blocks::templateTypes($resource); $data = $request->validate(Blocks::rules((array) $request->input('blocks', []), $allowed, bindings: true)); $template = BlockTemplate::query()->firstOrNew(['resource' => $resource]); $template->blocks = Blocks::clean((array) ($data['blocks'] ?? []), $allowed); $template->save(); app(AuditTrail::class)->about($template); return response()->json($this->payload($definition)); } /** @return array */ private function resource(string $key): array { $definition = Resources::find($key); abort_if($definition === null || $definition['template'] === false, 404); Gate::authorize($key.'.manage'); return $definition; } /** * @param array $definition * @return array */ private function payload(array $definition): array { $template = BlockTemplate::query()->where('resource', $definition['key'])->first(); $allowed = Blocks::templateTypes($definition['key']); return [ 'resource' => ['key' => $definition['key'], 'label' => $definition['label'], 'singular' => $definition['singular'], 'simple_label' => $definition['simple_label'], 'simple_singular' => $definition['simple_singular']], // What `{record.}` can name: the fields of the list, and the number of the record. 'record_fields' => [['key' => 'id', 'label' => 'Number'], ...array_values(array_map(fn (array $field) => ['key' => $field['key'], 'label' => $field['label']], $definition['fields']))], 'blocks' => Blocks::forEditor(is_array($template?->blocks) ? $template->blocks : []), 'block_types' => Blocks::catalogue($allowed), ]; } }