This document is for an older version of

Freeform

.

View latest version →

User Guides

Building a Custom Module

Freeform has a wide variety of developer events and is very customizable, allowing you to easily extend Freeform for all of your needs! This guide assumes you have some level of PHP and Craft custom development experience.

Instructions

In the example below, we'll be making a Craft module that sends an email notification to a recipient, based upon the selection of a Select/Checkbox Group/Radio fields' options. This will be done using the EVENT_AFTER_SUBMIT event.

Create a new file named MyFreeformModule.php inside of the Craft modules directory:

<?php

namespace modules;

use Solspace\Freeform\Events\Submissions\SubmitEvent;
use Solspace\Freeform\Freeform;
use Solspace\Freeform\Services\SubmissionsService;
use yii\base\Event;

class MyFreeformModule extends Module
{
    public function init()
    {
        parent::init();

        // Subscribe to the AFTER_SUBMIT event
        Event::on(
            SubmissionsService::class,
            SubmissionsService::EVENT_AFTER_SUBMIT,
            function (SubmitEvent $event) {
                // ===============================
                // Set the notification ID here
                // This is the notification ID from the database
                // ===============================
                $notificationId = 5;

                $form = $event->getForm();
                $submission = $event->getSubmission();

                // Skip all other forms except "my-form-handle" form
                if ($form->getHandle() !== 'my-form-handle') {
                    return;
                }

                // Get the submitted value by which we'll be gathering recipients
                $selectedStates = $submission->state->getValue();
                if (!is_array($selectedStates)) {
                    $selectedStates = [$selectedStates];
                }

                $recipients = [];
                foreach ($selectedStates as $selectedState) {
                    switch ($selectedState) {
                        // Assign "first@recipient.com" if any of the following states is selected
                        case 'AL':
                        case 'CA':
                        case 'ID':
                        case 'IL':
                            $recipients[] = 'first@recipient.com';
                            break;

                        // Assign two recipients if some other states are selected
                        case 'MI':
                        case 'NE':
                        case 'NV':
                            $recipients[] = 'second@recipient.com';
                            $recipients[] = 'third@recipient.com';
                            break;

                        // Assign these recipients if ANY OTHER state is selected from the ones listed above
                        default:
                            $recipients[] = 'first@recipient.com';
                            $recipients[] = 'fourth@recipient.com';
                            break;
                    }
                }

                // Send the notification
                Freeform::getInstance()->mailer->sendEmail(
                    $form,
                    $recipients,
                    $notificationId,
                    $form->getLayout()->getFields(),
                    $submission
                );
            }
        );
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

Enable the module in the config/app.php config file:

<?php

return [
    'modules' => [
        // Make the module available in Craft
        'my-freeform-module' => \modules\MyFreeformModule::class,
    ],
    'bootstrap' => [
        // Initialize the module on every craft request
        'my-freeform-module',
    ],
];
1
2
3
4
5
6
7
8
9
10
11
12
Finished!