/
SelectWithApiDataExtension.php
106 lines (88 loc) · 3.07 KB
/
SelectWithApiDataExtension.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
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Extension;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Support Remote-API calls for Entity select-boxes.
*/
class SelectWithApiDataExtension extends AbstractTypeExtension
{
/**
* @var UrlGeneratorInterface
*/
private $router;
/**
* @param UrlGeneratorInterface $router
*/
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public static function getExtendedTypes(): iterable
{
return [EntityType::class];
}
/**
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (!isset($options['api_data'])) {
return;
}
$apiData = $options['api_data'];
if (!\is_array($apiData)) {
throw new \InvalidArgumentException('Option "api_data" must be an array for form "' . $form->getName() . '"');
}
if (!isset($apiData['select'])) {
return;
}
if (!isset($apiData['route'])) {
throw new \InvalidArgumentException('Missing "route" option for "api_data" option for form "' . $form->getName() . '"');
}
if (!isset($apiData['route_params'])) {
$apiData['route_params'] = [];
}
$formPrefixes = [];
$parent = $form->getParent();
do {
$formPrefixes[] = $parent->getName();
} while (($parent = $parent->getParent()) !== null);
$formPrefix = implode('_', array_reverse($formPrefixes));
$formField = $apiData['select'];
// forms with prefix (like toolbar & search) would result in a wrong field name "_foo" instead of "foo"
if ($formPrefix !== '') {
$formField = $formPrefix . '_' . $apiData['select'];
}
$view->vars['attr'] = array_merge($view->vars['attr'], [
'data-form-prefix' => $formPrefix,
'data-related-select' => $formField,
'data-api-url' => $this->router->generate($apiData['route'], $apiData['route_params']),
]);
if (isset($apiData['empty_route_params'])) {
$view->vars['attr'] = array_merge($view->vars['attr'], [
'data-empty-url' => $this->router->generate($apiData['route'], $apiData['empty_route_params']),
]);
}
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(['api_data']);
$resolver->setAllowedTypes('api_data', 'array');
}
}