From 4df73c9becb340d1bfe3947c4b3b5e77990ce131 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Tue, 10 Dec 2024 10:55:56 +0100 Subject: [PATCH 1/4] feat: add option to keep filters from overview page in breadcrumb link --- config/cp.php | 16 ++++++++++ src/CP/Breadcrumbs.php | 30 +++++++++++++++++++ src/Entries/Collection.php | 24 +++++++++++++++ .../CP/Collections/EntriesController.php | 2 +- .../CP/Taxonomies/TermsController.php | 2 +- src/Taxonomies/Taxonomy.php | 24 +++++++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/config/cp.php b/config/cp.php index 24f23edd95..ed48ae0815 100644 --- a/config/cp.php +++ b/config/cp.php @@ -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 + ], ]; diff --git a/src/CP/Breadcrumbs.php b/src/CP/Breadcrumbs.php index e27b81b184..98e4901a06 100644 --- a/src/CP/Breadcrumbs.php +++ b/src/CP/Breadcrumbs.php @@ -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 { @@ -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; + } } diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 8c89afe4be..d76583864b 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -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; @@ -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()); diff --git a/src/Http/Controllers/CP/Collections/EntriesController.php b/src/Http/Controllers/CP/Collections/EntriesController.php index 08921a71e1..5535e11a30 100644 --- a/src/Http/Controllers/CP/Collections/EntriesController.php +++ b/src/Http/Controllers/CP/Collections/EntriesController.php @@ -545,7 +545,7 @@ protected function breadcrumbs($collection) ], [ 'text' => $collection->title(), - 'url' => $collection->showUrl(), + 'url' => $collection->breadcrumbUrl(), ], ]); } diff --git a/src/Http/Controllers/CP/Taxonomies/TermsController.php b/src/Http/Controllers/CP/Taxonomies/TermsController.php index 9482380aae..d336504858 100644 --- a/src/Http/Controllers/CP/Taxonomies/TermsController.php +++ b/src/Http/Controllers/CP/Taxonomies/TermsController.php @@ -354,7 +354,7 @@ protected function breadcrumbs($taxonomy) ], [ 'text' => $taxonomy->title(), - 'url' => $taxonomy->showUrl(), + 'url' => $taxonomy->breadcrumbUrl(), ], ]); } diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index b2bfd4e76e..66ce7317e2 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -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; @@ -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()); From 4883b1143d613bf5347d0977e3c72fcff78eaecb Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Tue, 10 Dec 2024 15:08:06 +0100 Subject: [PATCH 2/4] fix: pint linting issue --- config/cp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/cp.php b/config/cp.php index ed48ae0815..cb3db4aef1 100644 --- a/config/cp.php +++ b/config/cp.php @@ -171,6 +171,6 @@ */ 'breadcrumbs' => [ - 'add_filters_from_overview' => false + 'add_filters_from_overview' => false, ], ]; From 9224a5f514df22188bb263d4beaeb180d0290ddb Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sat, 14 Dec 2024 19:11:30 +0100 Subject: [PATCH 3/4] chore: remove opt-in and add `page` param --- config/cp.php | 16 ---------------- src/CP/Breadcrumbs.php | 6 +----- src/Entries/Collection.php | 2 ++ src/Taxonomies/Taxonomy.php | 2 ++ 4 files changed, 5 insertions(+), 21 deletions(-) diff --git a/config/cp.php b/config/cp.php index cb3db4aef1..24f23edd95 100644 --- a/config/cp.php +++ b/config/cp.php @@ -157,20 +157,4 @@ '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, - ], ]; diff --git a/src/CP/Breadcrumbs.php b/src/CP/Breadcrumbs.php index 98e4901a06..df528ccfb9 100644 --- a/src/CP/Breadcrumbs.php +++ b/src/CP/Breadcrumbs.php @@ -54,10 +54,6 @@ public function title($title = null) 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; @@ -72,7 +68,7 @@ public static function addFiltersFromReferer(string $validRefererRoute, string $ return false; } - if (! $refererRequest->hasAny(['sort', 'order', 'filters', 'search'])) { + if (! $refererRequest->hasAny(['sort', 'order', 'filters', 'search', 'page'])) { return false; } diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index d76583864b..0cf71cd0f1 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -280,12 +280,14 @@ public function breadcrumbUrl() $sort = $refererRequest->input('sort'); $order = $refererRequest->input('order'); $filters = $refererRequest->input('filters'); + $page = $refererRequest->input('page'); return cp_route('collections.show', array_merge((array) $this->handle(), [ 'sort' => $sort, 'order' => $order, 'filters' => $filters, 'search' => $search, + 'page' => $page, ])); } diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index 66ce7317e2..f711d2e171 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -96,12 +96,14 @@ public function breadcrumbUrl() $sort = $refererRequest->input('sort'); $order = $refererRequest->input('order'); $filters = $refererRequest->input('filters'); + $page = $refererRequest->input('page'); return cp_route('taxonomies.show', array_merge((array) $this->handle(), [ 'sort' => $sort, 'order' => $order, 'filters' => $filters, 'search' => $search, + 'page' => $page, ])); } From a39f63fba3d8e16872e100deda8b54ee977de474 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Tue, 17 Dec 2024 09:23:00 +0100 Subject: [PATCH 4/4] chore: simplify solution based on review --- src/CP/Breadcrumbs.php | 30 ------------------------------ src/Entries/Collection.php | 22 +++------------------- src/Taxonomies/Taxonomy.php | 22 +++------------------- 3 files changed, 6 insertions(+), 68 deletions(-) diff --git a/src/CP/Breadcrumbs.php b/src/CP/Breadcrumbs.php index 98e4901a06..e27b81b184 100644 --- a/src/CP/Breadcrumbs.php +++ b/src/CP/Breadcrumbs.php @@ -3,11 +3,8 @@ 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 { @@ -51,31 +48,4 @@ 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; - } } diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index d76583864b..70e5c562d4 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -4,11 +4,9 @@ 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; @@ -269,24 +267,10 @@ public function showUrl() 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')); + $referer = request()->header('referer'); + $showUrl = $this->showUrl(); - $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, - ])); + return $referer && Str::before($referer, '?') === $showUrl ? $referer : $showUrl; } public function editUrl() diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index 66ce7317e2..da83cced27 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -5,10 +5,8 @@ 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; @@ -85,24 +83,10 @@ public function showUrl() 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'); + $referer = request()->header('referer'); + $showUrl = $this->showUrl(); - return cp_route('taxonomies.show', array_merge((array) $this->handle(), [ - 'sort' => $sort, - 'order' => $order, - 'filters' => $filters, - 'search' => $search, - ])); + return $referer && Str::before($referer, '?') === $showUrl ? $referer : $showUrl; } public function editUrl()