<blockquote>
  <p>How does one use this event? What can it be used for?</p>
</blockquote>

<p>It gives plugins a chance to manipulate the posted message to however they want instead of using the plugin's default implementation.  A plugin would listen to it like they listen to any <a href="https://craftcms.com/docs/plugins/hooks-and-events#events" rel="nofollow">other event</a>. Something like:</p>

<pre><code>public function init()
{
    craft()-&gt;on('contactForm.onBeforeMessageCompile', function(Event $event) {
        $postedMessage = $event-&gt;params['postedMessage'];
        $event-&gt;messageFields = $postedMessage;

        // Do your thing.

        $event-&gt;message = "Your custom compiled message";
        $event-&gt;htmlMessage = "Your custom compiled HTML message";
    });
}
</code></pre>

<blockquote>
  <p>Why is an <code>$event</code> object being instantiated at this point in the controller?</p>
</blockquote>

<p>The short answer is "because that's how events work". :) Is there a particular thing that's confusing you?</p>

CC BY-SA 3.0
2016-07-13T17:57:43.530Z
2016-07-13T17:57:43.530Z
Brad Bell
<p>I see there is an undocumented event called <code>onBeforeMessageCompile</code> in the Contact Form plugin source:</p>

<pre><code>Craft::import('plugins.contactform.events.ContactFormMessageEvent');
$event = new ContactFormMessageEvent($this, array('postedMessage' =&gt; $postedMessage));
craft()-&gt;contactForm-&gt;onBeforeMessageCompile($event);
</code></pre>

<p>My questions are:</p>

<ol>
<li>How does one use this event? What can it be used for? and</li>
<li>Why is an <code>$event</code> object being instantiated <a href="https://github.com/pixelandtonic/ContactForm/blob/master/contactform/controllers/ContactFormController.php#L55" rel="nofollow">at this point in the controller</a>?</li>
</ol>

<plugin-development><events><plugin-contactform>
What does the Before compile event inside the Contact Form plugin do?
# What does the Before compile event inside the Contact Form plugin do?

(Asked by: username_0 on 2016-07-13)

I see there is an undocumented event called onBeforeMessageCompile in the Contact Form plugin source:
Craft::import('plugins.contactform.events.ContactFormMessageEvent');
$event = new ContactFormMessageEvent($this, array('postedMessage' => $postedMessage));
craft()->contactForm->onBeforeMessageCompile($event);

My questions are:

How does one use this event? What can it be used for? and
Why is an $event object being instantiated at this point in the controller?

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


---

## 1 answers

### Answer by username_1 on 2016-07-13. Score: 1 | Accepted answer

How does one use this event? What can it be used for?

It gives plugins a chance to manipulate the posted message to however they want instead of using the plugin's default implementation.  A plugin would listen to it like they listen to any other event. Something like:
public function init()
{
    craft()->on('contactForm.onBeforeMessageCompile', function(Event $event) {
        $postedMessage = $event->params['postedMessage'];
        $event->messageFields = $postedMessage;

        // Do your thing.

        $event->message = "Your custom compiled message";
        $event->htmlMessage = "Your custom compiled HTML message";
    });
}


Why is an $event object being instantiated at this point in the controller?

The short answer is "because that's how events work". :) Is there a particular thing that's confusing you?


---

