From b5cff42542f8740d9046668ae2665b05bf432d34 Mon Sep 17 00:00:00 2001 From: philippe Date: Wed, 17 Dec 2025 09:33:18 +0100 Subject: [PATCH] Fix global filter handing in API routes --- src/Http/Middleware/HandleGlobalFilters.php | 23 ++++++++--- tests/Http/GlobalFilterRoutesTest.php | 43 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/Http/Middleware/HandleGlobalFilters.php b/src/Http/Middleware/HandleGlobalFilters.php index 450006d38..e5dca0df0 100644 --- a/src/Http/Middleware/HandleGlobalFilters.php +++ b/src/Http/Middleware/HandleGlobalFilters.php @@ -5,6 +5,7 @@ use Closure; use Code16\Sharp\Filters\GlobalFilters\GlobalFilters; use Code16\Sharp\Filters\GlobalRequiredFilter; +use Code16\Sharp\Http\Context\SharpBreadcrumb; use Illuminate\Http\Request; use Illuminate\Support\Facades\URL; @@ -14,12 +15,21 @@ public function __construct(private GlobalFilters $globalFiltersHandler) {} public function handle(Request $request, Closure $next) { - if ($filterKey = $request->route('filterKey')) { - $filterKeys = explode(GlobalFilters::$valuesUrlSeparator, $filterKey); + $globalFilterValue = $request->route('filterKey'); + if (! $globalFilterValue && $request->wantsJson()) { + if ($urlToParse = $request->header(SharpBreadcrumb::CURRENT_PAGE_URL_HEADER) ?: request()->query('current_page_url')) { + $globalFilterValue = str($urlToParse) + ->after($request->host().'/'.sharp()->config()->get('custom_url_segment').'/') + ->before('/'); + } + } + + if ($globalFilterValue) { + $globalFilterValues = explode(GlobalFilters::$valuesUrlSeparator, $globalFilterValue); if ($this->globalFiltersHandler->isEnabled()) { $globalFilters = $this->globalFiltersHandler->getFilters(); - if (count($filterKeys) !== count($globalFilters)) { + if (count($globalFilterValues) !== count($globalFilters)) { return redirect()->route('code16.sharp.home', [ 'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(), ]); @@ -27,13 +37,14 @@ public function handle(Request $request, Closure $next) collect($globalFilters) ->each(fn (GlobalRequiredFilter $globalFilter, int $index) => $globalFilter - ->setCurrentValue($filterKeys[$index]) + ->setCurrentValue($globalFilterValues[$index]) ); - if (! $request->wantsJson() + if (sharp()->context()->globalFilterUrlSegmentValue() !== $globalFilterValue + && ! $request->wantsJson() && $request->isMethod('GET') - && sharp()->context()->globalFilterUrlSegmentValue() !== $filterKey ) { + // Filter value is invalid, redirect to homepage return redirect()->route('code16.sharp.home', [ 'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(), ]); diff --git a/tests/Http/GlobalFilterRoutesTest.php b/tests/Http/GlobalFilterRoutesTest.php index 3de5529db..653501d86 100644 --- a/tests/Http/GlobalFilterRoutesTest.php +++ b/tests/Http/GlobalFilterRoutesTest.php @@ -1,8 +1,13 @@ config()->declareEntity(PersonEntity::class); @@ -76,3 +81,41 @@ expect(sharp()->context()->globalFilterValue('test1'))->toEqual('one'); expect(sharp()->context()->globalFilterValue('test2'))->toEqual('two'); }); + +it('sets the current filterKey according to the URL in an API case', function () { + fakeGlobalFilter(); + + fakeListFor('person', new class() extends PersonList + { + protected function getEntityCommands(): ?array + { + return [ + 'cmd' => new class() extends EntityCommand + { + public function label(): ?string + { + return 'entity'; + } + + public function buildFormFields(FieldsContainer $formFields): void + { + $formFields->addField(SharpFormTextField::make('name')); + } + + public function execute(array $data = []): array {} + }, + ]; + } + }); + + $this + ->getJson( + route('code16.sharp.api.list.command.entity.form', ['person', 'cmd']), + headers: [ + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => url('/sharp/one/s-list/person'), + ] + ) + ->assertOk(); + + expect(sharp()->context()->globalFilterValue('test'))->toEqual('one'); +});