# Not getting custom fields on :approved\_post event

**URL:** <https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649>\
**Category:** Development\
**Created:** [August 13, 2019, 10:22am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649 "2019-08-13T10:22:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)\
**Post date:** [August 13, 2019, 10:22am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/1 "2019-08-13T10:22:28Z")

</div>

I am trying to save custom fields for a post that needs review. When I approve the post, the custom fields don’t get saved.

I checked the :approved\_post event and the custom fields aren’t getting set in the payload.

How to preserve custom fields for reviewable posts.

Also, I used NewPostManager, and there in the handler, the custom fields are available, but ofcourse it gets triggered before saving the data to reviewables.

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [August 14, 2019, 7:10am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/2 "2019-08-14T07:10:53Z")

</div>

Can you show us some code? Ideally the minimal amount we can use to reproduce this issue.

---

<div class="post-metadata">

**Author:** ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)\
**Post date:** [August 14, 2019, 7:20am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/3 "2019-08-14T07:20:30Z")

</div>

I’m fixing an issue with events plugin where the events data in the post\_custom\_field doesn’t get saved if the category is set to require approval for topics.  
I reffered to the **polls** plugin and there it seems that the custom fields are being set on  
`:appoved_post`.  
this is the snippet from polls.

```plaintext
NewPostManager.add_handler(1) do |manager|
    post = Post.new(raw: manager.args[:raw])

    if !DiscoursePoll::PollsValidator.new(post).validate_polls
      result = NewPostResult.new(:poll, false)

      post.errors.full_messages.each do |message|
        result.errors[:base] << message
      end

      result
    else
      manager.args["is_poll"] = true
      nil
    end
  end

```

```plaintext
on(:approved_post) do |queued_post, created_post|
    if queued_post.payload["is_poll"]
      created_post.validate_polls(true)
    end
  end

```

In my attempt, the custom fields data is available at, `NewPostManager`, but the `ReviewableQueuedPost` instance in the callback of `:approved_post` (which ideally should have the custom field in its payload) doesn’t have it.

I tried

```plaintext

NewPostManager.add_handler do |manager|
 if manager.args['event'] && NewPostManager.post_needs_approval?(manager) # this condition stands true 
      manager.args['is_event'] = true
end

```

then

```plaintext
on(:approved_post) do |reviewable, post|
   p reviewable.payload['is_event'] #data isn't available here
 end

```

---

<div class="post-metadata">

**Author:** ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)\
**Post date:** [August 14, 2019, 12:40pm UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/4 "2019-08-14T12:40:03Z")

</div>

This is interesting because I would not expect the polls to work here either. The args passed to manager are not automatically put into the reviewable’s payload.

I suspect there’s a bug here with polls and queued posts. I suspect `validate_polls` is never called after approval. @Roman can you look into it?

---

<div class="post-metadata">

**Author:** ![Roman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/roman/32/157504_2.png) [@Roman](https://meta.discourse.org/u/Roman)\
**Post date:** [August 14, 2019, 5:42pm UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/5 "2019-08-14T17:42:38Z")

</div>

I confirm this is a bug. `is_poll` is not being stored inside the payload, making the callback useless.

Here’s a PR to fix it:

[https://github.com/discourse/discourse/pull/8009](https://github.com/discourse/discourse/pull/8009)

I also added a new interface so we can extend the list of allowed attributes from a plugin.

---

<div class="post-metadata">

**Author:** ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)\
**Post date:** [August 14, 2019, 6:19pm UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/6 "2019-08-14T18:19:26Z")

</div>

Thanks a lot for this @Roman, @eviltrout. I spent 2 complete days figuring this out.

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [August 14, 2019, 6:20pm UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/7 "2019-08-14T18:20:08Z")

</div>

Well done @fzngagan!

---

<div class="post-metadata">

**Author:** ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)\
**Post date:** [August 15, 2019, 1:47pm UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/8 "2019-08-15T13:47:36Z")

</div>

@Roman, Do you think this feature needs to be backported?  
I think it should be so that people on tests-passed and release branches can also benifit. I think some if not most plugins that use `post\_custom\_fields’ need to handle this edge case.

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [August 16, 2019, 5:13am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/9 "2019-08-16T05:13:31Z")

</div>

@fzngagan looks like this is already on `tests-passed`, see [Commits · discourse/discourse · GitHub](https://github.com/discourse/discourse/commits/tests-passed)

---

<div class="post-metadata">

**Author:** ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)\
**Post date:** [August 16, 2019, 5:14am UTC](https://meta.discourse.org/t/not-getting-custom-fields-on-approved-post-event/125649/10 "2019-08-16T05:14:53Z")

</div>

@merefield. Ohh, I didn’t notice.
