Skip to content

Commit

Permalink
Handle EL createButtonLabel & Show editButtonLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine committed Nov 15, 2024
1 parent 510e132 commit 0e0ae76
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions demo/app/Sharp/Posts/PostList.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function buildList(EntityListFieldsContainer $fields): void
public function buildListConfig(): void
{
$this
->configureCreateButtonLabel('New post...')
->configureEntityState('state', PostStateHandler::class)
->configureDefaultSort('published_at', 'desc')
->configureDelete(confirmationText: 'Are you sure you want to delete this post (this will permanently delete its data)?')
Expand Down
2 changes: 2 additions & 0 deletions docs/guide/building-entity-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ Here is the full list of available methods:

- `configureDelete(bool $hide = false, ?string $onfirmationText = null)`: the first argument is to show / hide the delete command on each instance (shown by default); this is only useful to hide the link if you want to only display the delete action in the Show Page (if you have defined one), this is NOT to be used for authorization purpose (see [dedicated documentation on this topic](entity-authorizations.md)). The second argument is the message to display in the confirmation dialog (a sensible default will be used).

- `configureCreateButtonLabel(string $label)` to set a custom "New..." button label.

## Configure the Entity List

The Entity List must be declared in the correct entity class, as documented here: [Write an entity](entity-class.md)).
Expand Down
1 change: 1 addition & 0 deletions docs/guide/building-show-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ Here is the full list of available methods:
toggle, [see detailed doc](entity-states.md)
- `configurePageTitleAttribute(string $titleAttribute, bool $localized = false)`: define a title to the Show Page, configuring an attribute that should be part of the `find($id)` array
- `configureDeleteConfirmationText(string $text)` to add a custom confirm message when the use clicks on the delete button.
- `configureEditButtonLabel(string $label)` to set a custom "Edit..." button label.

## Accessing the navigation breadcrumb

Expand Down
1 change: 1 addition & 0 deletions src/Data/EntityList/EntityListConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public string $deleteConfirmationText,
public bool $deleteHidden,
public ?string $multiformAttribute,
public ?string $createButtonLabel = null,
public ?ConfigFiltersData $filters = null,
public ?ConfigCommandsData $commands = null,
public ?EntityStateData $state = null,
Expand Down
1 change: 1 addition & 0 deletions src/Data/Show/ShowConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
public ?string $multiformAttribute = null,
public ?string $titleAttribute = null,
public ?string $breadcrumbAttribute = null,
public ?string $editButtonLabel = null,
public ?EntityStateData $state = null,
) {}

Expand Down
13 changes: 11 additions & 2 deletions src/EntityList/SharpEntityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ abstract class SharpEntityList
protected ?string $defaultSortDir = null;
protected bool $deleteHidden = false;
protected ?string $deleteConfirmationText = null;
protected ?string $createButtonLabel = null;

final public function initQueryParams(?array $query): self
{
$this->queryParams = (new EntityListQueryParams(
$this->queryParams = new EntityListQueryParams(
filterContainer: $this->filterContainer(),
filterValues: $this->filterContainer()->getCurrentFilterValues($query),
sortedBy: $query['sort'] ?? $this->defaultSort,
sortedDir: $query['dir'] ?? $this->defaultSortDir,
page: $query['page'] ?? null,
search: ($query['search'] ?? null) ? urldecode($query['search']) : null,
specificIds: $query['ids'] ?? [],
));
);

return $this;
}
Expand Down Expand Up @@ -111,6 +112,7 @@ final public function listConfig(bool $hasShowPage = false): array
'hasShowPage' => $hasShowPage,
'deleteConfirmationText' => $this->deleteConfirmationText ?: trans('sharp::show.delete_confirmation_text'),
'deleteHidden' => $this->deleteHidden,
'createButtonLabel' => $this->createButtonLabel,
'filters' => $this->filterContainer()->getFiltersConfigArray(),
];

Expand Down Expand Up @@ -143,6 +145,13 @@ final public function configureSearchable(bool $searchable = true): self

return $this;
}

final public function configureCreateButtonLabel(string $label): self
{
$this->createButtonLabel = $label;

return $this;
}

final public function configureDelete(bool $hide = false, ?string $confirmationText = null): self
{
Expand Down
9 changes: 9 additions & 0 deletions src/Show/SharpShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class SharpShow
protected ?string $multiformAttribute = null;
protected ?SharpShowTextField $pageTitleField = null;
protected ?string $deleteConfirmationText = null;
protected ?string $editButtonLabel = null;

final public function showLayout(): array
{
Expand Down Expand Up @@ -63,6 +64,7 @@ public function showConfig(mixed $instanceId, array $config = []): array
$config = collect($config)
->merge([
'deleteConfirmationText' => $this->deleteConfirmationText ?: trans('sharp::show.delete_confirmation_text'),
'editButtonLabel' => $this->editButtonLabel,
])
->when($this->multiformAttribute, fn ($collection) => $collection->merge([
'multiformAttribute' => $this->multiformAttribute,
Expand Down Expand Up @@ -92,6 +94,13 @@ final protected function configurePageTitleAttribute(string $attribute, bool $lo

return $this;
}

final public function configureEditButtonLabel(string $label): self
{
$this->editButtonLabel = $label;

return $this;
}

final protected function configureDeleteConfirmationText(string $text): self
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/EntityList/SharpEntityListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public function buildListConfig(): void
'deleteHidden' => false,
'deleteConfirmationText' => trans('sharp::show.delete_confirmation_text'),
'filters' => null,
'createButtonLabel' => null,
]);
});

Expand Down Expand Up @@ -294,3 +295,17 @@ public function reorder(array $ids): void {}
expect($list->listConfig()['reorderable'])->toBeTrue()
->and($list->reorderHandler())->toBeInstanceOf(ReorderHandler::class);
});

it('allows to configure a create button label', function () {
$list = new class() extends FakeSharpEntityList
{
public function buildListConfig(): void
{
$this->configureCreateButtonLabel('New post...');
}
};

$list->buildListConfig();

expect($list->listConfig()['createButtonLabel'])->toEqual('New post...');
});
15 changes: 15 additions & 0 deletions tests/Unit/Show/SharpShowTestDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ public function buildShowConfig(): void
->toHaveKey('multiformAttribute', 'role');
});

it('allows to set an edit button label', function () {
$sharpShow = new class() extends FakeSharpShow
{
public function buildShowConfig(): void
{
$this->configureEditButtonLabel('Edit post');
}
};

$sharpShow->buildShowConfig();

expect($sharpShow->showConfig(1))
->toHaveKey('editButtonLabel', 'Edit post');
});

it('allows to declare a page alert', function () {
$sharpShow = new class() extends FakeSharpShow
{
Expand Down

0 comments on commit 0e0ae76

Please sign in to comment.