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
|
|
|
|
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.