Skip to content

Commit 8a50434

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents c1fe5a8 + 25d74b2 commit 8a50434

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

docs/guide/link-to.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ To manually build the querystring (which you should avoid).
130130

131131
To generate a list > show > form breadcrumb, instead of (by default) just a list > form.
132132

133+
### `withListEntityKey(string $entityClassOrKey)`
134+
135+
`LinkToShow` and `LinkToForm` only
136+
137+
Allows specifying the entity key of the list, very useful in multi-entities lists (see the [Entity map feature](building-entity-list.md#entity-map)).
138+
133139
### `withBreadcrumb(Closure $closure)`
134140

135141
`LinkToForm` and `LinkToShowPage` only

src/Utils/Links/LinkToForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function generateUrl(): string
2323
return route('code16.sharp.form.edit', [
2424
'parentUri' => sprintf(
2525
's-list/%s%s',
26-
$this->entityKey,
26+
$this->listEntityKey ?: $this->entityKey,
2727
$this->throughShowPage ? '/'.parent::generateUri() : ''
2828
),
2929
'entityKey' => $this->entityKey,
@@ -32,7 +32,7 @@ protected function generateUrl(): string
3232
}
3333

3434
return route('code16.sharp.form.create', [
35-
'parentUri' => sprintf('s-list/%s', $this->entityKey),
35+
'parentUri' => sprintf('s-list/%s', $this->listEntityKey ?: $this->entityKey),
3636
'entityKey' => $this->entityKey,
3737
]);
3838
}

src/Utils/Links/LinkToShowPage.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Code16\Sharp\Utils\Links;
44

55
use Closure;
6+
use Code16\Sharp\Utils\Entities\SharpEntityManager;
67

78
class LinkToShowPage extends SharpLinkTo
89
{
910
protected string $instanceId;
1011
protected BreadcrumbBuilder $breadcrumbBuilder;
12+
protected ?string $listEntityKey = null;
1113

1214
public static function make(string $entityClassOrKey, string $instanceId): self
1315
{
@@ -28,6 +30,13 @@ public function withBreadcrumb(Closure $closure): self
2830
return $this;
2931
}
3032

33+
public function withListEntityKey(string $entityClassOrKey): self
34+
{
35+
$this->listEntityKey = app(SharpEntityManager::class)->entityKeyFor($entityClassOrKey);
36+
37+
return $this;
38+
}
39+
3140
public function renderAsUrl(): string
3241
{
3342
if (isset($this->breadcrumbBuilder)) {
@@ -47,7 +56,7 @@ public function renderAsUrl(): string
4756
protected function generateUrl(): string
4857
{
4958
return route('code16.sharp.show.show', [
50-
'parentUri' => sprintf('s-list/%s', $this->entityKey),
59+
'parentUri' => sprintf('s-list/%s', $this->listEntityKey ?: $this->entityKey),
5160
'entityKey' => $this->entityKey,
5261
'instanceId' => $this->instanceId,
5362
]);

tests/Unit/Utils/SharpLinkToTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Code16\Sharp\Exceptions\SharpInvalidBreadcrumbItemException;
55
use Code16\Sharp\Filters\SelectFilter;
66
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
7+
use Code16\Sharp\Tests\Fixtures\Entities\PersonPhysicistEntity;
78
use Code16\Sharp\Utils\Links\BreadcrumbBuilder;
89
use Code16\Sharp\Utils\Links\LinkToDashboard;
910
use Code16\Sharp\Utils\Links\LinkToEntityList;
@@ -28,6 +29,15 @@
2829
);
2930
});
3031

32+
it('allows to generate a link to a form with another key for the list', function () {
33+
$this->assertEquals(
34+
'<a href="http://localhost/sharp/s-list/my-list-entity/s-form/my-entity/23" title="">test</a>',
35+
LinkToForm::make('my-entity', 23)
36+
->withListEntityKey('my-list-entity')
37+
->renderAsText('test'),
38+
);
39+
});
40+
3141
it('allows to generate a link to a form through a show page', function () {
3242
$this->assertEquals(
3343
'<a href="http://localhost/sharp/s-list/my-entity/s-show/my-entity/23/s-form/my-entity/23" title="">test</a>',
@@ -37,6 +47,16 @@
3747
);
3848
});
3949

50+
it('allows to generate a link to a form through a show page with another key for the list', function () {
51+
$this->assertEquals(
52+
'<a href="http://localhost/sharp/s-list/my-list-entity/s-show/my-entity/23/s-form/my-entity/23" title="">test</a>',
53+
LinkToForm::make('my-entity', 23)
54+
->withListEntityKey('my-list-entity')
55+
->throughShowPage()
56+
->renderAsText('test'),
57+
);
58+
});
59+
4060
it('allows to generate a link to a show page', function () {
4161
$this->assertEquals(
4262
'<a href="http://localhost/sharp/s-list/my-entity/s-show/my-entity/23" title="">test</a>',
@@ -45,6 +65,15 @@
4565
);
4666
});
4767

68+
it('allows to generate a link to a show page with another key for the list', function () {
69+
$this->assertEquals(
70+
'<a href="http://localhost/sharp/s-list/my-list-entity/s-show/my-entity/23" title="">test</a>',
71+
LinkToShowPage::make('my-entity', 23)
72+
->withListEntityKey('my-list-entity')
73+
->renderAsText('test'),
74+
);
75+
});
76+
4877
it('allows to generate a link to a single show page', function () {
4978
$this->assertEquals(
5079
'<a href="http://localhost/sharp/s-show/my-entity" title="">test</a>',
@@ -71,6 +100,19 @@
71100
);
72101
});
73102

103+
it('allows to generate a link to a show page with another key for the list passing SharpEntity classes', function () {
104+
app(SharpConfigBuilder::class)
105+
->declareEntity(PersonEntity::class)
106+
->declareEntity(PersonPhysicistEntity::class);
107+
108+
$this->assertEquals(
109+
'<a href="http://localhost/sharp/s-list/person/s-show/person-physicist/23" title="">test</a>',
110+
LinkToShowPage::make(PersonPhysicistEntity::class, 23)
111+
->withListEntityKey(PersonEntity::class)
112+
->renderAsText('test'),
113+
);
114+
});
115+
74116
it('allows to generate an url to a show page with a specific breadcrumb', function () {
75117
$this->assertEquals(
76118
'http://localhost/sharp/s-list/base-entity/s-show/base-entity/3/s-show/my-entity/4',

0 commit comments

Comments
 (0)