Skip to content

Commit

Permalink
Handle query scopes on custom query string
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 2, 2024
1 parent 829fdff commit 140f85a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Http/Livewire/Traits/HandleParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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')) {
Expand Down
9 changes: 9 additions & 0 deletions src/Http/Middleware/HandleFiltersQueryString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 140f85a

Please sign in to comment.