-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
EasyAdminExtension.php
109 lines (95 loc) · 3.28 KB
/
EasyAdminExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EasyCorp\Bundle\EasyAdminBundle\Form\Extension;
use EasyCorp\Bundle\EasyAdminBundle\Form\Util\LegacyFormHelper;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Extension that injects EasyAdmin related information in the view used to
* render the form.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class EasyAdminExtension extends AbstractTypeExtension
{
/** @var Request|null */
private $request;
/** @var RequestStack|null */
private $requestStack;
/**
* @param RequestStack|null $requestStack
*/
public function __construct(RequestStack $requestStack = null)
{
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
if (null !== $this->requestStack) {
$this->request = $this->requestStack->getCurrentRequest();
}
if (null === $this->request) {
return;
}
if ($this->request->attributes->has('easyadmin')) {
$easyadmin = $this->request->attributes->get('easyadmin');
$entity = $easyadmin['entity'];
$action = $easyadmin['view'];
$fields = isset($entity[$action]['fields']) ? $entity[$action]['fields'] : array();
$view->vars['easyadmin'] = array(
'entity' => $entity,
'view' => $action,
'item' => $easyadmin['item'],
'field' => null,
'form_group' => $form->getConfig()->getAttribute('easyadmin_form_group'),
'form_tab' => $form->getConfig()->getAttribute('easyadmin_form_tab'),
);
/*
* Checks if current form view is direct child on the topmost form
* (ie. this form view`s field exists in easyadmin configuration)
*/
if (null !== $view->parent && null === $view->parent->parent) {
$view->vars['easyadmin']['field'] = isset($fields[$view->vars['name']]) ? $fields[$view->vars['name']] : null;
}
}
}
/**
* BC for SF < 2.4.
* To be replaced by the usage of the request stack when 2.3 support is dropped.
*
* @param Request|null $request
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return LegacyFormHelper::getType('form');
}
/**
* {@inheritdoc}
*/
public static function getExtendedTypes()
{
// needed to avoid a deprecation when using Symfony 4.2
return array(LegacyFormHelper::getType('form'));
}
}
class_alias('EasyCorp\Bundle\EasyAdminBundle\Form\Extension\EasyAdminExtension', 'JavierEguiluz\Bundle\EasyAdminBundle\Form\Extension\EasyAdminExtension', false);