1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Form\EventListener; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\NodeBundle\Form\Type\URLChooserType; |
6
|
|
|
use Kunstmaan\NodeBundle\Validation\URLValidator; |
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
|
|
|
class URLChooserFormSubscriber implements EventSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
use URLValidator; |
17
|
|
|
|
18
|
|
|
public static function getSubscribedEvents() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
FormEvents::POST_SET_DATA => 'postSetData', |
22
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* When opening the form for the first time, check the type of URL and set the according fields. |
28
|
|
|
* |
29
|
|
|
* @param FormEvent $event |
30
|
|
|
*/ |
31
|
|
|
public function postSetData(FormEvent $event) |
32
|
|
|
{ |
33
|
|
|
$this->formModifier($event); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function preSubmit(FormEvent $event) |
37
|
|
|
{ |
38
|
|
|
$this->formModifier($event); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function formModifier(FormEvent $event) |
42
|
|
|
{ |
43
|
|
|
$form = $event->getForm(); |
44
|
|
|
$data = $form->getData(); |
45
|
|
|
|
46
|
|
|
$constraints = []; |
47
|
|
|
$attributes['class'] = 'js-change-urlchooser'; |
|
|
|
|
48
|
|
|
|
49
|
|
|
if (!empty($data) && $form->has('link_type')) { |
50
|
|
|
// Check if e-mail address |
51
|
|
|
if ($this->isEmailAddress($data)) { |
52
|
|
|
$form->get('link_type')->setData(URLChooserType::EMAIL); |
53
|
|
|
$constraints[] = new Email(); |
54
|
|
|
} // Check if internal link |
55
|
|
|
elseif ($this->isInternalLink($data) || $this->isInternalMediaLink($data)) { |
56
|
|
|
$form->get('link_type')->setData(URLChooserType::INTERNAL); |
57
|
|
|
$attributes['choose_url'] = true; |
58
|
|
|
} // Else, it's an external link |
59
|
|
|
else { |
60
|
|
|
$form->get('link_type')->setData(URLChooserType::EXTERNAL); |
61
|
|
|
$constraints[] = new Url(); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$choices = $form->get('link_type')->getConfig()->getOption('choices'); |
65
|
|
|
$firstOption = array_shift($choices); |
66
|
|
|
|
67
|
|
View Code Duplication |
switch ($firstOption) { |
|
|
|
|
68
|
|
|
case URLChooserType::INTERNAL: |
69
|
|
|
$attributes['choose_url'] = true; |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case URLChooserType::EXTERNAL: |
73
|
|
|
$attributes['placeholder'] = 'https://'; |
74
|
|
|
$constraints[] = new Url(); |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
case URLChooserType::EMAIL: |
78
|
|
|
$constraints[] = new Email(); |
79
|
|
|
|
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$form->get('link_type')->setData($firstOption); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$form->add( |
87
|
|
|
'link_url', |
88
|
|
|
TextType::class, |
89
|
|
|
[ |
90
|
|
|
'label' => 'URL', |
91
|
|
|
'required' => true, |
92
|
|
|
'attr' => $attributes, |
93
|
|
|
'constraints' => $constraints, |
94
|
|
|
'error_bubbling' => true, |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.