Skip to content

Commit

Permalink
fix: /all not recognized as discussion route when a different route i…
Browse files Browse the repository at this point in the history
…s default
  • Loading branch information
imorland committed Feb 11, 2025
1 parent a769cee commit 0c5a388
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/Middleware/AddLanguageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,47 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
*/
protected function addQueryParams(ServerRequestInterface $request, array $params, string $language): ServerRequestInterface
{
// use recursive merge to preserve filters added by other extensions
$newParams = array_merge_recursive($params, ['filter' => ['language' => $language]]);
// Remove the top-level 'language' parameter so it isn’t passed along.
unset($params['language']);

// If a search is in progress, add the search gambit
if (Arr::get($params, 'q')) {
$newParams['q'] = Arr::get($params, 'q').' language:'.$language;
// Ensure the 'filter' key exists as an array.
if (!isset($params['filter']) || !is_array($params['filter'])) {
$params['filter'] = [];
}

return $request->withQueryParams($newParams);
// Set or overwrite the language filter.
$params['filter']['language'] = $language;

// If a search query is present, append the language filter to it.
if (isset($params['q']) && $params['q'] !== '') {
$params['q'] = $params['q'] . ' language:' . $language;
}

return $request->withQueryParams($params);
}

protected function isDiscussionListPath(ServerRequestInterface $request)
{
$path = $request->getAttribute('originalUri')->getPath();

// Check for the 'index' route (showing all discussions)
$defaultRoute = $this->settings->get('default_route');

if ($defaultRoute === '/all') {
if ($path === '/') {
// If the default route is '/all', treat both '/' and '/all' as the discussion list.
if ($path === '/' || $path === '/all') {
return true;
}
} else {
// Otherwise, if the default route is not '/all', then '/all' is the discussion list.
if ($path === '/all') {
return true;
}
} elseif ($path === '/all') {
return true;
}

// Check for the 'tag' route (tag page)
// Also apply for tag pages.
if (substr($path, 0, 2) === '/t') {
return true;
}

return false;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/integration/api/MiddlewareLanguageFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function setUp(): void
{
parent::setUp();

$this->setting('default_route', '/all');

$this->extension('flarum-tags');
$this->extension('fof-discussion-language');

Expand Down Expand Up @@ -95,6 +97,7 @@ public function routesProvider(): array
return [
[''],
['all'],
['t/tag-1'],

];
}
Expand Down

0 comments on commit 0c5a388

Please sign in to comment.