|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Action; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminHelper; |
|
17
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
18
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
|
19
|
|
|
use Symfony\Bridge\Twig\Command\DebugCommand; |
|
20
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
|
21
|
|
|
use Symfony\Bridge\Twig\Form\TwigRenderer; |
|
22
|
|
|
use Symfony\Component\Form\FormRenderer; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
26
|
|
|
use Twig\Environment; |
|
27
|
|
|
|
|
28
|
|
|
final class RetrieveFormFieldElementAction |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Pool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $pool; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var AdminHelper |
|
37
|
|
|
*/ |
|
38
|
|
|
private $helper; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Environment |
|
42
|
|
|
*/ |
|
43
|
|
|
private $twig; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(Environment $twig, Pool $pool, AdminHelper $helper) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->pool = $pool; |
|
48
|
|
|
$this->helper = $helper; |
|
49
|
|
|
$this->twig = $twig; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws NotFoundHttpException |
|
54
|
|
|
* |
|
55
|
|
|
* @return Response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __invoke(Request $request) |
|
58
|
|
|
{ |
|
59
|
|
|
$code = $request->get('code'); |
|
60
|
|
|
$elementId = $request->get('elementId'); |
|
61
|
|
|
$objectId = $request->get('objectId'); |
|
62
|
|
|
$uniqid = $request->get('uniqid'); |
|
63
|
|
|
|
|
64
|
|
|
$admin = $this->pool->getInstance($code); |
|
65
|
|
|
$admin->setRequest($request); |
|
66
|
|
|
|
|
67
|
|
|
if ($uniqid) { |
|
68
|
|
|
$admin->setUniqid($uniqid); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($objectId) { |
|
72
|
|
|
$subject = $admin->getObject($objectId); |
|
73
|
|
|
if (!$subject) { |
|
74
|
|
|
throw new NotFoundHttpException(sprintf( |
|
75
|
|
|
'Unable to find the object id: %s, class: %s', |
|
76
|
|
|
$objectId, |
|
77
|
|
|
$admin->getClass() |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
$subject = $admin->getNewInstance(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$admin->setSubject($subject); |
|
85
|
|
|
|
|
86
|
|
|
$formBuilder = $admin->getFormBuilder(); |
|
87
|
|
|
|
|
88
|
|
|
$form = $formBuilder->getForm(); |
|
89
|
|
|
$form->setData($subject); |
|
90
|
|
|
$form->handleRequest($request); |
|
91
|
|
|
|
|
92
|
|
|
$view = $this->helper->getChildFormView($form->createView(), $elementId); |
|
93
|
|
|
|
|
94
|
|
|
// render the widget |
|
95
|
|
|
$renderer = $this->getFormRenderer(); |
|
96
|
|
|
$renderer->setTheme($view, $admin->getFormTheme()); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return new Response($renderer->searchAndRenderBlock($view, 'widget')); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return FormRenderer|TwigRenderer |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getFormRenderer() |
|
105
|
|
|
{ |
|
106
|
|
|
// BC for Symfony < 3.2 where this runtime does not exists |
|
107
|
|
|
if (!method_exists(AppVariable::class, 'getToken')) { |
|
108
|
|
|
$extension = $this->twig->getExtension(FormExtension::class); |
|
109
|
|
|
$extension->initRuntime($this->twig); |
|
110
|
|
|
|
|
111
|
|
|
return $extension->renderer; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// BC for Symfony < 3.4 where runtime should be TwigRenderer |
|
115
|
|
|
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { |
|
116
|
|
|
$runtime = $this->twig->getRuntime(TwigRenderer::class); |
|
117
|
|
|
$runtime->setEnvironment($this->twig); |
|
118
|
|
|
|
|
119
|
|
|
return $runtime; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->twig->getRuntime(FormRenderer::class); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: