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\Component\Form\Form; |
19
|
|
|
use Symfony\Component\Form\FormRenderer; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
use Twig\Environment; |
24
|
|
|
|
25
|
|
|
final class AppendFormFieldElementAction |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Pool |
29
|
|
|
*/ |
30
|
|
|
private $pool; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AdminHelper |
34
|
|
|
*/ |
35
|
|
|
private $helper; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Environment |
39
|
|
|
*/ |
40
|
|
|
private $twig; |
41
|
|
|
|
42
|
|
|
public function __construct(Environment $twig, Pool $pool, AdminHelper $helper) |
43
|
|
|
{ |
44
|
|
|
$this->pool = $pool; |
45
|
|
|
$this->helper = $helper; |
46
|
|
|
$this->twig = $twig; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws NotFoundHttpException |
51
|
|
|
*/ |
52
|
|
|
public function __invoke(Request $request): Response |
53
|
|
|
{ |
54
|
|
|
$code = $request->get('code'); |
55
|
|
|
$elementId = $request->get('elementId'); |
56
|
|
|
$objectId = $request->get('objectId'); |
57
|
|
|
$uniqid = $request->get('uniqid'); |
58
|
|
|
|
59
|
|
|
$admin = $this->pool->getInstance($code); |
60
|
|
|
$admin->setRequest($request); |
61
|
|
|
|
62
|
|
|
if ($uniqid) { |
63
|
|
|
$admin->setUniqid($uniqid); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$subject = $admin->getObject($objectId); |
67
|
|
|
if ($objectId && !$subject) { |
68
|
|
|
throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$subject) { |
72
|
|
|
$subject = $admin->getNewInstance(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$admin->setSubject($subject); |
76
|
|
|
|
77
|
|
|
list(, $form) = $this->helper->appendFormFieldElement($admin, $subject, $elementId); |
78
|
|
|
|
79
|
|
|
\assert($form instanceof Form); |
80
|
|
|
$view = $this->helper->getChildFormView($form->createView(), $elementId); |
81
|
|
|
|
82
|
|
|
// render the widget |
83
|
|
|
$renderer = $this->getFormRenderer(); |
84
|
|
|
$renderer->setTheme($view, $admin->getFormTheme()); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return new Response($renderer->searchAndRenderBlock($view, 'widget')); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getFormRenderer(): FormRenderer |
90
|
|
|
{ |
91
|
|
|
return $this->twig->getRuntime(FormRenderer::class); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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: