|
| 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 | +``` |
0 commit comments