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 d9c6a7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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)

Check failure on line 14 in src/Form/TierPriceMapper.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.11.2, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Method Brille24\SyliusTierPricePlugin\Form\TierPriceMapper::mapDataToForms() has no return typehint specified.

Check failure on line 14 in src/Form/TierPriceMapper.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.11.2, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Method Brille24\SyliusTierPricePlugin\Form\TierPriceMapper::mapDataToForms() has no return typehint specified.
{
$this->inner->mapDataToForms($viewData, $forms);
}

public function mapFormsToData(Traversable $forms, &$viewData)

Check failure on line 19 in src/Form/TierPriceMapper.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.11.2, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Method Brille24\SyliusTierPricePlugin\Form\TierPriceMapper::mapFormsToData() has no return typehint specified.

Check failure on line 19 in src/Form/TierPriceMapper.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.11.2, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Method Brille24\SyliusTierPricePlugin\Form\TierPriceMapper::mapFormsToData() has no return typehint specified.
{
$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
8 changes: 7 additions & 1 deletion src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit d9c6a7e

Please sign in to comment.