From 140f85a655112b7f7ec5c74e8c53992e635566af Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Sat, 2 Nov 2024 14:48:00 +0200 Subject: [PATCH] Handle query scopes on custom query string --- src/Http/Livewire/Traits/HandleParams.php | 22 ++++++++++++++----- .../Middleware/HandleFiltersQueryString.php | 9 ++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Http/Livewire/Traits/HandleParams.php b/src/Http/Livewire/Traits/HandleParams.php index 7d9e592..b7bb71e 100644 --- a/src/Http/Livewire/Traits/HandleParams.php +++ b/src/Http/Livewire/Traits/HandleParams.php @@ -203,12 +203,7 @@ protected function updateCustomQueryStringUrl(): void return; } - $aliases = array_flip(array_merge( - [ - 'sort' => 'sort', - ], - config('statamic-livewire-filters.custom_query_string_aliases', []) - )); + $aliases = $this->getConfigAliases(); $prefix = config('statamic-livewire-filters.custom_query_string', 'filters'); @@ -239,6 +234,21 @@ protected function updateCustomQueryStringUrl(): void $this->dispatch('update-url', newUrl: url($fullPath)); } + protected function getConfigAliases(): array + { + return collect(config('statamic-livewire-filters.custom_query_string_aliases', []))->transform(function ($value, $key) { + if (str_contains($value, 'query_scope')) { + [$scopeString, $scopeKey] = explode(':', $value, 2); + + return $scopeKey; + } + + return $value; + })->merge(['sort' => 'sort']) + ->flip() + ->all(); + } + protected function dispatchParamsUpdated(): void { if (config('statamic-livewire-filters.enable_filter_values_count')) { diff --git a/src/Http/Middleware/HandleFiltersQueryString.php b/src/Http/Middleware/HandleFiltersQueryString.php index 088a397..5c62253 100644 --- a/src/Http/Middleware/HandleFiltersQueryString.php +++ b/src/Http/Middleware/HandleFiltersQueryString.php @@ -86,6 +86,15 @@ protected function parseFilterSegments(array $segments): array // Convert alias to actual filter key $actualKey = $aliases[$key]; + // Handle query_scopes + if (str_contains($actualKey, 'query_scope')) { + [$scopeString, $scopeClass, $scopeKey] = explode(':', $actualKey); + // Add the query scope param to the array + $params['query_scope'] = $scopeClass; + // Set the the actual parameter key + $actualKey = $scopeClass.':'.$scopeKey; + } + // Handle multiple values $value = str_contains($value, ',') ? implode('|', explode(',', $value))