Skip to content

Commit

Permalink
feat: add option to keep filters from overview page in breadcrumb link
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Brinkmann committed Dec 10, 2024
1 parent ed9161d commit 4df73c9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
16 changes: 16 additions & 0 deletions config/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,20 @@
'thumbnail_presets' => [
// 'medium' => 800,
],

/*
|--------------------------------------------------------------------------
| Breadcrumbs
|--------------------------------------------------------------------------
|
| Here you can define if the breadcrumb on a collection entry or
| taxonomy term single view that points back to the list view should
| contain the filters that were used on that list before entering
| the single view.
|
*/

'breadcrumbs' => [
'add_filters_from_overview' => false
],
];
30 changes: 30 additions & 0 deletions src/CP/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Statamic\CP;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
use JsonSerializable;
use Statamic\Statamic;
use Statamic\Support\Str;

class Breadcrumbs implements Arrayable, JsonSerializable
{
Expand Down Expand Up @@ -48,4 +51,31 @@ public function title($title = null)

return $crumbs->reverse()->join($arrow);
}

public static function addFiltersFromReferer(string $validRefererRoute, string $handle)
{
if (! config('statamic.cp.breadcrumbs.add_filters_from_overview')) {
return false;
}

$referer = request()->headers->get('referer');
if (! $referer) {
return false;
}

$refererRequest = Request::create($referer);
if (Route::getRoutes()->match($refererRequest)?->uri() !== $validRefererRoute) {
return false;
}

if (! Str::contains($refererRequest->getPathInfo(), $handle)) {
return false;
}

if (! $refererRequest->hasAny(['sort', 'order', 'filters', 'search'])) {
return false;
}

return true;
}
}
24 changes: 24 additions & 0 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Request;
use InvalidArgumentException;
use Statamic\Contracts\Data\Augmentable as AugmentableContract;
use Statamic\Contracts\Entries\Collection as Contract;
use Statamic\CP\Breadcrumbs;
use Statamic\Data\ContainsCascadingData;
use Statamic\Data\ExistsAsFile;
use Statamic\Data\HasAugmentedData;
Expand Down Expand Up @@ -265,6 +267,28 @@ public function showUrl()
return cp_route('collections.show', $this->handle());
}

public function breadcrumbUrl()
{
$cpRoute = config('statamic.cp.route');
if (! Breadcrumbs::addFiltersFromReferer("{$cpRoute}/collections/{collection}", $this->handle())) {
return $this->showUrl();
}

$refererRequest = Request::create(request()->headers->get('referer'));

$search = $refererRequest->input('search');
$sort = $refererRequest->input('sort');
$order = $refererRequest->input('order');
$filters = $refererRequest->input('filters');

return cp_route('collections.show', array_merge((array) $this->handle(), [
'sort' => $sort,
'order' => $order,
'filters' => $filters,
'search' => $search,
]));
}

public function editUrl()
{
return cp_route('collections.edit', $this->handle());
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ protected function breadcrumbs($collection)
],
[
'text' => $collection->title(),
'url' => $collection->showUrl(),
'url' => $collection->breadcrumbUrl(),
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/CP/Taxonomies/TermsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function breadcrumbs($taxonomy)
],
[
'text' => $taxonomy->title(),
'url' => $taxonomy->showUrl(),
'url' => $taxonomy->breadcrumbUrl(),
],
]);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Taxonomies/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Support\Facades\Request;
use Statamic\Contracts\Data\Augmentable as AugmentableContract;
use Statamic\Contracts\Taxonomies\Taxonomy as Contract;
use Statamic\CP\Breadcrumbs;
use Statamic\Data\ContainsCascadingData;
use Statamic\Data\ContainsSupplementalData;
use Statamic\Data\ExistsAsFile;
Expand Down Expand Up @@ -81,6 +83,28 @@ public function showUrl()
return cp_route('taxonomies.show', $this->handle());
}

public function breadcrumbUrl()
{
$cpRoute = config('statamic.cp.route');
if (! Breadcrumbs::addFiltersFromReferer("{$cpRoute}/taxonomies/{taxonomy}", $this->handle())) {
return $this->showUrl();
}

$refererRequest = Request::create(request()->headers->get('referer'));

$search = $refererRequest->input('search');
$sort = $refererRequest->input('sort');
$order = $refererRequest->input('order');
$filters = $refererRequest->input('filters');

return cp_route('taxonomies.show', array_merge((array) $this->handle(), [
'sort' => $sort,
'order' => $order,
'filters' => $filters,
'search' => $search,
]));
}

public function editUrl()
{
return cp_route('taxonomies.edit', $this->handle());
Expand Down

0 comments on commit 4df73c9

Please sign in to comment.