How to access Category custom fields in Plugins

Prefacing, this is for a plugin that needs to know Category custom_fields for creating a button in the before-create-topic plugin-outlet. I also need to use this in the after-topic-footer-main-buttons connector.

I’m likely missing something incredibly simple here…

I can use the following in the initializer to get the category class… sortof

const controller = api.container.lookup("controller:navigation/category");

When I print that out, I can see various properties. In the devconsole, I can see category: (...). When I click (..) I can see custom_fields and the field I’m looking for. How can I get to this from plugin API??

I’ve tried various forms of “getting” the property, but all return undefined.

You might back up and say what you’re trying to do. Mostly, the data you need is already available. If not, you might use Ajax to pull it in, in a theme, or add it to the serializer, in a plugin.

I’m trying to get a custom field from my category. Sorry that wasn’t clear before. I’ve updated the OP with more information.

1 Like

That’s more information than I understood before… . . Something like this in plugin.rb:

  add_to_serializer(:category, :your_plugin) do

I think what you want to do, though, is look for some other plugins that are likely to add stuff to the serializer. I think that Topic Ratings Plugin or Topic List Previews should be good ones to look at, and hopefully it’ll be “obvious” (but only after you’ve spent hours doing a bunch of other “obvious” things, if you’re llike me).

The variable is already added to the serializer. I’m trying to access these within JS. I thought I’d get it from the controller variable I mentioned in the original post, but I don’t understand how to get to it.

1 Like