Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion demo/app/Sharp/Entities/ProfileEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
3 changes: 3 additions & 0 deletions demo/resources/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"profile": "Profile"
}
3 changes: 3 additions & 0 deletions demo/resources/lang/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"profile": "Profil"
}
6 changes: 4 additions & 2 deletions docs/guide/entity-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 {}
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Context/SharpBreadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions src/Http/Controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/SingleShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Entities/BaseSharpEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Entities/SharpDashboardEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 15 additions & 2 deletions src/Utils/Entities/SharpEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Http/BreadcrumbTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
use Code16\Sharp\Tests\Fixtures\Entities\PersonWithDynamicLabelEntity;
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow;
use Code16\Sharp\Utils\Entities\SharpEntityManager;
Expand Down
Loading