Skip to content

Commit e049b75

Browse files
Add documentation
1 parent 2e698cf commit e049b75

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

docs/.vitepress/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function sidebar(): DefaultTheme.SidebarItem[] {
5858
{ text: 'List', link: '/guide/show-fields/list.md' },
5959
{ text: 'File', link: '/guide/show-fields/file.md' },
6060
{ text: 'Entity List', link: '/guide/show-fields/entity-list.md' },
61+
{ text: 'Dashboard', link: '/guide/show-fields/dashboard.md' },
6162
// { text: 'Custom show field', link: '/guide/custom-show-fields.md' }
6263
]
6364
},
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Dashboard
2+
3+
Class: `Code16\Sharp\Show\Fields\SharpShowDashboardField`
4+
5+
## Constructor
6+
7+
This field needs, as first parameter, either the entity key or the `SharpDashboardEntity` class that declares the dashboard which will be included in the Show Page.
8+
9+
For instance:
10+
11+
```php
12+
SharpShowDashboardField::make('posts_dashboard')
13+
```
14+
15+
or
16+
17+
```php
18+
SharpShowDashboardField::make(PostDashboardEntity::class)
19+
```
20+
::: warning
21+
This last syntax is better in terms of DX (since it allows using the IDE to navigate to the Entity List implementation), but it won’t work in two specific cases: if you use a custom `SharpEntityResolver` or if you your Entity is declared with multiple keys.
22+
:::
23+
24+
## Configuration
25+
26+
### `hideFilterWithValue(string $filterName, $value)`
27+
This is the most important method of the field, since it will not only hide a filter but also set its value. The purpose is to allow to **scope the data to the instance** of the Show Page. For example, let’s say we display a Post and that we want to embed a dashboard with the post's statistics:
28+
29+
30+
```php
31+
class PostShow extends SharpShow
32+
{
33+
// ...
34+
35+
public function buildShowFields(FieldsContainer $showFields): void
36+
{
37+
$showFields->addField(
38+
SharpShowDashboardField::make(PostDashboardEntity::class)
39+
->hideFilterWithValue(PostFilter::class, 64)
40+
);
41+
}
42+
}
43+
```
44+
45+
Here we're scoping the `PostDashboard` declared in the `PostDashboardEntity` to the instance of the `Post` with id 64.
46+
47+
48+
You can pass a closure as the value, and it will contain the current Show instance id. In most cases, you'll have to write this:
49+
50+
```php
51+
SharpShowDashboardField::make(PostDashboardEntity::class)
52+
->hideFilterWithValue(PostFilter::class, fn ($instanceId) => $instanceId);
53+
```
54+
55+
**One final note**: sometimes the linked filter is really just a scope, never displayed to the user. In this case, it can be tedious to write a full implementation in the Dashboard. In this situation, you can use the `HiddenFiler` class for the filter, passing a key:
56+
57+
```php
58+
class PostShow extends SharpShow
59+
{
60+
// ...
61+
62+
public function buildShowFields(FieldsContainer $showFields): void
63+
{
64+
$showFields->addField(
65+
SharpShowDashboardField::make(PostDashboardEntity::class)
66+
->hideFilterWithValue('post', fn ($instanceId) => $instanceId);
67+
);
68+
}
69+
}
70+
```
71+
72+
```php
73+
use \Code16\Sharp\EntityList\Filters\HiddenFilter;
74+
75+
class PostDashboard extends SharpDashboard
76+
{
77+
// ...
78+
79+
protected function getFilters(): ?array
80+
{
81+
return [
82+
HiddenFilter::make('post')
83+
];
84+
}
85+
86+
protected function buildWidgetsData(): void
87+
{
88+
return $this->setFigureData('visit_count',
89+
figure: Post::query()
90+
->findOrFail($this->queryParams->filterFor('post'))
91+
->get()?->visit_count
92+
);
93+
}
94+
}
95+
```
96+
97+
### `hideDashboardCommand(array|string $commands): self`
98+
99+
Use it to hide any dashboard command in this particular Dashboard (useful when reusing a Dashboard). This will apply before looking at authorizations.
100+
101+
## Display in layout
102+
103+
To display your dashboard in your show page's layout, you have to use the `addDashboardSection()` method in your Show Page's `buildShowLayout()` method.
104+
105+
```php
106+
protected function buildShowLayout(ShowLayout $showLayout): void
107+
{
108+
$showLayout
109+
->addSection(function (ShowLayoutSection $section) {
110+
$section
111+
->addColumn(7, function (ShowLayoutColumn $column) {
112+
$column
113+
->withFields(categories: 5, author: 7)
114+
// ...
115+
})
116+
->addColumn(5, function (ShowLayoutColumn $column) {
117+
// ...
118+
});
119+
})
120+
->addDashboardSection(PostDashboardEntity::class);
121+
}
122+
```

docs/guide/show-fields/entity-list.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ Use it to show or hide the search field in this particular Entity List (useful w
138138

139139
Use it to show or hide the count of items in the Entity List.
140140

141+
## Display in layout
142+
143+
To display your entity list in your show page's layout, you have to use the `addEntityListSection()` method in your Show Page's `buildShowLayout()` method.
144+
145+
```php
146+
protected function buildShowLayout(ShowLayout $showLayout): void
147+
{
148+
$showLayout
149+
->addSection(function (ShowLayoutSection $section) {
150+
$section
151+
->addColumn(7, function (ShowLayoutColumn $column) {
152+
$column
153+
->withFields(categories: 5, author: 7)
154+
// ...
155+
})
156+
->addColumn(5, function (ShowLayoutColumn $column) {
157+
// ...
158+
});
159+
})
160+
->addEntityListSection(ProductEntity::class);
161+
}
162+
```
163+
141164
## Transformer
142165

143166
There is no transformer, since Sharp will NOT look for an attribute in the instance sent. The data of the Entity List is brought by a distinct XHR call, the same Sharp will use for any Entity List.

0 commit comments

Comments
 (0)