Skip to content

Commit

Permalink
Empty tierprices should remove the form row
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Apr 24, 2024
1 parent 9e30179 commit a70781c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Form/TierPriceMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Brille24\SyliusTierPricePlugin\Form;

use Symfony\Component\Form\DataMapperInterface;
use Traversable;

class TierPriceMapper implements DataMapperInterface
{
public function __construct(private DataMapperInterface $inner) {}

public function mapDataToForms($viewData, Traversable $forms)
{
$this->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);
}
}
}
5 changes: 5 additions & 0 deletions src/Form/TierPriceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -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
Expand Down

0 comments on commit a70781c

Please sign in to comment.