This document is for an older version of

Express Forms

.

View latest version →

Express Forms Express Forms for Craft

Field Events

If you wish to extend the capabilities of Exporting data in Express Forms, feel free to use any of the events listed below:

Registering Export Types

The EVENT_REGISTER_EXPORT_TYPES event is called when Express Forms is looking for exporting types available to forms. You can create your own exporters and make them available for forms by listening to this event.

use Solspace\ExpressForms\services\Export;
use Solspace\ExpressForms\events\export\RegisterExportTypesEvent;

Event::on(
    Export::class,
    Export::EVENT_REGISTER_EXPORT_TYPES,
    function (RegisterExportTypesEvent $event) {
        $event->addType('My Custom Export Type');
    }
);
1
2
3
4
5
6
7
8
9
10

Compiling Exportable Fields

The EVENT_COMPILE_EXPORTABLE_FIELDS event is called when the export controller is fetching field values that should be exported. You can use this event to attach extra fields to the query which should be exported.

use Solspace\ExpressForms\controllers\ExportController;
use Solspace\ExpressForms\events\export\CompileExportableFields;
use Solspace\ExpressForms\objects\Export\StringField;

Event::on(
    ExportController::class,
    ExportController::EVENT_COMPILE_EXPORTABLE_FIELDS,
    function (CompileExportableFields $event) {
        $event->addField(
            new StringField(
                "mytable.[[myColumnName]]",
                "My Column"
            )
        );
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Building the Exportable Data Query

Use the EVENT_BUILD_QUERY event to join any extra tables or attach any other conditions necessary to export the data that you need.

use Solspace\ExpressForms\controllers\ExportController;
use Solspace\ExpressForms\events\export\BuildExportQueryEvent;

Event::on(
    ExportController::class,
    ExportController::EVENT_BUILD_QUERY,
    function (BuildExportQueryEvent $event) {
        // Inner join another table used in the previous event example
        // on the submission table `s` that is set as the FROM table
        $event
            ->getQuery()
            ->innerJoin(
                '{{%mytable}} mytable',
                'mytable.[[elementId]] = s.[[id]]'
            );
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

When Exporting Submissions

The EVENT_EXPORT_SUBMISSIONS event is called when Express Forms is exporting submission data. You can do anything with the exportable data and then return it however you want - either as a file, or display on their screens, save it as a file.

use Solspace\ExpressForms\services\Export;
use Solspace\ExpressForms\events\export\ExportSubmissionsEvent;

Event::on(
    Export::class,
    Export::EVENT_EXPORT_SUBMISSIONS,
    function (ExportSubmissionsEvent $event) {
        if ($event->getType() !== 'my-custom-export-type') {
            return;
        }

        $content = '';

        $submissions = $event->getSubmissions();
        foreach ($submissions AS $values) {
            foreach ($values as $index => $value) {
                if (is_array($value)) {
                    $value = implode(', ', $value);
                } else if ($value instanceof \DateTime) {
                    $value = $value->format('Y-m-d H:i:s');
                } else if (is_bool($value)) {
                    $value = $value ? 'yes' : 'no';
                }

                $value = htmlentities($value);

                $content .= $value . ", ";
            }

            $content .= "\n";
        }

        $fileName = sprintf(
            '%s submissions %s.my-type',
            $event->getForm()->getName(),
            date('Y-m-d H:i')
        );

        $response = $event->getResponse();
        $response->sendContentAsFile($content, $fileName, ['mimeType' => 'text/plain']);
    }
);
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