1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
16
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use Symfony\Component\Form\FormView; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
21
|
|
|
use Symfony\Component\PropertyAccess\PropertyPath; |
22
|
|
|
|
23
|
|
|
final class DownloadTypeExtension extends AbstractTypeExtension |
24
|
|
|
{ |
25
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
26
|
|
|
{ |
27
|
|
|
$resolver |
28
|
|
|
->setDefaults([ |
29
|
|
|
'download_path' => null, |
30
|
|
|
'download_text' => 'link_download', |
31
|
|
|
]) |
32
|
|
|
->setAllowedTypes('download_path', ['null', 'string']) |
33
|
|
|
->setAllowedTypes('download_text', ['null', 'string']) |
34
|
|
|
; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
38
|
|
|
{ |
39
|
|
|
if (null !== $options['download_path']) { |
40
|
|
|
$builder->setAttribute('download_path', $options['download_path']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options): void |
45
|
|
|
{ |
46
|
|
|
if (null !== $options['download_path'] && null !== $form->getParent()) { |
47
|
|
|
$parentData = $form->getParent()->getData(); |
48
|
|
|
|
49
|
|
|
if (null !== $parentData) { |
50
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
51
|
|
|
$propertyPath = new PropertyPath($options['download_path']); |
52
|
|
|
|
53
|
|
|
$downloadPath = $propertyAccessor->getValue($parentData, $propertyPath); |
54
|
|
|
} else { |
55
|
|
|
$downloadPath = null; |
56
|
|
|
} |
57
|
|
|
// set an "download_path" variable that will be available when rendering this field |
58
|
|
|
$view->vars['download_path'] = $downloadPath; |
59
|
|
|
} |
60
|
|
|
$view->vars['download_text'] = $options['download_text']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getExtendedTypes(): iterable |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
FileType::class, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|