1

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:

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

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.