From 85c5d0ee7f41c065090d8530ce07c87658dde58e Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Sun, 16 Nov 2025 15:28:33 +0100 Subject: [PATCH 1/2] Add the possibility to specify layout section order --- formwork/src/Fields/Layout/Layout.php | 9 ++++++--- formwork/src/Fields/Layout/Section.php | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/formwork/src/Fields/Layout/Layout.php b/formwork/src/Fields/Layout/Layout.php index 7cd868ef2..8c68a7e77 100644 --- a/formwork/src/Fields/Layout/Layout.php +++ b/formwork/src/Fields/Layout/Layout.php @@ -19,10 +19,9 @@ class Layout /** * @param array $data */ - public function __construct(array $data, Translation $translation) + public function __construct(protected array $data, protected Translation $translation) { $this->type = $data['type']; - $this->sections = new SectionCollection($data['sections'] ?? [], $translation); } /** Get layout type @@ -38,6 +37,10 @@ public function type(): string */ public function sections(): SectionCollection { - return $this->sections; + return $this->sections ??= (new SectionCollection( + $this->data['sections'] ?? [], + $this->translation, + )) + ->sort(sortBy: fn(Section $a, Section $b): int => $a->order() <=> $b->order()); } } diff --git a/formwork/src/Fields/Layout/Section.php b/formwork/src/Fields/Layout/Section.php index 811dbd066..f1d1a757c 100644 --- a/formwork/src/Fields/Layout/Section.php +++ b/formwork/src/Fields/Layout/Section.php @@ -2,13 +2,16 @@ namespace Formwork\Fields\Layout; +use Formwork\Data\Contracts\Arrayable; +use Formwork\Data\Traits\DataArrayable; use Formwork\Data\Traits\DataGetter; use Formwork\Translations\Translation; use Formwork\Utils\Str; -class Section +class Section implements Arrayable { use DataGetter; + use DataArrayable; /** * @param array $data @@ -35,4 +38,12 @@ public function label(): string { return Str::interpolate($this->get('label', ''), fn($key) => $this->translation->translate($key)); } + + /** + * Get section order + */ + public function order(): int + { + return (int) $this->get('order', PHP_INT_MAX); + } } From 6c4bc6930605becc96353b385de2e747de341ff8 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Sun, 16 Nov 2025 15:39:18 +0100 Subject: [PATCH 2/2] Correctly handle null section order --- formwork/src/Fields/Layout/Section.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formwork/src/Fields/Layout/Section.php b/formwork/src/Fields/Layout/Section.php index f1d1a757c..0431b13ee 100644 --- a/formwork/src/Fields/Layout/Section.php +++ b/formwork/src/Fields/Layout/Section.php @@ -44,6 +44,6 @@ public function label(): string */ public function order(): int { - return (int) $this->get('order', PHP_INT_MAX); + return (int) ($this->get('order') ?? PHP_INT_MAX); } }