Skip to content

Commit a5c2155

Browse files
authored
Merge pull request #704 from code16/refactor-breadcrumb
Refactor breadcrumb + "Query parent shows label" feature
2 parents 52ed37e + 3d7b242 commit a5c2155

14 files changed

Lines changed: 624 additions & 118 deletions

File tree

docs/guide/sharp-breadcrumb.md

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,6 @@
22

33
Under the hood Sharp manages a breadcrumb to keep track of stacked pages.
44

5-
## Display the breadcrumb
6-
7-
You can activate the breadcrumb display in sharp's configuration:
8-
9-
```php
10-
class SharpServiceProvider extends SharpAppServiceProvider
11-
{
12-
protected function configureSharp(SharpConfigBuilder $config): void
13-
{
14-
$config
15-
->displayBreadcrumb()
16-
// [...]
17-
}
18-
}
19-
```
20-
215
## Configure entity label
226

237
In Entity classes, you can define how an entity should be labeled in the breadcrumb with the `label` attribute:
@@ -74,6 +58,72 @@ class PostShow extends \Code16\Sharp\Show\SharpShow
7458
In the Form, the breadcrumb label is only used in one particular case: when coming from an embedded Entity List inside a Show Page. In this case, the Show Page and the Form entity are different, and the breadcrumb helps to keep track of the current edited entity.
7559
:::
7660

61+
## Configure custom labels cache
62+
63+
Breadcrumb labels are cached for 30 minutes to reduce DB queries between each navigation. If you don't want to cache them, which means all `SharpShow` in breadcrumb are loaded on every navigation, you can update the config in the SharpServiceProvider:
64+
65+
```php
66+
class SharpServiceProvider extends SharpAppServiceProvider
67+
{
68+
protected function configureSharp(SharpConfigBuilder $config): void
69+
{
70+
$config
71+
->configureBreadcrumbLabelsCache(false)
72+
// ...
73+
}
74+
}
75+
```
76+
77+
Alternatively, you can change the cache duration (default is 30 minutes):
78+
79+
```php
80+
class SharpServiceProvider extends SharpAppServiceProvider
81+
{
82+
protected function configureSharp(SharpConfigBuilder $config): void
83+
{
84+
$config
85+
->configureBreadcrumbLabelsCache(duration: 10)
86+
// ...
87+
}
88+
}
89+
```
90+
91+
### Lazy loading
92+
93+
In some cases, having the labels replaced by the default Entity label is acceptable and you want to have less DB queries, you can activate the lazy loading:
94+
95+
```php
96+
class SharpServiceProvider extends SharpAppServiceProvider
97+
{
98+
protected function configureSharp(SharpConfigBuilder $config): void
99+
{
100+
$config
101+
->enableBreadcrumbLabelsLazyLoading()
102+
}
103+
}
104+
```
105+
::: warning
106+
Be aware that the user may see the breadcrumb with default entity labels (e.g. "Posts > Post > Category > Edit") when :
107+
- a nested page is accessed directly (e.g. direct link)
108+
- cached labels are expired
109+
:::
110+
111+
## Hide the breadcrumb
112+
113+
If you don't want any breadcrumb, you can hide it in sharp's configuration:
114+
115+
```php
116+
class SharpServiceProvider extends SharpAppServiceProvider
117+
{
118+
protected function configureSharp(SharpConfigBuilder $config): void
119+
{
120+
$config
121+
->displayBreadcrumb(false)
122+
// [...]
123+
}
124+
}
125+
```
126+
77127
## Interact with Sharp's Breadcrumb
78128

79-
Refer to [the Context documentation](context.md) to find out how to interact with Sharp's breadcrumb.
129+
Refer to [the Context documentation](context.md) to find out how to interact with Sharp's breadcrumb.

resources/js/Pages/Dashboard/Dashboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<Title :entity-key="dashboardKey" />
5757

5858
<template #breadcrumb>
59-
<template v-if="config('sharp.display_breadcrumb')">
59+
<template v-if="config('sharp.breadcrumb.display')">
6060
<PageBreadcrumb :breadcrumb="breadcrumb" />
6161
</template>
6262
</template>

resources/js/Pages/Form/Form.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<Title :breadcrumb="breadcrumb" />
7878

7979
<template #breadcrumb>
80-
<template v-if="config('sharp.display_breadcrumb')">
80+
<template v-if="config('sharp.breadcrumb.display')">
8181
<PageBreadcrumb :breadcrumb="breadcrumb" />
8282
</template>
8383
</template>

resources/js/Pages/Show/Show.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
<Title :breadcrumb="breadcrumb" />
129129

130130
<template #breadcrumb>
131-
<template v-if="config('sharp.display_breadcrumb')">
131+
<template v-if="config('sharp.breadcrumb.display')">
132132
<PageBreadcrumb :breadcrumb="breadcrumb" />
133133
</template>
134134
</template>

src/Config/SharpConfigBuilder.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ class SharpConfigBuilder
3333
'name' => 'Sharp',
3434
'custom_url_segment' => 'sharp',
3535
'display_sharp_version_in_title' => true,
36-
'display_breadcrumb' => true,
36+
'breadcrumb' => [
37+
'display' => true,
38+
'labels' => [
39+
'lazy_loading' => false,
40+
'cache' => true,
41+
'cache_duration' => 30,
42+
],
43+
],
3744
'entities' => [],
3845
'entity_resolver' => null,
3946
'global_filters' => [],
@@ -132,7 +139,22 @@ public function displaySharpVersionInTitle(bool $displaySharpVersionInTitle = tr
132139

133140
public function displayBreadcrumb(bool $displayBreadcrumb = true): self
134141
{
135-
$this->config['display_breadcrumb'] = $displayBreadcrumb;
142+
$this->config['breadcrumb']['display'] = $displayBreadcrumb;
143+
144+
return $this;
145+
}
146+
147+
public function enableBreadcrumbLabelsLazyLoading(bool $lazyLoading = true): self
148+
{
149+
$this->config['breadcrumb']['labels']['lazy_loading'] = $lazyLoading;
150+
151+
return $this;
152+
}
153+
154+
public function configureBreadcrumbLabelsCache(bool $cache = true, int $duration = 30): self
155+
{
156+
$this->config['breadcrumb']['labels']['cache'] = $cache;
157+
$this->config['breadcrumb']['labels']['cache_duration'] = $duration;
136158

137159
return $this;
138160
}

src/Config/SharpLegacyConfigBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function get(string $key): mixed
4141
return null;
4242
}
4343

44+
if ($key == 'breadcrumb.display') {
45+
return config('sharp.display_breadcrumb', true);
46+
}
47+
4448
return config('sharp.'.$key);
4549
}
4650
}

src/Data/BreadcrumbItemData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
final class BreadcrumbItemData extends Data
99
{
1010
public function __construct(
11-
public string $type,
1211
public string $label,
1312
public ?string $documentTitleLabel,
1413
public string $entityKey,

0 commit comments

Comments
 (0)