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());