From d9c6a7ed9b370829e38da7e8061124cc3bfa6aca Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:23:34 +0200 Subject: [PATCH] Empty tierprices should remove the form row --- src/Form/TierPriceMapper.php | 31 +++++++++++++++++++++++++++++++ src/Form/TierPriceType.php | 5 +++++ src/Resources/config/services.php | 8 +++++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Form/TierPriceMapper.php diff --git a/src/Form/TierPriceMapper.php b/src/Form/TierPriceMapper.php new file mode 100644 index 0000000..fe7a052 --- /dev/null +++ b/src/Form/TierPriceMapper.php @@ -0,0 +1,31 @@ +inner->mapDataToForms($viewData, $forms); + } + + public function mapFormsToData(Traversable $forms, &$viewData) + { + $formData = iterator_to_array($forms); + if ($formData['qty']->getData() === null + && $formData['price']->getData() === null + && $formData['channel']->getData() === null + ) { + $viewData = null; + } else { + $this->inner->mapFormsToData($forms, $viewData); + } + } +} diff --git a/src/Form/TierPriceType.php b/src/Form/TierPriceType.php index bf23bc2..cbc6d1f 100644 --- a/src/Form/TierPriceType.php +++ b/src/Form/TierPriceType.php @@ -25,6 +25,9 @@ class TierPriceType extends AbstractType { + public function __construct(private TierPriceMapper $dataMapper) { + } + public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('qty', NumberType::class, [ @@ -45,6 +48,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->add('channel', ChannelChoiceType::class, [ 'attr' => ['style' => 'display:none'], ]); + + $builder->setDataMapper($this->dataMapper); } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 47b6e69..be01aa2 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -17,6 +17,7 @@ use Brille24\SyliusTierPricePlugin\Factory\TierPriceFactoryInterface; use Brille24\SyliusTierPricePlugin\Fixtures\TierPriceFixture; use Brille24\SyliusTierPricePlugin\Form\Extension\ProductVariantTypeExtension; +use Brille24\SyliusTierPricePlugin\Form\TierPriceMapper; use Brille24\SyliusTierPricePlugin\Form\TierPriceType; use Brille24\SyliusTierPricePlugin\Repository\TierPriceRepositoryInterface; use Brille24\SyliusTierPricePlugin\Services\OrderPricesRecalculator; @@ -27,6 +28,7 @@ use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantType; use Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; +use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $containerConfigurator): void { @@ -66,7 +68,11 @@ ->tag('sylius_fixtures.fixture') ; - $services->set(TierPriceType::class); + $services->set(TierPriceType::class) + ->args([TierPriceMapper::class]); + + $services->set(TierPriceMapper::class) + ->args([DataMapper::class]); $services->set(ProductVariantTypeExtension::class) ->tag('form.type_extension', ['extended_type' => ProductVariantType::class, 'priority' => -5])