# Hoe voeg ik een super basic topic custom field toe

**URL:** <https://meta.discourse.org/t/how-to-add-a-super-basic-topic-custom-field/184229>\
**Category:** Development\
**Created:** [23 maart 2021 om 19:56 UTC](https://meta.discourse.org/t/how-to-add-a-super-basic-topic-custom-field/184229 "2021-03-23T19:56:21Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![JQ331](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@JQ331](https://meta.discourse.org/u/JQ331)\
**Post date:** [23 maart 2021 om 19:56 UTC](https://meta.discourse.org/t/how-to-add-a-super-basic-topic-custom-field/184229/1 "2021-03-23T19:56:21Z")

</div>

I’m trying to understand how to add a custom field to topics, and working through a very basic example. Goal: Add a custom field called “sample\_field” to each topic created, with a simple string value.

I’ve reviewed various examples, like [the poll plugin](https://github.com/discourse/discourse/tree/master/plugins/poll) and the [solved plugin](https://github.com/discourse/discourse/tree/master/plugins/poll) and [this discussion](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/2), but these plugins do so much more with their custom fields that I haven’t yet figured out the basic code you need.

So I’m not quite there–my plugin.rb file is missing something (I think), and I haven’t yet figured out how to bind the string value to the custom\_field at the time the topic is created.

What do I need to make this work?

Any help is appreciated. Thanks!

Here’s what I’ve got

**plugin.rb:**  
_//create the custom field:_

```plaintext
  after_initialize do
     Topic.register_custom_field_type('sample_field', :string)
     add_to_serializer(:topic_view, :custom_fields) { object.custom_fields } //if I want to show the custom field on the client side
  end

```

**assets/javascripts/initializers/topic-custom-field.js.es6:**  
_//initialize the custom fields object, and make it so it can be sent to the server:_

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

export default {
  name: 'topic-custom-field',
  initialize() {
    withPluginApi('0.8.31', api => {
      api.modifyClass('model:topic', {
        custom_fields: {},
        asJSON() {
          return Object.assign(this._super(), {
            custom_fields: this.custom_fields
          });
        }
      })
    })
  }
}

```

* * *

**Then, how do I add the value of the custom field to the topic at the time it is saved?**

I believe the key “save” action for a topic happens here in the codebase:

app/templates/composer.hbs:

```plaintext
  <div class="save-or-cancel">
            {{#unless model.viewFullscreen}}
              {{composer-save-button action=(action "save")
                                     icon=saveIcon
                                     label=saveLabel
                                     disableSubmit=disableSubmit}}
...

```

How do I do something (in this case, add a value to the custom field) when that “save” action occurs?

I’ve tried creating a js file under initializers, where I could do something like:

```
api.modifyClass('component:composer-save-button', {
   actions: {
       topic.set('custom_fields.sample_field', 'here's a value for the sample_field')
   }
}

```

But I need to be able to link that “topic.set” to the “save” action in composer.hbs, and I don’t know how to do that.

* * *

And if there’s a simpler way to do this, I’m happy to hear it!

---

_[View the full topic](https://meta.discourse.org/t/how-to-add-a-super-basic-topic-custom-field/184229)._
