Skip to content

Commit 647a602

Browse files
committed
Attached EventListener to link field
1 parent 28725bc commit 647a602

3 files changed

Lines changed: 32 additions & 40 deletions

File tree

src/lib/Form/EventSubscriber/FixUrlProtocolListener.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,18 @@ public function __construct(?string $defaultProtocol = 'http')
3434
public function onSubmit(FormEvent $event): void
3535
{
3636
$data = $event->getData();
37-
$dataLink = $data['link'] ?? null;
38-
if (null === $this->defaultProtocol || empty($data) || empty($dataLink) || !\is_string($dataLink)) {
37+
if (null === $this->defaultProtocol || empty($data) || !\is_string($data)) {
3938
return;
4039
}
4140

42-
$protocol = explode(':', $dataLink)[0];
41+
$protocol = explode(':', $data)[0];
4342
if ($this->hasAuthority($protocol) && $this->hasAuthority($this->defaultProtocol)) {
44-
$event->setData($dataLink);
4543
$this->fixUrlProtocolListener->onSubmit($event);
46-
$data['link'] = $event->getData();
47-
$event->setData($data);
4844

4945
return;
5046
}
5147

52-
if (!$this->hasAuthority($protocol) && preg_match('~^(?:[/.]|[\w+.-]+:|[^:/?@#]++@)~', $dataLink)) {
48+
if (!$this->hasAuthority($protocol) && preg_match('~^(?:[/.]|[\w+.-]+:|[^:/?@#]++@)~', $data)) {
5349
return;
5450
}
5551

@@ -61,9 +57,8 @@ public function onSubmit(FormEvent $event): void
6157
$regExp = '~^[\w+.-]+:~'; // allowing emails for non-http/https/file
6258
}
6359

64-
if (!preg_match($regExp, $dataLink)) {
65-
$data['link'] = $this->defaultProtocol . $schemaSeparator . $dataLink;
66-
$event->setData($data);
60+
if (!preg_match($regExp, $data)) {
61+
$event->setData($this->defaultProtocol . $schemaSeparator . $data);
6762
}
6863
}
6964

src/lib/Form/Type/FieldType/UrlFieldType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5353
'default_protocol' => null,
5454
]
5555
)
56-
->addEventSubscriber(new FixUrlProtocolListener())
5756
->add(
5857
'text',
5958
TextType::class,
@@ -63,6 +62,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6362
]
6463
)
6564
->addModelTransformer(new FieldValueTransformer($this->fieldTypeService->getFieldType('ezurl')));
65+
66+
$builder->get('link')->addEventSubscriber(new FixUrlProtocolListener());
6667
}
6768

6869
public function configureOptions(OptionsResolver $resolver)

tests/lib/Form/EventSubscriber/FixUrlProtocolListenerTest.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ final class FixUrlProtocolListenerTest extends TestCase
2828
/**
2929
* @dataProvider provideUrlCases
3030
*
31-
* @param array<string, string>|null $inputData
32-
* @param array<string, string>|null $expectedData
31+
* @param string|null $inputData
32+
* @param string|null $expectedData
3333
* @param string $defaultProtocol
3434
*/
35-
public function testUrlProtocolHandling(?array $inputData, ?array $expectedData, ?string $defaultProtocol = 'http'): void
35+
public function testUrlProtocolHandling(?string $inputData, ?string $expectedData, ?string $defaultProtocol = 'http'): void
3636
{
3737
$form = $this->createMock(FormInterface::class);
3838
$listener = new FixUrlProtocolListener($defaultProtocol);
@@ -46,58 +46,54 @@ public function testUrlProtocolHandling(?array $inputData, ?array $expectedData,
4646

4747
/**
4848
* @return iterable<string, array{
49-
* 0: array<string, string>|null,
50-
* 1: array<string, string>|null
49+
* 0: string|null,
50+
* 1: string|null
5151
* }>
5252
*/
5353
public static function provideUrlCases(): iterable
5454
{
5555
return [
5656
'adds http when protocol missing' => [
57-
['link' => self::DOMAIN],
58-
['link' => self::URL_HTTP],
57+
self::DOMAIN,
58+
self::URL_HTTP,
5959
],
6060
'does not modify https url' => [
61-
['link' => self::URL_HTTPS],
62-
['link' => self::URL_HTTPS],
61+
self::URL_HTTPS,
62+
self::URL_HTTPS,
6363
],
6464
'does not modify http url' => [
65-
['link' => self::URL_HTTP],
66-
['link' => self::URL_HTTP],
65+
self::URL_HTTP,
66+
self::URL_HTTP,
6767
],
6868
'keep relative url with leading / intact' => [
69-
['link' => self::URL_RELATIVE],
70-
['link' => self::URL_RELATIVE],
69+
self::URL_RELATIVE,
70+
self::URL_RELATIVE,
7171
],
7272
'keeps ftp intact' => [
73-
['link' => self::URL_SFTP],
74-
['link' => self::URL_SFTP],
73+
self::URL_SFTP,
74+
self::URL_SFTP,
7575
],
7676
'keeps tel intact' => [
77-
['link' => self::URL_TEL],
78-
['link' => self::URL_TEL],
77+
self::URL_TEL,
78+
self::URL_TEL,
7979
],
8080
'adds default tel' => [
81-
['link' => self::TEL],
82-
['link' => self::URL_TEL],
81+
self::TEL,
82+
self::URL_TEL,
8383
'tel',
8484
],
8585
'keeps mailto intact' => [
86-
['link' => self::URL_MAILTO],
87-
['link' => self::URL_MAILTO],
86+
self::URL_MAILTO,
87+
self::URL_MAILTO,
8888
],
8989
'adds default mailto' => [
90-
['link' => self::MAIL],
91-
['link' => self::URL_MAILTO],
90+
self::MAIL,
91+
self::URL_MAILTO,
9292
'mailto',
9393
],
9494
'does nothing when link is empty string' => [
95-
['link' => ''],
96-
['link' => ''],
97-
],
98-
'does nothing when link key is missing' => [
99-
[],
100-
[],
95+
'',
96+
'',
10197
],
10298
'does nothing when data is null' => [
10399
null,

0 commit comments

Comments
 (0)