<p>You really, really shouldn't do this.</p>

<p>I'm not familiar with FormBuilder, but if it is well-built, its controllers will comprise thin methods calling services to do the grunt work; services which you can also call.</p>

<p>If there isn't a service method which does what you want (still assuming everything is well-architected), that means the author chose not to make that functionality part of FormBuilder's API, presumably for a good reason.</p>

<p>If FormBuilder isn't well-built, and instead shoves a load of code in its controllers, well, that's still not a reason for you to do something similarly ill-considered.</p>

CC BY-SA 3.0
2017-03-11T08:37:16.480Z
2017-03-11T08:37:16.480Z
Stephen Lewis
<p>I am working on extending the <a href="https://github.com/roundhouse/FormBuilder-2-Craft-CMS" rel="nofollow noreferrer">formbuilder2</a> plugin to call and pass the $event variable to a custom Controller after FormBuilder2 has initialized the "onBeforeSave" event within the FormBuilder2 Entry_Controller.</p>

<p><strong>Background Info</strong></p>

<ul>
<li>Trying to extend the Formbuilder2 plugin</li>
<li>My plugin is called FormBuilder2ExtensionPlugin</li>
<li>Trying to call a Controller Function from within my FormBuilder2Extension_NotifyController and pass $event variables.</li>
<li>Within my FormBuilder2Extension_NotifyController, I am calling the actionNotifyAdmin($event) function.</li>
</ul>

<p><strong>My Plugin Logic:</strong></p>

<ol>
<li><p>FormBuilder2 Plugin saves entry and initializes the "onBeforeSave"
event.</p></li>
<li><p>FormBuilder2Extension Plugin listens for "onBeforeSave"
event, and triggers a custom controller function to run. <em>Note: Must be
able to pass the $event variables to the new FormBuilder2Extension
controller function as well.</em></p></li>
<li><p>FormBuilder2Extension Controller will then parse $event data, and
run other FormBuilder2 Controller &amp; Service functions using the $data, so I will need to link from FormBuilder2Extension Controller functions to Formuilder2 Controller functions.</p></li>
</ol>

<p><strong>My Problem:</strong></p>

<ul>
<li>Cannot figure out how to run a Controller Function from within the init() event listener and pass the $event variables as well.</li>
</ul>

<p><strong>My Current Code: (within main plugin file)</strong></p>

<pre><code> public function init()
   {
       parent::init();

       craft()-&gt;on('formBuilder2_entry.beforeSave', function(Event $event)
       {

         $url = UrlHelper::getActionUrl('formBuilder2Extension/notify/notifyAdmin', array('event' =&gt; $event));

         return;
       });
   }
</code></pre>

<p><strong>Closing Remarks:</strong></p>

<p>It works if I use a service function.</p>

<p>Example: <code>craft()-&gt;formBuilder2Extension_notify-&gt;notifySubmitterOfSubmission($event);</code></p>

<p>But I would rather not rewrite the same Controller &amp; Service functions that formbuilder2 has already written and place it within my service folder.</p>

<p>Any thoughts…? Thanks!</p>

<plugin-development><events><controller>
Trouble calling a plugin's controller function from within an event listener function
# Trouble calling a plugin's controller function from within an event listener function

(Asked by: username_0 on 2017-03-10)

I am working on extending the formbuilder2 plugin to call and pass the $event variable to a custom Controller after FormBuilder2 has initialized the "onBeforeSave" event within the FormBuilder2 Entry_Controller.
Background Info

Trying to extend the Formbuilder2 plugin
My plugin is called FormBuilder2ExtensionPlugin
Trying to call a Controller Function from within my FormBuilder2Extension_NotifyController and pass $event variables.
Within my FormBuilder2Extension_NotifyController, I am calling the actionNotifyAdmin($event) function.

My Plugin Logic:

FormBuilder2 Plugin saves entry and initializes the "onBeforeSave"
event.
FormBuilder2Extension Plugin listens for "onBeforeSave"
event, and triggers a custom controller function to run. Note: Must be
able to pass the $event variables to the new FormBuilder2Extension
controller function as well.
FormBuilder2Extension Controller will then parse $event data, and
run other FormBuilder2 Controller & Service functions using the $data, so I will need to link from FormBuilder2Extension Controller functions to Formuilder2 Controller functions.

My Problem:

Cannot figure out how to run a Controller Function from within the init() event listener and pass the $event variables as well.

My Current Code: (within main plugin file)
 public function init()
   {
       parent::init();

       craft()->on('formBuilder2_entry.beforeSave', function(Event $event)
       {

         $url = UrlHelper::getActionUrl('formBuilder2Extension/notify/notifyAdmin', array('event' => $event));

         return;
       });
   }

Closing Remarks:
It works if I use a service function.
Example: craft()->formBuilder2Extension_notify->notifySubmitterOfSubmission($event);
But I would rather not rewrite the same Controller & Service functions that formbuilder2 has already written and place it within my service folder.
Any thoughts…? Thanks!

**Tags:** plugin-development, events, controller


---

## 1 answers

### Answer by username_1 on 2017-03-11. Score: 2

You really, really shouldn't do this.

I'm not familiar with FormBuilder, but if it is well-built, its controllers will comprise thin methods calling services to do the grunt work; services which you can also call.

If there isn't a service method which does what you want (still assuming everything is well-architected), that means the author chose not to make that functionality part of FormBuilder's API, presumably for a good reason.

If FormBuilder isn't well-built, and instead shoves a load of code in its controllers, well, that's still not a reason for you to do something similarly ill-considered.


---

