# How to override the buildQuote function?

**URL:** https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355
**Category:** Development
**Created:** [September 17, 2020, 1:46pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355 "2020-09-17T13:46:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 17, 2020, 1:46pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/1 "2020-09-17T13:46:27Z")

</div>

Hi!

I’d like to try to modify the `buildQuote` function so it adds a new line before the opening `[quote]` tag.

Basically, modifying this:

```js
  return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

```

to this:

```js
  return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

```

Is it possible to do such a thing with a theme component? How do I override this function? 😃

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 17, 2020, 4:03pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/2 "2020-09-17T16:03:07Z")

</div>

I’ve managed to achieve it, but is there a prettier solution?

```js
<script type="text/discourse-plugin" version="0.8.23">
    function buildQuote(post, contents, opts = {}) {
      if (!post || !contents) {
        return "";
      }
    
      const params = [
        opts.username || post.username,
        `post:${opts.post || post.post_number}`,
        `topic:${opts.topic || post.topic_id}`
      ];
    
      if (opts.full) params.push("full:true");
    
      return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
    }
    api.modifyClass('controller:composer', {
        actions: {
            importQuote(toolbarEvent) {
              const postStream = this.get("topic.postStream");
              let postId = this.get("model.post.id");
        
              // If there is no current post, use the first post id from the stream
              if (!postId && postStream) {
                postId = postStream.get("stream.firstObject");
              }
        
              // If we're editing a post, fetch the reply when importing a quote
              if (this.get("model.editingPost")) {
                const replyToPostNumber = this.get("model.post.reply_to_post_number");
                if (replyToPostNumber) {
                  const replyPost = postStream.posts.findBy(
                    "post_number",
                    replyToPostNumber
                  );
        
                  if (replyPost) {
                    postId = replyPost.id;
                  }
                }
              }
        
              if (postId) {
                this.set("model.loading", true);
        
                return this.store.find("post", postId).then(post => {
                  const quote = buildQuote(post, post.raw, {
                    full: true
                  });
        
                  toolbarEvent.addText(quote);
                  this.set("model.loading", false);
                });
              }
            }
        }
    });
</script>

```

---

<div class="post-metadata">

### Author: ![asc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/asc/32/288324_2.png) [@asc](https://meta.discourse.org/u/asc)
#### Post date: [February 27, 2023, 11:10pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/3 "2023-02-27T23:10:13Z")

</div>

Sorry for bumping an old topic, but is this still working for you/have you had to update it at all? I’ve been trying to get this to work and I can’t get it to fire at all, but I’ve been having a terrible time with anything at all to do with modifyClass.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [February 28, 2023, 8:05am UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/4 "2023-02-28T08:05:10Z")

</div>

> [@asc](#):
>
> but is this still working for you

I just tried it, and it is still working 👍

I literally copy-pasted it in my theme, and it worked without any modification.

---

<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: [February 28, 2023, 8:42am UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/5 "2023-02-28T08:42:43Z")

</div>

See [api.modifyClass sometimes(!) not working - #12 by RGJ](https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413/12)

I’m not sure if this has been resolved?

Also see related but different: [Issues overriding getters in a controller (3.0.0)](https://meta.discourse.org/t/issues-overriding-getters-in-a-controller-3-0-0/251793)

---

<div class="post-metadata">

### Author: ![asc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/asc/32/288324_2.png) [@asc](https://meta.discourse.org/u/asc)
#### Post date: [February 28, 2023, 2:10pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/6 "2023-02-28T14:10:30Z")

</div>

Thanks @merefield and @Canapin . I’ve been trying to learn the Discourse plugin API fiddling around with what feels like they should be simple things like this but modifyClass is so inconsistent when it will/won’t work. I’ve got a little theme component (that one you were mentioned into @merefield ) where two modifyClass calls work but a third doesn’t, very frustrating trying to figure out if I’ve got something wrong or it’s just modifyClass.

Sorry for the OT on your topic and thanks for the quick reply @Canapin

**Update:**

> [@Force a linebreak before and after a quote](https://meta.discourse.org/t/force-a-linebreak-before-and-after-a-quote/270376/5):
>
> I was able to get that working by adding
> 
> ```plaintext
> const composerController = api.container.lookup("controller:composer")
> 
> ```
> 
> to the bottom of the theme component code. It’s related to [api.modifyClass sometimes(!) not working - #12 by RGJ](https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413/12)

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [February 28, 2023, 2:16pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/7 "2023-02-28T14:16:47Z")

</div>

> [@asc](#):
>
> Sorry for the OT on your topic and thanks for the quick reply @Canapin

No worry, I don’t know what OT means but we’re here to help!

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [March 30, 2023, 2:16pm UTC](https://meta.discourse.org/t/how-to-override-the-buildquote-function/164355/8 "2023-03-30T14:16:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
