-
-
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.
WIP - Adding sample variant config to the admin UI
- Loading branch information
Showing
25 changed files
with
547 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
<?php | ||
|
||
use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer; | ||
use SlevomatCodingStandard\Sniffs\Commenting\InlineDocCommentDeclarationSniff; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symplify\EasyCodingStandard\ValueObject\Option; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->import('vendor/sylius-labs/coding-standard/ecs.php'); | ||
|
||
$containerConfigurator->parameters()->set(Option::SKIP, [ | ||
$parameters = $containerConfigurator->parameters(); | ||
$parameters->set(Option::PATHS, [__DIR__ . '/src']); | ||
$parameters->set(Option::SKIP, [ | ||
VisibilityRequiredFixer::class => ['*Spec.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 |
---|---|---|
@@ -1,3 +1,32 @@ | ||
parameters: | ||
ignoreErrors: [] | ||
ignoreErrors: | ||
- | ||
message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" | ||
count: 1 | ||
path: src/BabDevSyliusProductSamplesPlugin.php | ||
|
||
- | ||
message: "#^Cannot call method getData\\(\\) on Symfony\\\\Component\\\\Form\\\\FormInterface\\|null\\.$#" | ||
count: 2 | ||
path: src/Form/Type/SampleProductVariantType.php | ||
|
||
- | ||
message: "#^Cannot call method getParent\\(\\) on Symfony\\\\Component\\\\Form\\\\FormInterface\\|null\\.$#" | ||
count: 1 | ||
path: src/Form/Type/SampleProductVariantType.php | ||
|
||
- | ||
message: "#^Cannot call method getSamplesActive\\(\\) on Sylius\\\\Component\\\\Product\\\\Model\\\\ProductInterface\\|null\\.$#" | ||
count: 1 | ||
path: src/Form/Type/SampleProductVariantType.php | ||
|
||
- | ||
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#" | ||
count: 1 | ||
path: src/Form/Type/SampleProductVariantType.php | ||
|
||
- | ||
message: "#^Parameter \\#1 \\$sampleOf of method BabDev\\\\SyliusProductSamplesPlugin\\\\Model\\\\ProductVariantInterface\\:\\:setSampleOf\\(\\) expects Sylius\\\\Component\\\\Product\\\\Model\\\\ProductVariantInterface\\|null, mixed given\\.$#" | ||
count: 1 | ||
path: src/Form/Type/SampleProductVariantType.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
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,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<files psalm-version="4.20.0@f82a70e7edfc6cf2705e9374c8a0b6a974a779ed"> | ||
<file src="src/BabDevSyliusProductSamplesPlugin.php"> | ||
<MixedInferredReturnType occurrences="1"> | ||
<code>?ExtensionInterface</code> | ||
</MixedInferredReturnType> | ||
<MixedReturnStatement occurrences="1"> | ||
<code>$this->extension ?: null</code> | ||
</MixedReturnStatement> | ||
</file> | ||
<file src="src/Form/Type/SampleProductVariantType.php"> | ||
<MixedArgument occurrences="1"> | ||
<code>$variantForm->getData()</code> | ||
</MixedArgument> | ||
<MixedAssignment occurrences="1"> | ||
<code>$productVariant</code> | ||
</MixedAssignment> | ||
<PossiblyNullReference occurrences="2"> | ||
<code>getParent</code> | ||
<code>getSamplesActive</code> | ||
</PossiblyNullReference> | ||
<UndefinedInterfaceMethod occurrences="1"> | ||
<code>getSamplesActive</code> | ||
</UndefinedInterfaceMethod> | ||
</file> | ||
</files> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\BabDev\SyliusProductSamplesPlugin\Menu; | ||
|
||
use Knp\Menu\ItemInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\AdminBundle\Event\ProductVariantMenuBuilderEvent; | ||
|
||
final class ProductVariantFormMenuBuilderSpec extends ObjectBehavior | ||
{ | ||
public function it_adds_the_menu_item_to_the_product_form_menu(ProductVariantMenuBuilderEvent $event, ItemInterface $menu): void | ||
{ | ||
$event->getMenu()->willReturn($menu); | ||
$menu->addChild('product_samples')->willReturn($menu); | ||
$menu->setAttribute('template', '@BabDevSyliusProductSamplesPlugin/Admin/ProductVariant/Tab/_product_samples.html.twig')->willReturn($menu); | ||
$menu->setLabel('babdev_sylius_product_samples.ui.product_samples')->willReturn($menu); | ||
|
||
$this->addProductSamplesMenu($event); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\EventListener; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductInterface; | ||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
|
||
final class ProductListener | ||
{ | ||
public function ensureSampleVariantsHaveCodes(ResourceControllerEvent $event): void | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $event->getSubject(); | ||
|
||
foreach ($product->getVariants() as $variant) { | ||
if (!$variant instanceof ProductVariantInterface) { | ||
continue; | ||
} | ||
|
||
if (null === $sample = $variant->getSample()) { | ||
continue; | ||
} | ||
|
||
if (null === $sample->getCode()) { | ||
$sample->setCode(sprintf('SAMPLE-%s', $variant->getCode() ?? '')); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Form\Extension; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Form\Type\SampleProductVariantType; | ||
use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantType; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
final class ProductVariantTypeExtension extends AbstractTypeExtension | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('sample', SampleProductVariantType::class) | ||
; | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [ProductVariantType::class]; | ||
} | ||
} |
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Form\Type; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use Sylius\Bundle\CoreBundle\Form\Type\ChannelCollectionType; | ||
use Sylius\Bundle\CoreBundle\Form\Type\Product\ChannelPricingType; | ||
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; | ||
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingCategoryChoiceType; | ||
use Sylius\Bundle\TaxationBundle\Form\Type\TaxCategoryChoiceType; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
final class SampleProductVariantType extends AbstractResourceType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('shippingCategory', ShippingCategoryChoiceType::class, [ | ||
'required' => false, | ||
'placeholder' => 'sylius.ui.no_requirement', | ||
'label' => 'sylius.form.product_variant.shipping_category', | ||
]) | ||
->add('width', NumberType::class, [ | ||
'required' => false, | ||
'label' => 'sylius.form.variant.width', | ||
'invalid_message' => 'sylius.product_variant.width.invalid', | ||
]) | ||
->add('height', NumberType::class, [ | ||
'required' => false, | ||
'label' => 'sylius.form.variant.height', | ||
'invalid_message' => 'sylius.product_variant.height.invalid', | ||
]) | ||
->add('depth', NumberType::class, [ | ||
'required' => false, | ||
'label' => 'sylius.form.variant.depth', | ||
'invalid_message' => 'sylius.product_variant.depth.invalid', | ||
]) | ||
->add('weight', NumberType::class, [ | ||
'required' => false, | ||
'label' => 'sylius.form.variant.weight', | ||
'invalid_message' => 'sylius.product_variant.weight.invalid', | ||
]) | ||
->add('taxCategory', TaxCategoryChoiceType::class, [ | ||
'required' => false, | ||
'placeholder' => '---', | ||
'label' => 'sylius.form.product_variant.tax_category', | ||
]) | ||
; | ||
|
||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { | ||
$productVariant = $event->getData(); | ||
|
||
$event->getForm()->add('channelPricings', ChannelCollectionType::class, [ | ||
'entry_type' => ChannelPricingType::class, | ||
'entry_options' => static fn (ChannelInterface $channel): array => [ | ||
'channel' => $channel, | ||
'product_variant' => $productVariant, | ||
'required' => false, | ||
], | ||
'label' => 'sylius.form.variant.price', | ||
]); | ||
}); | ||
|
||
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event): void { | ||
/** @var ProductVariantInterface $sampleVariant */ | ||
$sampleVariant = $event->getData(); | ||
|
||
$variantForm = $event->getForm()->getParent(); | ||
|
||
if (null !== $productForm = $variantForm->getParent()) { | ||
if (!$productForm->get('samplesActive')->getData() && null === $sampleVariant->getId()) { | ||
$event->setData(null); | ||
|
||
return; | ||
} | ||
} else { | ||
if (!$sampleVariant->getProduct()->getSamplesActive() && null === $sampleVariant->getId()) { | ||
$event->setData(null); | ||
|
||
return; | ||
} | ||
} | ||
|
||
if (null !== $sampleVariant->getId()) { | ||
return; | ||
} | ||
|
||
/** @var ProductVariantInterface $actualVariant */ | ||
$actualVariant = $variantForm->getData(); | ||
$actualVariant->setSample($sampleVariant); | ||
|
||
$sampleVariant->setSampleOf($variantForm->getData()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.