-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from stefandoorn/split-callable
Move form event listeners to separate subscribers
- Loading branch information
Showing
6 changed files
with
149 additions
and
66 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/Form/EventSubscriber/AppendDataToStreetFieldEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StefanDoorn\SyliusStreetNumberPlugin\Form\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
final class AppendDataToStreetFieldEventSubscriber implements EventSubscriberInterface | ||
{ | ||
private const FIELD_STREET = 'street'; | ||
|
||
/** @var string */ | ||
private $field; | ||
|
||
public function __construct(string $field) | ||
{ | ||
$this->field = $field; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
FormEvents::PRE_SUBMIT => 'onPreSubmit', | ||
]; | ||
} | ||
|
||
public function onPreSubmit(FormEvent $event): void | ||
{ | ||
$data = $event->getData(); | ||
|
||
$data[self::FIELD_STREET] = sprintf( | ||
'%s %s', | ||
$data[self::FIELD_STREET], | ||
$data[$this->field] | ||
); | ||
|
||
$event->setData($data); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Form/EventSubscriber/SetStreetWithoutNumberAndAdditionEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StefanDoorn\SyliusStreetNumberPlugin\Form\EventSubscriber; | ||
|
||
use StefanDoorn\SyliusStreetNumberPlugin\Entity\Interfaces\AddressInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
final class SetStreetWithoutNumberAndAdditionEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
FormEvents::PRE_SET_DATA => 'onPreSetData', | ||
]; | ||
} | ||
|
||
public function onPreSetData(FormEvent $event): void | ||
{ | ||
/** @var AddressInterface $data */ | ||
$data = $event->getData(); | ||
|
||
if (!$data instanceof AddressInterface) { | ||
return; | ||
} | ||
|
||
if (null === $data->getId()) { | ||
return; // Only adjust the data from already saved entities (we add it below only on PRE SUBMIT) | ||
} | ||
|
||
$data->setStreet($data->getStreetWithoutNumberAndAddition()); | ||
$event->setData($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters