Skip to content

Form API

Form submissions

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

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($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);