Skip to content

Commit b0e2503

Browse files
authored
Merge pull request #697 from code16/custom-breadcrumb-attr-localized
Allow localized + limit breadcrumb custom label
2 parents e5f52de + 926d7e1 commit b0e2503

9 files changed

Lines changed: 148 additions & 38 deletions

File tree

demo/app/Sharp/Posts/PostShow.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function buildShowConfig(): void
107107
{
108108
$this
109109
->configureEntityState('state', PostStateHandler::class)
110-
->configureBreadcrumbCustomLabelAttribute('breadcrumb')
110+
->configureBreadcrumbCustomLabelAttribute('title', limit: 30, localized: true)
111111
->configurePageTitleAttribute('title', localized: true)
112112
->configureDeleteConfirmationText('Are you sure you want to delete this post (this will permanently delete its data)?')
113113
->configurePrimaryInstanceCommands([
@@ -143,10 +143,6 @@ public function find(mixed $id): array
143143
$post = Post::with('attachments', 'attachments.document')->findOrFail($id);
144144

145145
return $this
146-
->setCustomTransformer('breadcrumb', fn ($value, $instance) => str()
147-
->of($instance->getTranslation('title', 'en'))
148-
->limit(30)
149-
)
150146
->setCustomTransformer('publication', fn ($value, Post $instance) => [
151147
'is_planned' => $instance->isOnline() && $instance->published_at->isFuture(),
152148
'published_at' => $instance->published_at->isoFormat('LLL'),

src/Data/Show/ShowConfigData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function __construct(
1717
public bool $isSingle = false,
1818
public ?ConfigCommandsData $commands = null,
1919
public ?string $titleAttribute = null,
20-
public ?string $breadcrumbAttribute = null,
2120
public ?string $editButtonLabel = null,
2221
public ?EntityStateData $state = null,
2322
) {}

src/Http/Controllers/FormController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function edit(string $globalFilter, string $parentUri, EntityKey $entityK
8484
? $form->instance($instanceId)
8585
: $form->newInstance();
8686

87-
if ($breadcrumbLabel = $formData[$form->getBreadcrumbCustomLabelAttribute()] ?? false) {
87+
if ($breadcrumbLabel = $form->getBreadcrumbCustomLabel($formData)) {
8888
sharp()->context()->breadcrumb()->setCurrentInstanceLabel($breadcrumbLabel);
8989
$titleEntityLabel = $breadcrumbLabel;
9090
}

src/Http/Controllers/ShowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function show(string $globalFilter, string $parentUri, EntityKey $entityK
5353

5454
sharp()->context()
5555
->breadcrumb()
56-
->setCurrentInstanceLabel($showData[$show->getBreadcrumbCustomLabelAttribute()] ?? false);
56+
->setCurrentInstanceLabel($show->getBreadcrumbCustomLabel($showData));
5757

5858
$this->addPreloadHeadersForShowEntityLists($payload);
5959

src/Http/Controllers/SingleShowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function show(string $globalFilter, EntityKey $entityKey)
4949

5050
sharp()->context()
5151
->breadcrumb()
52-
->setCurrentInstanceLabel($showData[$show->getBreadcrumbCustomLabelAttribute()] ?? null);
52+
->setCurrentInstanceLabel($show->getBreadcrumbCustomLabel($showData));
5353

5454
$this->addPreloadHeadersForShowEntityLists($payload);
5555

src/Show/SharpShow.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function showConfig(mixed $instanceId, array $config = []): array
7272
->all();
7373

7474
return tap($config, function (&$config) use ($instanceId) {
75-
$this->appendBreadcrumbCustomLabelAttribute($config);
7675
$this->appendEntityStateToConfig($config, $instanceId);
7776
$this->appendInstanceCommandsToConfig($config, $instanceId);
7877
});

src/Utils/Traits/HandleCustomBreadcrumb.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,38 @@
22

33
namespace Code16\Sharp\Utils\Traits;
44

5+
use Illuminate\Support\Str;
6+
57
trait HandleCustomBreadcrumb
68
{
79
protected ?string $breadcrumbAttribute = null;
10+
protected bool $breadcrumbLocalized = false;
11+
protected ?int $breadcrumbLimit = null;
812

9-
public function configureBreadcrumbCustomLabelAttribute(string $breadcrumbAttribute): self
10-
{
13+
public function configureBreadcrumbCustomLabelAttribute(
14+
string $breadcrumbAttribute,
15+
?int $limit = null,
16+
bool $localized = false,
17+
): self {
1118
$this->breadcrumbAttribute = $breadcrumbAttribute;
19+
$this->breadcrumbLimit = $limit;
20+
$this->breadcrumbLocalized = $localized;
1221

1322
return $this;
1423
}
1524

16-
public function getBreadcrumbCustomLabelAttribute(): ?string
25+
public function getBreadcrumbCustomLabel(array $data): ?string
1726
{
18-
return $this->breadcrumbAttribute;
19-
}
20-
21-
protected function appendBreadcrumbCustomLabelAttribute(array &$config): void
22-
{
23-
if ($this->breadcrumbAttribute) {
24-
$config['breadcrumbAttribute'] = $this->breadcrumbAttribute;
27+
if (! $this->breadcrumbAttribute) {
28+
return null;
2529
}
30+
31+
$label = $this->breadcrumbLocalized
32+
? $data[$this->breadcrumbAttribute][$this->getDataLocalizations()[0]] ?? null
33+
: $data[$this->breadcrumbAttribute] ?? null;
34+
35+
return $label && $this->breadcrumbLimit
36+
? Str::limit($label, $this->breadcrumbLimit)
37+
: $label;
2638
}
2739
}

tests/Http/BreadcrumbTest.php

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Code16\Sharp\Form\Fields\SharpFormEditorField;
4+
use Code16\Sharp\Show\Fields\SharpShowTextField;
45
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
56
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
67
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
@@ -106,7 +107,7 @@
106107
);
107108
});
108109

109-
it('uses custom labels on show leaf if configured, based on unformatted data', function () {
110+
it('uses custom labels on show leaf if configured', function () {
110111
fakeShowFor('person', new class() extends PersonShow
111112
{
112113
public function buildShowConfig(): void
@@ -139,7 +140,83 @@ public function find($id): array
139140
);
140141
});
141142

142-
it('uses custom labels on form leaf if configured, based on unformatted data', function () {
143+
it('uses custom labels limit on show leaf if configured', function () {
144+
fakeShowFor('person', new class() extends PersonShow
145+
{
146+
public function buildShowConfig(): void
147+
{
148+
$this->configureBreadcrumbCustomLabelAttribute('name', limit: 20);
149+
}
150+
151+
public function find($id): array
152+
{
153+
return $this->transform([
154+
'id' => 1,
155+
'name' => 'A very long name that should be limited',
156+
]);
157+
}
158+
});
159+
160+
$this
161+
->get(
162+
route('code16.sharp.show.show', [
163+
'parentUri' => 's-list/person/',
164+
'person',
165+
1,
166+
])
167+
)
168+
->assertOk()
169+
->assertInertia(fn (Assert $page) => $page
170+
->where('breadcrumb.items.0.label', 'List')
171+
// Data is not formatted for breadcrumb:
172+
->where('breadcrumb.items.1.label', 'A very long name tha...')
173+
);
174+
});
175+
176+
it('uses localized custom labels on show leaf if configured', function () {
177+
fakeShowFor('person', new class() extends PersonShow
178+
{
179+
public function buildShowFields(FieldsContainer $showFields): void
180+
{
181+
$showFields->addField(SharpShowTextField::make('name')->setLocalized());
182+
}
183+
184+
public function buildShowConfig(): void
185+
{
186+
$this->configureBreadcrumbCustomLabelAttribute('name', localized: true);
187+
}
188+
189+
public function find($id): array
190+
{
191+
return $this->transform([
192+
'id' => 1,
193+
'name' => ['fr' => 'Marie Curie', 'en' => 'Mary Curry'],
194+
]);
195+
}
196+
197+
public function getDataLocalizations(): array
198+
{
199+
return ['fr', 'en'];
200+
}
201+
});
202+
203+
$this
204+
->get(
205+
route('code16.sharp.show.show', [
206+
'parentUri' => 's-list/person/',
207+
'person',
208+
1,
209+
])
210+
)
211+
->assertOk()
212+
->assertInertia(fn (Assert $page) => $page
213+
->where('breadcrumb.items.0.label', 'List')
214+
// Data is not formatted for breadcrumb:
215+
->where('breadcrumb.items.1.label', 'Marie Curie')
216+
);
217+
});
218+
219+
it('uses custom labels on form leaf if configured', function () {
143220
fakeFormFor('person', new class() extends PersonForm
144221
{
145222
public function buildFormFields(FieldsContainer $formFields): void
@@ -176,3 +253,46 @@ public function find($id): array
176253
->where('breadcrumb.items.1.label', 'Edit “Marie Curie”')
177254
);
178255
});
256+
257+
it('uses localized custom labels on form leaf if configured', function () {
258+
fakeFormFor('person', new class() extends PersonForm
259+
{
260+
public function buildFormFields(FieldsContainer $formFields): void
261+
{
262+
$formFields->addField(SharpFormEditorField::make('name')->setLocalized());
263+
}
264+
265+
public function buildFormConfig(): void
266+
{
267+
$this->configureBreadcrumbCustomLabelAttribute('name', localized: true);
268+
}
269+
270+
public function find($id): array
271+
{
272+
return $this->transform([
273+
'id' => 1,
274+
'name' => ['fr' => 'Marie Curie', 'en' => 'Mary Curry'],
275+
]);
276+
}
277+
278+
public function getDataLocalizations(): array
279+
{
280+
return ['fr', 'en'];
281+
}
282+
});
283+
284+
$this
285+
->get(
286+
route('code16.sharp.form.edit', [
287+
'parentUri' => 's-list/person',
288+
'person',
289+
1,
290+
])
291+
)
292+
->assertOk()
293+
->assertInertia(fn (Assert $page) => $page
294+
->where('breadcrumb.items.0.label', 'List')
295+
// Data is not formatted for breadcrumb:
296+
->where('breadcrumb.items.1.label', 'Edit “Marie Curie”')
297+
);
298+
});

tests/Http/ShowControllerTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,6 @@ public function buildShowLayout(ShowLayout $showLayout): void
182182
);
183183
});
184184

185-
it('returns show configuration', function () {
186-
fakeShowFor('person', new class() extends PersonShow
187-
{
188-
public function buildShowConfig(): void
189-
{
190-
$this->configureBreadcrumbCustomLabelAttribute('name');
191-
}
192-
});
193-
194-
$this->get('/sharp/root/s-list/person/s-show/person/1')
195-
->assertOk()
196-
->assertInertia(fn (Assert $page) => $page
197-
->where('show.config.breadcrumbAttribute', 'name')
198-
);
199-
});
200-
201185
it('gets show data for an instance in a single show case', function () {
202186
sharp()->config()->declareEntity(SinglePersonEntity::class);
203187

0 commit comments

Comments
 (0)