From 730cee07065c001d2d7491df36f85dd86bc2a584 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Wed, 18 Feb 2026 12:00:28 +0100 Subject: [PATCH] Allow custom label attribute in collection --- src/Console/ConfigureCmsCommand.php | 1 + src/OzuCms/OzuCollectionConfig.php | 13 ++++++++++++ tests/Unit/OzuCollectionConfigTest.php | 28 ++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Console/ConfigureCmsCommand.php b/src/Console/ConfigureCmsCommand.php index 814b947..8e1d024 100644 --- a/src/Console/ConfigureCmsCommand.php +++ b/src/Console/ConfigureCmsCommand.php @@ -157,6 +157,7 @@ private function updateCmsConfigurationFor($model, int $order = 1, bool $isSubCo 'isDeletable' => $collection->isDeletable(), 'subCollections' => $collection->subCollections() ->map(fn ($subCollectionClass) => app($subCollectionClass)->ozuCollectionKey()), + 'labelAttribute' => $collection->labelAttribute(), 'order' => $order, 'list' => [ 'isReorderable' => $list->isReorderable(), diff --git a/src/OzuCms/OzuCollectionConfig.php b/src/OzuCms/OzuCollectionConfig.php index 7fdc677..50d0c6f 100644 --- a/src/OzuCms/OzuCollectionConfig.php +++ b/src/OzuCms/OzuCollectionConfig.php @@ -12,6 +12,7 @@ class OzuCollectionConfig protected ?string $autoDeployDateField = null; private bool $isCreatable = true; private bool $isDeletable = true; + private string $labelAttribute = 'title'; private array $subCollections = []; public function setLabel(string $label): self @@ -71,6 +72,18 @@ public function setIsDeletable(bool $isDeletable = true): self return $this; } + public function overrideLabelAttributeInDashboard(string $attribute): self + { + $this->labelAttribute = $attribute; + + return $this; + } + + public function labelAttribute(): string + { + return $this->labelAttribute; + } + public function label(): string { return $this->label ?? 'no label'; diff --git a/tests/Unit/OzuCollectionConfigTest.php b/tests/Unit/OzuCollectionConfigTest.php index fbd995c..3ef9add 100644 --- a/tests/Unit/OzuCollectionConfigTest.php +++ b/tests/Unit/OzuCollectionConfigTest.php @@ -10,6 +10,7 @@ ->and($ozuCollectionConfig->autoDeployDateField())->toBeNull() ->and($ozuCollectionConfig->isCreatable())->toBeTrue() ->and($ozuCollectionConfig->isDeletable())->toBeTrue() + ->and($ozuCollectionConfig->labelAttribute())->toBe('title') ->and($ozuCollectionConfig)->toHaveProperties([ 'label', 'icon', @@ -17,6 +18,7 @@ 'autoDeployDateField', 'isCreatable', 'isDeletable', + 'labelAttribute', ]) ->and($ozuCollectionConfig::class)->toHaveMethods([ 'setLabel', @@ -25,6 +27,9 @@ 'setAutoDeployDateField', 'setIsCreatable', 'setIsDeletable', + 'addSubCollection', + 'subCollections', + 'overrideLabelAttributeInDashboard', 'label', 'icon', 'hasPublicationState', @@ -32,6 +37,7 @@ 'hasAutoDeployDateField', 'isCreatable', 'isDeletable', + 'labelAttribute', ]); }); @@ -67,6 +73,22 @@ ->and($ozuCollectionConfig->isDeletable())->toBeFalse(); }); +it('allows to add a sub collection', function () { + $ozuCollectionConfig = new OzuCollectionConfig(); + + $ozuCollectionConfig->addSubCollection('CollectionClass'); + + expect($ozuCollectionConfig->subCollections()->toArray())->toBe(['CollectionClass']); +}); + +it('allows to override label attribute', function () { + $ozuCollectionConfig = new OzuCollectionConfig(); + + $ozuCollectionConfig->overrideLabelAttributeInDashboard('name'); + + expect($ozuCollectionConfig->labelAttribute())->toBe('name'); +}); + it('allows to chain methods', function () { $ozuCollectionConfig = new OzuCollectionConfig(); @@ -76,12 +98,14 @@ ->setHasPublicationState() ->setAutoDeployDateField('date') ->setIsCreatable(false) - ->setIsDeletable(false); + ->setIsDeletable(false) + ->overrideLabelAttributeInDashboard('name'); expect($ozuCollectionConfig->label())->toBe('label') ->and($ozuCollectionConfig->icon())->toBe('icon') ->and($ozuCollectionConfig->hasPublicationState())->toBeTrue() ->and($ozuCollectionConfig->autoDeployDateField())->toBe('date') ->and($ozuCollectionConfig->isCreatable())->toBeFalse() - ->and($ozuCollectionConfig->isDeletable())->toBeFalse(); + ->and($ozuCollectionConfig->isDeletable())->toBeFalse() + ->and($ozuCollectionConfig->labelAttribute())->toBe('name'); });