# Plugin: access to toolbarEvent from modal

**URL:** https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413
**Category:** Development
**Created:** [October 19, 2021, 7:02am UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413 "2021-10-19T07:02:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sirdavidoff](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sirdavidoff](https://meta.discourse.org/u/sirdavidoff)
#### Post date: [October 19, 2021, 7:02am UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/1 "2021-10-19T07:02:11Z")

</div>

My plugin inserts a button in the composer toolbar. This toolbar opens a modal, and when the modal is closed I want to add text to the post. Everything works perfectly except the last step, because I’m struggling to give the modal access to the composer.

**If I put the button under a menu in the toolbar, everything works fine** :

```js
      api.modifyClass("controller:composer", {
        pluginId: "discourse-n8n-wf",
        actions: {
          showWfModal2() {
            showModal("discourse-n8n-wf").setProperties({"toolbarEvent": this.toolbarEvent});
          },
        },
      });
      api.addToolbarPopupMenuOptionsCallback(() => {
        return {
          action: "showWfModal2",
          icon: "network-wired",
          label: "n8n workflow"
        };
      });

```

**But if I put it on the top level, I don’t seem to have access to the toolbarEvent** to pass to the modal

```js
      Composer.reopen({
        actions: {
          showWfModal: function () {
            console.log(this.toolbarEvent); // undefined
            showModal('discourse-n8n-wf', {title:'Insert n8n workflow'});
          }
        }
      });

      api.onToolbarCreate(toolbar => {
        toolbar.addButton({
          id: 'discourse-n8n-wf',
          group: 'extras',
          icon: 'network-wired',
          action: 'showWfModal',
          title: 'Insert workflow'
        });
      });

```

Any tips?

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [October 19, 2021, 2:28pm UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/2 "2021-10-19T14:28:12Z")

</div>

The `toolbarEvent` should be available in your modal controller (discourse-n8n-wf), you don’t need to pass it. You can also look at how other modals do this, for example, see `discourse-post-event-builder.js.es6` in the poll plugin.

---

<div class="post-metadata">

### Author: ![sirdavidoff](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sirdavidoff](https://meta.discourse.org/u/sirdavidoff)
#### Post date: [October 19, 2021, 5:14pm UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/3 "2021-10-19T17:14:18Z")

</div>

Thanks, Penar! I’m not quite there yet though:

I found the file you referenced in the `discourse-calendar` plugin (couldn’t find it in the poll one), but there it seems like the toolbarEvent is indeed being passed in the action (through `setProperties()`)?

> <https://github.com/discourse/discourse-calendar/blob/7d583a9aaf449a90662feb84441906a4c9e3ada5/assets/javascripts/initializers/add-event-ui-builder.js.es6#L43>

---

<div class="post-metadata">

### Author: ![sirdavidoff](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sirdavidoff](https://meta.discourse.org/u/sirdavidoff)
#### Post date: [October 22, 2021, 7:32am UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/4 "2021-10-22T07:32:04Z")

</div>

Was just wondering whether you had any more thoughts here, @pmusaraj?

---

<div class="post-metadata">

### Author: ![keegan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/keegan/32/383395_2.png) [@keegan](https://meta.discourse.org/u/keegan)
#### Post date: [March 14, 2022, 5:30pm UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/5 "2022-03-14T17:30:48Z")

</div>

Hey @sirdavidoff

Not sure if you’re still stuck on this but I’ve went down the rabbit hole of trying to figure out how to access the toolbar event from a modal as well, and I found you can pass the event as a parameter like this:

`action: (event) => `

So I used it like this:

```js
  const composerController = api.container.lookup('controller:composer');

  api.onToolbarCreate((toolbar) => {
    toolbar.addButton({
      id: 'math-editor',
      group: 'extras',
      icon: 'square-root-alt',
      sendAction: (event) => composerController.send('showMathEditor', event),
      title: themePrefix('insert_equation'),
    });
  });

```

and then you can use it in your `action: {} method` like so:

```js
@action
showMathEditor(event) {
   showModal('matheditor-modal').set('toolbarEvent', event);
}

```

---

<div class="post-metadata">

### Author: ![Joshua\_Soileau](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joshua_soileau/32/482618_2.png) [@Joshua\_Soileau](https://meta.discourse.org/u/Joshua_Soileau)
#### Post date: [January 21, 2025, 3:45pm UTC](https://meta.discourse.org/t/plugin-access-to-toolbarevent-from-modal/206413/6 "2025-01-21T15:45:36Z")

</div>

Want to pile on – years later, I had the same issue (didn’t have access to toolbarEvent inside a modal), and this solution also worked for me.

```plaintext
      api.onToolbarCreate((toolbar) => {
        toolbar.addButton({
          ...
          sendAction: (event) =>
            toolbar.context.send("triggerMyModal", event),
          ...
        });
      });

      api.modifyClass("component:d-editor", {
        modal: service(),
        pluginId: "poll",
        actions: {
          triggerMyModal(toolbarEvent) {
            this.modal.show(MyCustomModal, {
              model: {
                toolbarEvent,
              },
            });
          },
        },
      });

```

Thanks for the help, Keegan.
