I’m writing a plugin to extend badge functionality. When the “Save” button is pressed on the admin badges page, the custom fields are always default value.
Discourse PluginOutlet
<PluginOutlet
@name="admin-above-badge-buttons"
@outletArgs={{hash badge=this.buffered}}
/>
My code assets\javascripts\discourse\initializers\badge-avatar-frame-setup.js
:
import { withPluginApi } from "discourse/lib/plugin-api";
import { ajax } from "discourse/lib/ajax";
export default {
name: "badge-avatar-frame-setup",
initialize() {
withPluginApi("0.8.31", (api) => {
api.modifyClass("model:badge", {
pluginId: "discourse-badge-avatar-frame",
pluginProperties: ["avatar_frame_enabled"],
save(data = {}) {
this.pluginProperties.forEach((prop) => {
if (this[prop] !== undefined) {
data[prop] = this[prop];
}
});
console.log("Saving badge with data:", data);
console.log("Saving badge:", this);
console.log("avatar_frame_enabled:", this.avatar_frame_enabled);
console.log("data:", data);
let url = "/admin/badges",
type = "POST";
if (this.id) {
url += `/${this.id}`;
type = "PUT";
}
return ajax(url, { type, data }).then((json) => {
this.updateFromJson(json);
return this;
});
},
});
});
},
};
refer from:Having a hard time overriding the save() function on the admin badge page
After I click the save button. can see it from console.
![admin badge page after click the save button|690x464]
data:{
"allow_title": true,
"multiple_grant": false,
"listable": true,
"auto_revoke": true,
"enabled": true,
"show_posts": false,
"target_posts": false,
"name": "新しいバッジ",
"description": "",
"long_description": "",
"icon": "angle-left",
"image_upload_id": null,
"image_url": null,
"query": null,
"badge_grouping_id": 6,
"trigger": 0,
"badge_type_id": 1,
"show_in_post_header": false,
"avatar_frame_enabled": false // always default value
}
My code assets\javascripts\discourse\connectors\admin-above-badge-buttons\badge-avatar-frame-settings.hbs
:
{{#if this.siteSettings.avatar_frame_enabled}}
<div class="badge-avatar-frame-settings">
<h3>{{i18n "admin.badges.avatar_frame_settings"}}</h3>
<div class="control-group">
<label class="checkbox-label">
<Input @type="checkbox" @checked={{this.avatar_frame_enabled}} />
{{i18n "admin.badges.avatar_frame_enabled"}}
+
{{log @outletArgs.badge}}
{{log @outletArgs}}
</label>
</div>
</div>
{{/if}}
This is console output
My code db\migrate\20250509000000_add_avatar_fields_to_badges.rb
:
class AddAvatarFieldsToBadges < ActiveRecord::Migration[6.1]
def change
add_column :badges, :avatar_frame_enabled, :boolean, default: false, null: false
end
end
Is it normal for ’@outletArgs.badge‘ to get an undefined value? What am I doing wrong?
How can I bind custom fields and sync form data on admin badge page?