1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\API; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ContentValueView; |
8
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\LocationValueView; |
9
|
|
|
use Netgen\InformationCollection\API\Form\DynamicFormBuilderInterface; |
10
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\AdditionalContent; |
11
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
trait InformationCollectionTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Builds Form, checks if Form is valid and dispatches InformationCollected event. |
18
|
|
|
* |
19
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentValueView $view |
20
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
21
|
|
|
* @param \Netgen\InformationCollection\API\Value\DataTransfer\AdditionalContent $additionalContent |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
protected function collectInformation(ContentValueView $view, Request $request, AdditionalContent $additionalContent): array |
26
|
|
|
{ |
27
|
|
|
$isValid = false; |
28
|
|
|
|
29
|
|
|
if (!$view instanceof LocationValueView) { |
30
|
|
|
throw new \BadMethodCallException('eZ view needs to implement LocationValueView interface'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @var DynamicFormBuilderInterface $formBuilder */ |
34
|
|
|
$formBuilder = $this->container |
|
|
|
|
35
|
|
|
->get('netgen_information_collection.form.builder'); |
36
|
|
|
|
37
|
|
|
$form = $formBuilder->createForm($view->getContent()); |
38
|
|
|
|
39
|
|
|
$form->handleRequest($request); |
40
|
|
|
|
41
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
42
|
|
|
$isValid = true; |
43
|
|
|
|
44
|
|
|
$event = new InformationCollected( |
45
|
|
|
$form->getData(), |
46
|
|
|
$view->getLocation(), |
47
|
|
|
$additionalContent |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
51
|
|
|
$dispatcher = $this->container |
52
|
|
|
->get('event_dispatcher'); |
53
|
|
|
|
54
|
|
|
$dispatcher->dispatch($event, Events::INFORMATION_COLLECTED); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return [ |
58
|
|
|
'is_valid' => $isValid, |
59
|
|
|
'form' => $form->createView(), |
60
|
|
|
'collected_fields' => $form->getData()->getFieldsData(), |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: