diff --git a/demo/app/Sharp/Entities/ProfileEntity.php b/demo/app/Sharp/Entities/ProfileEntity.php index 2fb331a2f..d6d040798 100644 --- a/demo/app/Sharp/Entities/ProfileEntity.php +++ b/demo/app/Sharp/Entities/ProfileEntity.php @@ -11,5 +11,9 @@ class ProfileEntity extends SharpEntity protected bool $isSingle = true; protected ?string $show = ProfileSingleShow::class; protected ?string $form = ProfileSingleForm::class; - protected string $label = 'My profile'; + + protected function getLabel(): string + { + return __('profile'); + } } diff --git a/demo/resources/lang/en.json b/demo/resources/lang/en.json new file mode 100644 index 000000000..68956b3cd --- /dev/null +++ b/demo/resources/lang/en.json @@ -0,0 +1,3 @@ +{ + "profile": "Profile" +} \ No newline at end of file diff --git a/demo/resources/lang/fr.json b/demo/resources/lang/fr.json new file mode 100644 index 000000000..17e2097dc --- /dev/null +++ b/demo/resources/lang/fr.json @@ -0,0 +1,3 @@ +{ + "profile": "Profil" +} \ No newline at end of file diff --git a/docs/guide/entity-class.md b/docs/guide/entity-class.md index 82b2d9825..36bddb821 100644 --- a/docs/guide/entity-class.md +++ b/docs/guide/entity-class.md @@ -25,10 +25,11 @@ The class must extend `Code16\Sharp\Entities\SharpEntity`. The easiest way to de ```php class ProductEntity extends SharpEntity { + protected string $label = 'Product'; protected ?string $list = ProductList::class; protected ?string $show = ProductShow::class; protected ?string $form = ProductForm::class; - protected string $label = 'Product'; +} ``` Here is the full list: @@ -54,6 +55,7 @@ class SalesDashboardEntity extends SharpDashboardEntity If you need more control, you can override these instead of the attributes: ```php +protected function getLabel(): string {} protected function getList(): ?string {} protected function getShow(): ?string {} protected function getForm(): ?string {} @@ -65,7 +67,7 @@ The last one, `getPolicy()`, allows you to return a `SharpEntityPolicy` implemen ```php class MyEntity extends SharpEntity { - // [...] + // ... protected function getPolicy(): string|SharpEntityPolicy|null { diff --git a/src/Http/Context/SharpBreadcrumb.php b/src/Http/Context/SharpBreadcrumb.php index 32bd76785..f0dfd7602 100644 --- a/src/Http/Context/SharpBreadcrumb.php +++ b/src/Http/Context/SharpBreadcrumb.php @@ -241,7 +241,7 @@ private function getEntityLabelForInstance(BreadcrumbItem $item, bool $isLeaf): return app(SharpEntityManager::class) ->entityFor($item->key) - ->getLabel((new EntityKey($item->key))->subEntity()); + ->getLabelOrFail((new EntityKey($item->key))->subEntity()); } private function isSameEntityKeys(string $key1, string $key2, bool $compareBaseEntities): bool diff --git a/src/Http/Controllers/Api/Commands/ApiEntityListQuickCreationCommandController.php b/src/Http/Controllers/Api/Commands/ApiEntityListQuickCreationCommandController.php index 13d168f79..ed31d995b 100644 --- a/src/Http/Controllers/Api/Commands/ApiEntityListQuickCreationCommandController.php +++ b/src/Http/Controllers/Api/Commands/ApiEntityListQuickCreationCommandController.php @@ -36,7 +36,7 @@ public function create(EntityKey $entityKey) ->setEntityKey($entityKey) ->setFormInstance($form) ->setTitle(__('sharp::breadcrumb.form.create_entity', [ - 'entity' => $entity->getLabel($entityKey->subEntity()), + 'entity' => $entity->getLabelOrFail($entityKey->subEntity()), ])); $quickCreationHandler->buildCommandConfig(); diff --git a/src/Http/Controllers/FormController.php b/src/Http/Controllers/FormController.php index c7997a0ff..6d82c0faa 100644 --- a/src/Http/Controllers/FormController.php +++ b/src/Http/Controllers/FormController.php @@ -46,7 +46,7 @@ public function create(string $parentUri, EntityKey $entityKey) 'form' => FormData::from([ ...$data, 'title' => $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [ - 'entity' => $entity->getLabel($entityKey->subEntity()), + 'entity' => $entity->getLabelOrFail($entityKey->subEntity()), ]), ]), 'breadcrumb' => BreadcrumbData::from([ @@ -86,8 +86,10 @@ public function edit(string $parentUri, EntityKey $entityKey, ?string $instanceI 'form' => FormData::from([ ...$data, 'title' => $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [ - 'entity' => sharp()->context()->breadcrumb()->getParentShowCachedBreadcrumbLabel() - ?: $entity->getLabel($entityKey->subEntity()), + 'entity' => sharp() + ->context() + ->breadcrumb() + ->getParentShowCachedBreadcrumbLabel() ?: $entity->getLabelOrFail($entityKey->subEntity()), ]), ]), 'breadcrumb' => BreadcrumbData::from([ diff --git a/src/Http/Controllers/ShowController.php b/src/Http/Controllers/ShowController.php index d5444582c..1094aec29 100644 --- a/src/Http/Controllers/ShowController.php +++ b/src/Http/Controllers/ShowController.php @@ -36,7 +36,7 @@ public function show(string $parentUri, EntityKey $entityKey, string $instanceId $showData = $show->instance($instanceId); $payload = ShowData::from([ - 'title' => $showData[$show->titleAttribute()] ?? $entity->getLabel($entityKey->subEntity()), + 'title' => $showData[$show->titleAttribute()] ?? $entity->getLabelOrFail($entityKey->subEntity()), 'config' => $show->showConfig($instanceId), 'fields' => $show->fields(), 'layout' => $show->showLayout(), diff --git a/src/Http/Controllers/SingleShowController.php b/src/Http/Controllers/SingleShowController.php index 4418c2aad..6fa51f38f 100644 --- a/src/Http/Controllers/SingleShowController.php +++ b/src/Http/Controllers/SingleShowController.php @@ -33,7 +33,7 @@ public function show(EntityKey $entityKey) $showData = $show->instance(null); $payload = ShowData::from([ - 'title' => $showData[$show->titleAttribute()] ?? $entity->getLabel($entityKey->subEntity()), + 'title' => $showData[$show->titleAttribute()] ?? $entity->getLabelOrFail($entityKey->subEntity()), 'config' => $show->showConfig(null), 'fields' => $show->fields(), 'layout' => $show->showLayout(), diff --git a/src/Utils/Entities/BaseSharpEntity.php b/src/Utils/Entities/BaseSharpEntity.php index c07149f48..f9b6e5d76 100644 --- a/src/Utils/Entities/BaseSharpEntity.php +++ b/src/Utils/Entities/BaseSharpEntity.php @@ -32,7 +32,7 @@ final public function getPolicyOrDefault(): SharpEntityPolicy return $policy; } - abstract public function getLabel(): string; + abstract protected function getLabel(): string; final public function isDashboard(): bool { diff --git a/src/Utils/Entities/SharpDashboardEntity.php b/src/Utils/Entities/SharpDashboardEntity.php index 2a1216252..f91d5757c 100644 --- a/src/Utils/Entities/SharpDashboardEntity.php +++ b/src/Utils/Entities/SharpDashboardEntity.php @@ -24,7 +24,7 @@ final public function hasView(): bool return $this->getView() !== null; } - final public function getLabel(): string + protected function getLabel(): string { return $this->label; } diff --git a/src/Utils/Entities/SharpEntity.php b/src/Utils/Entities/SharpEntity.php index 0b787bc40..d9a23a183 100644 --- a/src/Utils/Entities/SharpEntity.php +++ b/src/Utils/Entities/SharpEntity.php @@ -51,6 +51,19 @@ final public function getFormOrFail(?string $subEntity = null): SharpForm return $form instanceof SharpForm ? $form : app($form); } + final public function getLabelOrFail(?string $subEntity = null): string + { + $label = $subEntity + ? $this->getMultiforms()[$subEntity][1] ?? null + : $this->getLabel(); + + if ($label === null) { + throw new SharpInvalidEntityKeyException("The label of the subform for the entity [{$this->entityKey}:{$subEntity}] was not found."); + } + + return $label; + } + final public function isActionProhibited(string $action): bool { return in_array($action, $this->prohibitedActions); @@ -61,9 +74,9 @@ final public function isSingle(): bool return $this->isSingle; } - final public function getLabel(?string $subEntity = null): string + protected function getLabel(): string { - return $subEntity ? $this->getMultiforms()[$subEntity][1] : $this->label; + return $this->label; } protected function getList(): ?SharpEntityList diff --git a/tests/Http/BreadcrumbTest.php b/tests/Http/BreadcrumbTest.php index f19ac489d..17d0514a2 100644 --- a/tests/Http/BreadcrumbTest.php +++ b/tests/Http/BreadcrumbTest.php @@ -1,6 +1,7 @@