Skip to content

Commit d21970b

Browse files
Merge pull request #580 from code16/handle-label-method-in-entities
Handle dynamic label in entities
2 parents af31d4c + 8a6533c commit d21970b

File tree

13 files changed

+42
-14
lines changed

13 files changed

+42
-14
lines changed

demo/app/Sharp/Entities/ProfileEntity.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ class ProfileEntity extends SharpEntity
1111
protected bool $isSingle = true;
1212
protected ?string $show = ProfileSingleShow::class;
1313
protected ?string $form = ProfileSingleForm::class;
14-
protected string $label = 'My profile';
14+
15+
protected function getLabel(): string
16+
{
17+
return __('profile');
18+
}
1519
}

demo/resources/lang/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"profile": "Profile"
3+
}

demo/resources/lang/fr.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"profile": "Profil"
3+
}

docs/guide/entity-class.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ The class must extend `Code16\Sharp\Entities\SharpEntity`. The easiest way to de
2525
```php
2626
class ProductEntity extends SharpEntity
2727
{
28+
protected string $label = 'Product';
2829
protected ?string $list = ProductList::class;
2930
protected ?string $show = ProductShow::class;
3031
protected ?string $form = ProductForm::class;
31-
protected string $label = 'Product';
32+
}
3233
```
3334

3435
Here is the full list:
@@ -54,6 +55,7 @@ class SalesDashboardEntity extends SharpDashboardEntity
5455
If you need more control, you can override these instead of the attributes:
5556

5657
```php
58+
protected function getLabel(): string {}
5759
protected function getList(): ?string {}
5860
protected function getShow(): ?string {}
5961
protected function getForm(): ?string {}
@@ -65,7 +67,7 @@ The last one, `getPolicy()`, allows you to return a `SharpEntityPolicy` implemen
6567
```php
6668
class MyEntity extends SharpEntity
6769
{
68-
// [...]
70+
// ...
6971

7072
protected function getPolicy(): string|SharpEntityPolicy|null
7173
{

src/Http/Context/SharpBreadcrumb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private function getEntityLabelForInstance(BreadcrumbItem $item, bool $isLeaf):
241241

242242
return app(SharpEntityManager::class)
243243
->entityFor($item->key)
244-
->getLabel((new EntityKey($item->key))->subEntity());
244+
->getLabelOrFail((new EntityKey($item->key))->subEntity());
245245
}
246246

247247
private function isSameEntityKeys(string $key1, string $key2, bool $compareBaseEntities): bool

src/Http/Controllers/Api/Commands/ApiEntityListQuickCreationCommandController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function create(EntityKey $entityKey)
3636
->setEntityKey($entityKey)
3737
->setFormInstance($form)
3838
->setTitle(__('sharp::breadcrumb.form.create_entity', [
39-
'entity' => $entity->getLabel($entityKey->subEntity()),
39+
'entity' => $entity->getLabelOrFail($entityKey->subEntity()),
4040
]));
4141

4242
$quickCreationHandler->buildCommandConfig();

src/Http/Controllers/FormController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function create(string $parentUri, EntityKey $entityKey)
4646
'form' => FormData::from([
4747
...$data,
4848
'title' => $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [
49-
'entity' => $entity->getLabel($entityKey->subEntity()),
49+
'entity' => $entity->getLabelOrFail($entityKey->subEntity()),
5050
]),
5151
]),
5252
'breadcrumb' => BreadcrumbData::from([
@@ -86,8 +86,10 @@ public function edit(string $parentUri, EntityKey $entityKey, ?string $instanceI
8686
'form' => FormData::from([
8787
...$data,
8888
'title' => $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [
89-
'entity' => sharp()->context()->breadcrumb()->getParentShowCachedBreadcrumbLabel()
90-
?: $entity->getLabel($entityKey->subEntity()),
89+
'entity' => sharp()
90+
->context()
91+
->breadcrumb()
92+
->getParentShowCachedBreadcrumbLabel() ?: $entity->getLabelOrFail($entityKey->subEntity()),
9193
]),
9294
]),
9395
'breadcrumb' => BreadcrumbData::from([

src/Http/Controllers/ShowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function show(string $parentUri, EntityKey $entityKey, string $instanceId
3636

3737
$showData = $show->instance($instanceId);
3838
$payload = ShowData::from([
39-
'title' => $showData[$show->titleAttribute()] ?? $entity->getLabel($entityKey->subEntity()),
39+
'title' => $showData[$show->titleAttribute()] ?? $entity->getLabelOrFail($entityKey->subEntity()),
4040
'config' => $show->showConfig($instanceId),
4141
'fields' => $show->fields(),
4242
'layout' => $show->showLayout(),

src/Http/Controllers/SingleShowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function show(EntityKey $entityKey)
3333
$showData = $show->instance(null);
3434

3535
$payload = ShowData::from([
36-
'title' => $showData[$show->titleAttribute()] ?? $entity->getLabel($entityKey->subEntity()),
36+
'title' => $showData[$show->titleAttribute()] ?? $entity->getLabelOrFail($entityKey->subEntity()),
3737
'config' => $show->showConfig(null),
3838
'fields' => $show->fields(),
3939
'layout' => $show->showLayout(),

src/Utils/Entities/BaseSharpEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final public function getPolicyOrDefault(): SharpEntityPolicy
3232
return $policy;
3333
}
3434

35-
abstract public function getLabel(): string;
35+
abstract protected function getLabel(): string;
3636

3737
final public function isDashboard(): bool
3838
{

0 commit comments

Comments
 (0)