Is 'post' the correct serializer to use when one wants to access extra data via decorateWidget with post-avatar:after?

I was hoping to add information to posts in the plugin.rb file using add_to_serializer, and then access that data in my plugin’s default initializer file using decorateWidget from the api.

My plugin.rb contains

after_initialize do
  add_to_serializer :post, :foo do
    return "bar"
  end
end

From what I understand, the code should be adding a ‘foo’ attribute (with a value of ‘bar’) to each serialized post.

My only javascript initializer contains:

import { withPluginApi } from 'discourse/lib/plugin-api';

export default {
  name: 'my-test',
  initialize() {
    withPluginApi("0.8.23", api => {       
      api.decorateWidget('post-avatar:after', helper => {
        console.log(helper.attrs);
      });
    });
  }
}

The admin plugins page shows that my plugin is installed, but when I view the output in my browsers console, I do not see the ‘foo’ attribute. Obviously, I’m doing something wrong, and I would greatly appreciate some knowledge.

5 Likes

I think you will need:

api.includePostAttributes('foo');

https://github.com/discourse/discourse/blob/f7b4a2b3bac6d74b9b1949a8073f4a0314677388/app/assets/javascripts/discourse/lib/plugin-api.js.es6#L296-L310

7 Likes

Thank you! That absolutely did the trick!

4 Likes