While building a plugin with custom topic list filters (e.g., by price, location) using URL query parameters. The URL updates correctly (e.g., ...?market_item_statuses=Available
), but the parameters aren’t appearing in topic_query.options
on the server.
Setup:
-
Client-Side Param Registration (
tecenc-discovery-params.js
):// tecenc-discovery-params.js import { apiInitializer } from "discourse/lib/api"; export default apiInitializer("1.37.3", (api) => { const MARKET_PARAMS_KEYS = [ "market_price_min", "market_price_max", "market_location", "market_conditions", "market_warranty", "market_item_statuses" ]; MARKET_PARAMS_KEYS.forEach(paramKey => { api.addDiscoveryQueryParam(paramKey, { replace: true, refreshModel: true }); }); });
-
Server-Side Whitelisting Attempt (
plugin.rb
):# plugin.rb module ::Tecenc MARKET_PARAMS_KEYS = [ :market_price_min, :market_price_max, :market_location, :market_conditions, :market_warranty, :market_item_statuses ].freeze end # after_initialize do if SiteSetting.tecenc_enabled? if defined?(::TopicQuery.add_custom_param_handler) ::Tecenc::MARKET_PARAMS_KEYS.each do |param_key| ::TopicQuery.add_custom_param_handler(param_key) { |value| value } # Simplified for brevity end Rails.logger.info "[Tecenc] Registered with add_custom_param_handler." elsif defined?(::TopicQuery) && ::TopicQuery.methods.include?(:extra_options_whitelist) current_whitelist = ::TopicQuery.extra_options_whitelist || [] new_whitelist = (current_whitelist + ::Tecenc::MARKET_PARAMS_KEYS).uniq ::TopicQuery.extra_options_whitelist(*new_whitelist) Rails.logger.info "[Tecenc] Extended extra_options_whitelist." else Rails.logger.warn "[Tecenc] PLUGIN_WARN: Could not find method to whitelist params for TopicQuery." end
-
Server-Side Filtering Logic (
plugin.rb
):# plugin.rb (inside after_initialize / if enabled) ::TopicQuery.add_custom_filter(:"tecenc_filters") do |topics, topic_query| opts = topic_query.options Rails.logger.info "[Tecenc_TopicQuery] Opts: #{opts.inspect}" # CRITICAL LOG # market_params_present = ::Tecenc::MARKET_PARAMS_KEYS.any? { |p| opts[p].present? } # if market_params_present # # ... filtering logic using opts[key] ... # end topics # or modified_topics end
The Problem (Logs):
-
Parameter whitelisting fails:
[Tecenc] PLUGIN_WARN: Could not find a suitable method (add_custom_param_handler or extra_options_whitelist) to whitelist custom params for TopicQuery.
-
opts
inTopicQuery.add_custom_filter
is missing our custom params:
When URL is...?market_item_statuses=Available
, log shows:[Tecenc_TopicQuery] Opts: {:category=>5, :filter=>"default", :topic_ids=>nil, :category_id=>5}
Our
market_item_statuses
(and other custom params) are not present.
Our Environment:
Questions:
- What’s the current best practice for ensuring custom URL query params reach
topic_query.options
in recent Discourse versions? - Why might our attempts to use
add_custom_param_handler
orextra_options_whitelist
be failing with the “Could not find a suitable method” warning? - Is there an alternative approach for parameter registration with
TopicQuery
we should use?
Any help would be much appreciated!