|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
|
8
|
|
|
* code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Form; |
|
12
|
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\ImageEntityNameResolver; |
|
14
|
|
|
use Symfony\Component\Form\AbstractType; |
|
15
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
|
18
|
|
|
use Symfony\Component\Form\FormEvent; |
|
19
|
|
|
use Symfony\Component\Form\FormEvents; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ResponsiveImageType |
|
23
|
|
|
* |
|
24
|
|
|
* @package IrishDan\ResponsiveImageBundle\Form |
|
25
|
|
|
*/ |
|
26
|
|
|
class ResponsiveImageType extends AbstractType |
|
27
|
|
|
{ |
|
28
|
|
|
private $responsiveImageEntityName; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(ImageEntityNameResolver $entityNameResolver) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->responsiveImageEntityName = $entityNameResolver->getClassName(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param FormBuilderInterface $builder |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
*/ |
|
39
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
40
|
|
|
{ |
|
41
|
|
|
$builder |
|
42
|
|
|
->add('title') |
|
43
|
|
|
->add('alt'); |
|
44
|
|
|
|
|
45
|
|
|
$builder->addEventListener( |
|
46
|
|
|
FormEvents::PRE_SET_DATA, |
|
47
|
|
|
function (FormEvent $event) { |
|
48
|
|
|
$image = $event->getData(); |
|
49
|
|
|
$form = $event->getForm(); |
|
50
|
|
|
// Conditionally add form elements. |
|
51
|
|
|
if (!empty($image) && !empty($image->getId())) { |
|
52
|
|
|
$form->add( |
|
53
|
|
|
'crop_coordinates', |
|
54
|
|
|
CropFocusType::class, |
|
55
|
|
|
[ |
|
56
|
|
|
'data' => $image, |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
else { |
|
61
|
|
|
$form->add('file', FileType::class, ['label' => 'Upload an image']); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param OptionsResolver $resolver |
|
69
|
|
|
*/ |
|
70
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
71
|
|
|
{ |
|
72
|
|
|
$resolver->setDefaults( |
|
73
|
|
|
[ |
|
74
|
|
|
'data_class' => $this->responsiveImageEntityName, |
|
75
|
|
|
] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |