Skip to content

Form API

Form submissions

To manage form submissions created in the Form Builder, use FormSubmissionServiceInterface.

Restricting access to form submissions

By default, back office users with access to the form content item can access the form submissions.

If your form submissions require stricter access control than the form itself, you can introduce a dedicated policy that manages access to submission data.

Getting form submissions

To get existing form submissions, use FormSubmissionServiceInterface::loadByContent() (which takes a ContentInfo object as parameter), or FormSubmissionServiceInterface::loadById().

1
        $submissions = $this->formSubmissionService->loadByContent($contentInfo);

Through this object, you can get information about submissions, such as their total number, and submission contents.

1
2
3
4
5
6
7
8
9
        $output->writeln('Total number of submissions: ' . $submissions->getTotalCount());
        foreach ($submissions as $sub) {
            $output->write($sub->getId() . '. submitted on ');
            $output->write($sub->getCreated()->format('Y-m-d H:i:s') . ' by ');
            $output->writeln((string) $this->userService->loadUser($sub->getUserId())->getName());
            foreach ($sub->getValues() as $value) {
                $output->writeln('- ' . $value->getIdentifier() . ': ' . $value->getDisplayValue());
            }
        }

Creating form submissions

To create a form submission, use the FormSubmissionServiceInterface::create() method.

This method takes:

  • the ContentInfo object of the content item containing the form
  • the language code
  • the value of the field containing the form
  • the array of form field values
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
        $formValue = $content->getFieldValue('form', 'eng-GB')->getFormValue();
        $data = [
            ['id' => 7, 'identifier' => 'single_line', 'name' => 'Line', 'value' => 'The name'],
            ['id' => 8, 'identifier' => 'number', 'name' => 'Number', 'value' => 123],
            ['id' => 9, 'identifier' => 'checkbox', 'name' => 'Checkbox', 'value' => 0],
        ];

        $this->formSubmissionService->create(
            $contentInfo,
            'eng-GB',
            $formValue,
            $data
        );

Deleting form submissions

You can delete a form submission by using the FormSubmissionServiceInterface::delete() method.

1
2
        $submission = $this->formSubmissionService->loadById(29);
        $this->formSubmissionService->delete($submission);