diff --git a/code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php b/code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php new file mode 100644 index 0000000000..58835823c3 --- /dev/null +++ b/code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php @@ -0,0 +1,72 @@ +userService = $userService; + $this->permissionResolver = $permissionResolver; + $this->formSubmissionService = $formSubmissionService; + $this->contentService = $contentService; + + parent::__construct('doc:form-submission'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $user = $this->userService->loadUserByLogin('admin'); + $this->permissionResolver->setCurrentUserReference($user); + + $content = $this->contentService->loadContent(143); + $contentInfo = $content->contentInfo; + + $formValue = $content->getFieldValue('form', 'eng-GB')->getFormValue(); + $data = [ + ['identifier' => 'single_line', 'name' => 'Line', 'value' => 'The name'], + ['identifier' => 'number', 'name' => 'Number', 'value' => 123], + ['identifier' => 'checkbox', 'name' => 'Checkbox', 'value' => 0], + ]; + + $this->formSubmissionService->create( + $contentInfo, + 'eng-GB', + $formValue, + $data + ); + + $submissions = $this->formSubmissionService->loadByContent($contentInfo); + + $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()); + } + } + + $submission = $this->formSubmissionService->loadById(29); + $this->formSubmissionService->delete($submission); + + return self::SUCCESS; + } +} diff --git a/docs/api/public_php_api_managing_forms.md b/docs/api/public_php_api_managing_forms.md new file mode 100644 index 0000000000..e677b77d93 --- /dev/null +++ b/docs/api/public_php_api_managing_forms.md @@ -0,0 +1,44 @@ +# Managing forms [[% include 'snippets/experience_badge.md' %]] [[% include 'snippets/commerce_badge.md' %]] + +## Form submissions + +To manage form submissions created in the [Form Builder](../guide/form_builder/forms.md), use `FormSubmissionServiceInterface`. + +### Getting form submissions + +To get existing form submissions, use `FormSubmissionServiceInterface::loadByContent()` +(which takes a `ContentInfo` object as parameter), or `FormSubmissionServiceInterface::loadById()`. + +``` php +[[= include_file('code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php', 54, 55) =]] +``` + +Through this object, you can get information about submissions, such as their total number, +and submission contents. + +``` php +[[= include_file('code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php', 55, 66) =]] +``` + +### 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 + +``` php +[[= include_file('code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php', 40, 53) =]] +``` + +### Deleting form submissions + +You can delete a form submission by using the `FormSubmissionServiceInterface::delete()` method. + +``` php +[[= include_file('code_samples/api/public_php_api/src/Command/FormSubmissionCommand.php', 66, 68) =]] +``` diff --git a/docs/guide/form_builder/forms.md b/docs/guide/form_builder/forms.md index dff73e2e53..3bf3957b00 100644 --- a/docs/guide/form_builder/forms.md +++ b/docs/guide/form_builder/forms.md @@ -2,6 +2,11 @@ You can build forms consisting of different fields in the Form Builder. +!!! tip + + To learn how to get, create, and delete form submissions by using the PHP API, + see [Managing forms](../../api/public_php_api_managing_forms.md). + ## Existing Form fields ### Captcha field diff --git a/mkdocs.yml b/mkdocs.yml index 5369fe3038..ce3aaf2de2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -59,6 +59,7 @@ nav: - 'Managing Content': 'api/public_php_api_managing_content.md' - 'Managing catalog': 'api/public_php_api_managing_catalog.md' - 'Managing prices': 'api/public_php_api_managing_prices.md' + - 'Managing forms': 'api/public_php_api_managing_forms.md' - 'Managing Repository': 'api/public_php_api_managing_repository.md' - 'Managing migrations': 'api/public_php_api_managing_migrations.md' - 'Managing Users': 'api/public_php_api_managing_users.md'