Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace App\Command;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\FormBuilder\FormSubmission\FormSubmissionServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class FormSubmissionCommand extends Command
{
private UserService $userService;

private PermissionResolver $permissionResolver;

private FormSubmissionServiceInterface $formSubmissionService;

private ContentService $contentService;

public function __construct(UserService $userService, PermissionResolver $permissionResolver, FormSubmissionServiceInterface $formSubmissionService, ContentService $contentService)
{
$this->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;
}
}
44 changes: 44 additions & 0 deletions docs/api/public_php_api_managing_forms.md
Original file line number Diff line number Diff line change
@@ -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) =]]
```
5 changes: 5 additions & 0 deletions docs/guide/form_builder/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down