1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Form\EventListener; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\NodeBundle\Form\Type\URLChooserType; |
6
|
|
|
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
9
|
|
|
use Symfony\Component\Form\FormEvent; |
10
|
|
|
use Symfony\Component\Form\FormEvents; |
11
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
12
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class URLChooserLinkTypeSubscriber |
16
|
|
|
*/ |
17
|
|
|
class URLChooserLinkTypeSubscriber implements EventSubscriberInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public static function getSubscribedEvents() |
23
|
|
|
{ |
24
|
|
|
return array( |
25
|
|
|
FormEvents::POST_SUBMIT => 'postSubmit', |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* When changing the link type, the form get's submitted with an ajax callback in the url_chooser.js; |
31
|
|
|
* We add the URL field only as an URL Chooser if it's an external link. |
32
|
|
|
* |
33
|
|
|
* @param FormEvent $event |
34
|
|
|
*/ |
35
|
|
|
public function postSubmit(FormEvent $event) |
36
|
|
|
{ |
37
|
|
|
// Suppress validation |
38
|
|
|
$event->stopPropagation(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$constraints = []; |
41
|
|
|
$attributes['class'] = 'js-change-urlchooser'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
$form = $event->getForm()->getParent(); |
44
|
|
|
$linkType = $event->getData(); |
45
|
|
|
|
46
|
|
|
if ($linkType) { |
47
|
|
|
$form->remove('link_url'); |
48
|
|
|
|
49
|
|
View Code Duplication |
switch ($linkType) { |
50
|
|
|
case URLChooserType::INTERNAL: |
51
|
|
|
$attributes['choose_url'] = true; |
52
|
|
|
|
53
|
|
|
break; |
54
|
|
|
case URLChooserType::EXTERNAL: |
55
|
|
|
$attributes['placeholder'] = 'https://'; |
56
|
|
|
$constraints[] = new ValidExternalUrl(); |
57
|
|
|
|
58
|
|
|
break; |
59
|
|
|
case URLChooserType::EMAIL: |
60
|
|
|
$constraints[] = new Email(); |
61
|
|
|
|
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$form->add('link_url', TextType::class, array( |
66
|
|
|
'label' => 'URL', |
67
|
|
|
'required' => true, |
68
|
|
|
'attr' => $attributes, |
69
|
|
|
'constraints' => $constraints, |
70
|
|
|
'error_bubbling' => true, |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.