Make it easier to define global forum filters

mattrogowski

Well-known member
In XF\Pub\Controller\Forum::getForumFilterInput it calls $filters = $forum->TypeHandler->getForumFilterInput($forum, $this->request, $filters); before returning the filters.

This is fine if you have forum type-specific filters, but it makes it difficult to create new filters for all forum types that are also available to the forum type handlers, if for any reason you want to check if a filter has been set. It's obviously not possible to extend the abstract forum type handler to apply a filter to all forum types, so the only way to do it is to extend getForumFilterInput(), but as I'd have to call the parent function first, $forum->TypeHandler->getForumFilterInput() has already run, but it won't know anything about my custom filters as I haven't defined them yet.

What I am currently having to do is add a new filter by extending the controller method, but then also check for the same filters again in the forum type handlers, because I need to check if it's been set but to do that I need to re-filter the input instead of just checking if it is set in $filters. This shouldn't be necessary.

All that needs to be done is before this:

PHP:
$filters = $forum->TypeHandler->getForumFilterInput($forum, $this->request, $filters);

add:

PHP:
$filters = $this->getAdditionalFilterInput($forum, $this->request, $filters);

and then have a function that does the same as XF\ForumType\AbstractHandler::getForumFilterInput does - just returns the filters unless it's extended:

PHP:
public function getAdditionalFilterInput(Forum $forum, Request $request, array $filters): array
{
    return $filters;
}

Then I can define my new global filter here, and it will be available in the forum type filter function so I can check if it has been set.

Hopefully this can be slipped in to 2.3 because the above is all that should need to be added.
 
Upvote 5
Just to expand on use case, I am trying to:
  • Add a global date filter
  • Apply the same date filter in specific forum types under certain conditions if one is not passed through in the URL
Currently in the controller I am setting the filter so it works if passed through in the URL, but in the forum type handler it isn't in $filters yet as the forum type handler filter function runs before my extended controller function, so I don't know if it's in the request URL. This means I have to filter the request input again in the forum type handler even though I already do this elsewhere, to determine if I want to set the filter manually for this forum type.

With the addition of this new function, the filter would already be set by the time it gets to the forum type handler, and I wouldn't need to check if it's been passed through in the request again, I'd just check if it was in $filters, which would avoid code duplication.
 
Honestly the entire filters display is also a janky mess as lots of template modifications to inject template fragments. This is one system which was begging for a handler class setup which is iteratively called to render requested filters being applied and likely for the filter options in the filter menu themselves.
 
Honestly the entire filters display is also a janky mess as lots of template modifications to inject template fragments. This is one system which was begging for a handler class setup which is iteratively called to render requested filters being applied and likely for the filter options in the filter menu themselves.
That’s a good idea actually, thinking about it is has always felt kinda messy adding those changes with template modifications.
 
Back
Top Bottom