Skip to content

Commit 914392d

Browse files
authored
Merge pull request #24 from Formfeed-UK/main
main -> dev
2 parents 136358b + b216131 commit 914392d

21 files changed

+717
-7296
lines changed

README.md

+99-61
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
# Nova 4 Breadcrumbs
22

3-
This [Laravel Nova](https://nova.laravel.com/) package adds automated breadcrumbs to the top of Nova 4 resources.
3+
This [Laravel Nova](https://nova.laravel.com/) package extends the breadcrumbs functionality of the First Party Nova breadcrumbs.
44

5-
Version 1.x has breaking changes from 0.x for parent resource discovery. See the [issues section](#100-breaking-changes) for details.
5+
Tests repo can be found here: https://github.com/Formfeed-UK/nova-breadcrumbs-tests
66

7-
## Requirements
7+
## Version 2.x Changes
8+
9+
Version 2.x is a significant change from previous versions, this package now augments the existing nova breadcrumbs to offer:
10+
11+
- Static methods on the breadcrumbs class allowing control of breadcrumb generation globally
12+
- Methods on Resources allowing control of breadcrumb generation per resource (per-resource methods override static callbacks)
13+
- Support for resource groups
14+
- Nested resource breadcrumbs
15+
16+
#### Breaking changes from 1.x
17+
- Will use the Nova 4.19+ Breadcrumbs Vue components
18+
- No Longer uses resource cards (This gives better UX as the breadcrumbs will be sent via the page props as per the built in ones and drops a request)
19+
- Will intercept the Nova Breadcrumbs via middleware
20+
- Can no longer have custom CSS (due to using the Nova components)
21+
- Can no longer use the onlyOn{view}, exceptOn{view} etc permissions methods. Breadcrumb visibility can now be controlled via the callbacks/class methods
22+
- Each breadcrumb will extend the Nova Breadcrumb class, and the array of Breadcrumbs will extend the Nova Breadcrumbs class.
23+
24+
## Requirements >= v2.x
25+
26+
- `php: >=8.0`
27+
- `laravel/nova: ^4.19`
28+
29+
## Requirements <= v1.x
830

931
- `php: >=8.0`
1032
- `laravel/nova: ^4.0`
@@ -21,10 +43,8 @@ It supports:
2143
- Linking directly to Resource Tabs in the [eminiarts/nova-tabs](https://github.com/eminiarts/nova-tabs) package (Tab slugs are recommended)
2244
- Linking to either the resource's index or its parent (for relationships included as fields)
2345
- Customising the title and label functions for resources
24-
- Specifying custom css classes
2546
- Use on Dashboards (only to the extent that the Breadcrumbs show as `Home -> {Current Dashboard}`). Mainly for UI consistency.
26-
27-
This package relies on [formfeed-uk/nova-resource-cards](https://github.com/Formfeed-UK/nova-resource-cards) which wrap a number of nova pages. If you override these pages yourself ensure that nova-resource-cards is loaded after the packages which do so.
47+
- Methods/Callbacks to control the breadcrumbs generation global or per resource
2848

2949
## Installation
3050

@@ -43,25 +63,17 @@ php artisan vendor:publish --tag=breadcrumbs-config
4363

4464
### General
4565

46-
1) Include the breadcrumbs card at the beginning of the cards array in any resources that you wish to use breadcrumbs (be sure to include `$this` as the second parameter):
66+
1) Enable Nova Breadcrumbs in the same way as the first party Nova Breadcrumbs in your `NovaServiceProvider` `boot` method:
4767

4868
```php
49-
...
5069

51-
use Formfeed\Breadcrumbs\Breadcrumbs;
70+
public function boot() {
71+
parent::boot();
5272

53-
class MyResource extends Resource {
54-
...
55-
public function cards(NovaRequest $request) {
56-
return [
57-
Breadcrumbs::make($request, $this),
58-
...
59-
];
73+
Nova::withBreadcrumbs(true);
6074
}
61-
...
62-
}
75+
6376
```
64-
This card extends `ResourceCard` from that package and as such has the visbility and authorisation methods available.
6577

6678
2) Optionally configure a `parent` method on your Model to explicitly define the relationship the package should query. The name of this function can be changed in the configuration file.
6779

@@ -84,35 +96,85 @@ class MyModel extends Model {
8496
}
8597
```
8698

87-
### Include in all Resources
99+
### Resource Methods
100+
101+
You can optionally override the default behaviour of the breadcrumbs package on a per resource basis by adding methods to your Nova Resource. These methods should all return an instance of Breadcrumb or an array of Breadcrumb instances.
102+
103+
- `groupBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $groupBreadcrumb)` - Override the group breadcrumb for this resource
104+
- `indexBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $indexBreadcrumb)` - Override the index breadcrumb for this resource
105+
- `detailBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $detailBreadcrumb)` - Override the detail breadcrumb for this resource
106+
- `formBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $formBreadcrumb, $type)` - Override the form breadcrumb for this resource, $type is a string referring to the current form type (create, update, attach, replicate etc)
107+
- `resourceBreadcrumbs(NovaRequest $request, Breadcrumbs $breadcrumbs, array $breadcrumbArray)` - Override the entire set of breadcrumbs for this resource
88108

89-
If you would like to include the Breadcrumbs on all resources in one place, the best way to do this is to override the `resolveCards` method on your `App/Nova/Resource.php` file, as by default this file is extended by all of the Nova resources in your Application.
109+
For Dashboards, you can use the following method:
90110

91-
A very basic example could look like this:
111+
- `dashboardBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $dashboardBreadcrumb)` - Override the dashboard breadcrumb for this resource
112+
113+
#### Example
92114

93115
```php
94-
public function resolveCards(NovaRequest $request)
95-
{
96-
$cards = $this->cards($request);
97-
array_unshift($cards, Breadcrumbs::make($request, $this));
98-
return collect(array_values($this->filter($cards)));
116+
117+
class MyResource extends Resource {
118+
119+
// Change the name of the breadcrumb
120+
public function detailBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $detailBreadcrumb) {
121+
return $detailBreadcrumb->name = _('My Custom Name');
122+
}
123+
124+
// Remove all previous breadcrumbs and add a new root
125+
public function resourceBreadcrumbs(NovaRequest $request, Breadcrumbs $breadcrumbs, array $breadcrumbArray) {
126+
$breadcrumbs->items = [Breadcrumb::make('Home', '/')];
127+
return $breadcrumbArray;
128+
}
129+
130+
// Prevent the group breadcrumb for this resource
131+
public function groupBreadcrumb(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $groupBreadcrumb) {
132+
return null;
99133
}
134+
}
135+
100136
```
101137

102-
### Usage on Dashboards
138+
### Static Callbacks
139+
140+
You can override the default behaviour of the breadcrumbs globally by using the following static methods on the Breadcrumbs class. They should be provided within a boot method on a service provider.
103141

104-
Simply include the Breadcrumbs at the start of the Cards array for your dashboard. You'll need to include the request manually, as its not available on the Dashboard cards method.
142+
These methods will be overriden by any per resource methods.
105143

106-
For example for the default Dashboard:
144+
The closure provided should return either an instance of Breadcrumb or an array of Breadcrumb instances.
145+
146+
- detailCallback(callable $callback)
147+
- indexCallback(callable $callback)
148+
- formCallback(callable $callback)
149+
- resourceCallback(callable $callback)
150+
- dashboardCallback(callable $callback)
151+
- rootCallback(callable $callback)
152+
- groupCallback(callable $callback)
153+
154+
#### Example
107155

108156
```php
109-
public function cards()
110-
{
111-
return [
112-
Breadcrumbs::make(app(NovaRequest::class), $this),
113-
new Help,
114-
];
157+
158+
use FormFeed\Breadcrumbs\Breadcrumbs;
159+
use FormFeed\Breadcrumbs\Breadcrumb;
160+
161+
class NovaServiceProvider extends ServiceProvider {
162+
163+
public function boot() {
164+
parent::boot();
165+
166+
Nova::withBreadcrumbs(true);
167+
168+
Breadcrumbs::detailCallback(function(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $detailBreadcrumb) {
169+
return $detailBreadcrumb->name = _('My Custom Name');
170+
});
171+
172+
Breadcrumbs::rootCallback(function(NovaRequest $request, Breadcrumbs $breadcrumbs, Breadcrumb $rootBreadcrumb) {
173+
return Breadcrumb::make(_('My Custom Root Breadcrumb'), "/my-root");
174+
});
115175
}
176+
}
177+
116178
```
117179

118180
### Configuration Options
@@ -121,7 +183,7 @@ Please see the included config file for a full list of configuration options (it
121183

122184
In addition to these options you can also specify the following options in the resource itself:
123185

124-
#### Link To parent
186+
#### Link To parent
125187
This determines if the breadcrumb should link to the parent resource regardless of if the current resource's index is navigable from the main menu:
126188

127189
`public static $linkToParent = true|false;`
@@ -131,14 +193,6 @@ Resolving Parent breadcrumbs can be disabled by adding the following static vari
131193

132194
`public static $resolveParentBreadcrumbs = false;`
133195

134-
#### Extra CSS Classes
135-
These can be configured globally or chained to the Breadcrumbs class:
136-
137-
`Breadcrumbs::make($request, $this)->withClasses(["my-extra", "classes"])`
138-
139-
#### Visibility and Authorisations
140-
Please see the [formfeed-uk/nova-resource-cards](https://github.com/Formfeed-UK/nova-resource-cards) package for information on the visbility and authorisation methods. They broadly follow the same pattern as fields.
141-
142196
#### Invoking Reflection
143197
Determining the parent via invoking reflected blank model methods and checking the returned type is now disabled by default.
144198

@@ -159,22 +213,6 @@ You can also set this on a per-resource basis with the following static:
159213

160214
If you have any requests for functionality or find any bugs please open an issue or submit a Pull Request. Pull requests will be actioned faster than Issues.
161215

162-
### 1.0.0 Breaking Changes
163-
164-
1.0.0 introduces a breaking change in how the parent model is determined. The following is now applied in order:
165-
166-
1) Attempt to get parent from the model method with name retrieved from `parentMethod` config option
167-
2) Attempt to get parent from the first `Laravel\Nova\Fields\BelongsTo` field in your resource
168-
3) Attempt to get parent from the first method on your model that has a defined return type of `Illuminate\Database\Eloquent\Relations\BelongsTo`
169-
170-
If none of these are fulfilled the package now optionally (defaulting to false):
171-
172-
4) Attempt to find a `Laravel\Nova\Fields\BelongsTo` relationship via creating a blank model, invoking it's methods, and checking the type of the return.
173-
174-
This was previously turned on by default if the `parentMethod` was not found, and had the potential to be destructive if there were methods on the model which were destructive across all records, such as `Model::truncate()`
175-
176-
Please see the [**Configuration Options**](#invoking-reflection) section for more details on how to enable this functionality.
177-
178216
## License
179217

180218
Nova Breadcrumbs is open-sourced software licensed under the [MIT license](LICENSE.md).

composer.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
{
22
"name": "formfeed-uk/nova-breadcrumbs",
3-
"description": "A Laravel Nova package to show breadcrumbs on Nova Resources",
3+
"description": "A Laravel Nova package to extend the capabilities of the first party Nova Breadcrumbs",
44
"homepage": "https://github.com/formfeed-uk/nova-breadcrumbs",
55
"license": "MIT",
66
"keywords": [
77
"laravel",
88
"nova",
9-
"cards",
10-
"resource-cards",
119
"breadcrumbs"
1210
],
1311
"require": {
1412
"php": "^7.4|^8",
15-
"laravel/nova": "^4.0",
16-
"formfeed-uk/nova-resource-cards": "^1.1.0"
13+
"laravel/nova": "^4.19"
1714
},
1815
"autoload": {
1916
"psr-4": {
@@ -23,7 +20,7 @@
2320
"extra": {
2421
"laravel": {
2522
"providers": [
26-
"Formfeed\\Breadcrumbs\\CardServiceProvider"
23+
"Formfeed\\Breadcrumbs\\BreadcrumbServiceProvider"
2724
]
2825
}
2926
},

config/breadcrumbs.php

+29-15
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
| the index view for that resource
1313
|
1414
| Default Value: false
15-
| Default Behaviour:
15+
| Default Behaviour:
1616
| Where a resource is in the main navigation, links to that resource will
1717
| go to its index view.
18-
|
18+
|
1919
| Can be overridden on a per resource basis with:
2020
| public static $linkToParent = true|false;
2121
|
@@ -27,7 +27,7 @@
2727
| Model Parent method
2828
|--------------------------------------------------------------------------
2929
|
30-
| The name of the optional method on the resources model that defines the
30+
| The name of the optional method on the resources model that defines the
3131
| parent model
3232
|
3333
| Default Value: parent
@@ -40,8 +40,8 @@
4040
| Use Invoking Reflection
4141
|--------------------------------------------------------------------------
4242
|
43-
| If the package can't get the parent relationship from either a specified
44-
| "parent" method, a Nova/Fields/BelongsTo field, or a method with BelongsTo
43+
| If the package can't get the parent relationship from either a specified
44+
| "parent" method, a Nova/Fields/BelongsTo field, or a method with BelongsTo
4545
| return type on the underlying model, it will attempt to find the parent
4646
| via creating a new blank model, invoking the methods, and reading the
4747
| response types.
@@ -70,7 +70,7 @@
7070
| The static property on a nova resource that stores the "title" column to
7171
| use as the label for resource record crumbs.
7272
|
73-
| Default: "title"
73+
| Default: "title"
7474
|
7575
*/
7676
"titleProperty" => "title",
@@ -81,27 +81,41 @@
8181
|--------------------------------------------------------------------------
8282
|
8383
| The resource static method to use to determine the label and
84-
| singular label for the resource name
84+
| singular label for the resource name
8585
|
86-
| Default:
86+
| Default:
8787
| labelFunction: "label"
88-
| singularLabelFunction: "singularLabel"
88+
| singularLabelFunction: "singularLabel"
8989
|
9090
*/
9191
"labelFunction" => "label",
9292
"singularLabelFunction" => "singularLabel",
9393

9494
/*
9595
|--------------------------------------------------------------------------
96-
| Global CSS Classes
96+
| Include Resource Group in Breadcrumbs Array
97+
|--------------------------------------------------------------------------
98+
|
99+
| Determines whether to include the group as defined by the group static
100+
| function on the array (where present)
101+
|
102+
| Default: false
103+
|
104+
*/
105+
"includeGroup" => false,
106+
107+
/*
108+
|--------------------------------------------------------------------------
109+
| Ignore default "Other" group
97110
|--------------------------------------------------------------------------
98111
|
99-
| Set global additional css classes for all breadcrumbs cards.
112+
| Nova automatically assigns a group of "Other", ignore this group in
113+
| breadcrumbs generation
100114
|
101-
| Can be added to on a per-resource basis using withClasses()
115+
| Default: false
102116
|
103117
*/
104-
"cssClasses" => [],
118+
"includeOtherGroup" => false,
119+
105120

106-
107-
];
121+
];

dist/css/card.css

-1
This file was deleted.

0 commit comments

Comments
 (0)