From c046fb7ad1f56c820d80b259d8f6ca81500ff2b8 Mon Sep 17 00:00:00 2001 From: Kamil Grygierzec Date: Mon, 6 May 2024 14:41:27 +0200 Subject: [PATCH] Use extension instead of changing the existing forms in ProductBundle --- .../ProductAssociationsTypeExtension.php | 42 +++++++++++++++++++ .../Resources/config/services/form.xml | 4 ++ .../config/services/product/form.xml | 2 +- .../Form/Type/ProductAssociationsType.php | 7 ++-- .../ProductBundle/Form/Type/ProductType.php | 3 ++ .../Resources/config/services/form.xml | 22 +++++----- 6 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 src/Sylius/Bundle/AdminBundle/Form/Extension/ProductAssociationsTypeExtension.php diff --git a/src/Sylius/Bundle/AdminBundle/Form/Extension/ProductAssociationsTypeExtension.php b/src/Sylius/Bundle/AdminBundle/Form/Extension/ProductAssociationsTypeExtension.php new file mode 100644 index 00000000000..8c0647e7ec4 --- /dev/null +++ b/src/Sylius/Bundle/AdminBundle/Form/Extension/ProductAssociationsTypeExtension.php @@ -0,0 +1,42 @@ +setDefaults([ + 'entry_type' => ProductAutocompleteChoiceType::class, + 'entry_options' => fn (ProductAssociationTypeInterface $productAssociationType) => [ + 'label' => $productAssociationType->getName(), + 'multiple' => true, + ], + ]); + } + + /** + * @return iterable + */ + public static function getExtendedTypes(): iterable + { + return [ProductAssociationsType::class]; + } +} diff --git a/src/Sylius/Bundle/AdminBundle/Resources/config/services/form.xml b/src/Sylius/Bundle/AdminBundle/Resources/config/services/form.xml index 4ea8b8e196f..6611ce6428a 100644 --- a/src/Sylius/Bundle/AdminBundle/Resources/config/services/form.xml +++ b/src/Sylius/Bundle/AdminBundle/Resources/config/services/form.xml @@ -60,6 +60,10 @@ + + + + %sylius.model.product_variant.class% diff --git a/src/Sylius/Bundle/AdminBundle/Resources/config/services/product/form.xml b/src/Sylius/Bundle/AdminBundle/Resources/config/services/product/form.xml index f8c0ca11b35..8bfd193bcc5 100644 --- a/src/Sylius/Bundle/AdminBundle/Resources/config/services/product/form.xml +++ b/src/Sylius/Bundle/AdminBundle/Resources/config/services/product/form.xml @@ -13,7 +13,7 @@ - + diff --git a/src/Sylius/Bundle/ProductBundle/Form/Type/ProductAssociationsType.php b/src/Sylius/Bundle/ProductBundle/Form/Type/ProductAssociationsType.php index 292b80aec9a..fa2e6cd459a 100644 --- a/src/Sylius/Bundle/ProductBundle/Form/Type/ProductAssociationsType.php +++ b/src/Sylius/Bundle/ProductBundle/Form/Type/ProductAssociationsType.php @@ -14,7 +14,6 @@ namespace Sylius\Bundle\ProductBundle\Form\Type; use Doctrine\Common\Collections\Collection; -use Sylius\Bundle\AdminBundle\Form\Type\ProductAutocompleteChoiceType; use Sylius\Bundle\ResourceBundle\Form\Type\FixedCollectionType; use Sylius\Component\Product\Model\ProductAssociationInterface; use Sylius\Component\Product\Model\ProductAssociationTypeInterface; @@ -22,6 +21,7 @@ use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -46,11 +46,10 @@ public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'entries' => $this->productAssociationTypeRepository->findAll(), + 'entry_type' => TextType::class, 'entry_name' => fn (ProductAssociationTypeInterface $productAssociationType) => $productAssociationType->getCode(), - 'entry_type' => ProductAutocompleteChoiceType::class, 'entry_options' => fn (ProductAssociationTypeInterface $productAssociationType) => [ 'label' => $productAssociationType->getName(), - 'multiple' => true, ], ]); } @@ -62,6 +61,6 @@ public function getParent(): string public function getBlockPrefix(): string { - return 'sylius_admin_product_associations'; + return 'sylius_product_associations'; } } diff --git a/src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php b/src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php index 5ef820f2220..34f95a02810 100644 --- a/src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php +++ b/src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php @@ -68,6 +68,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'by_reference' => false, 'label' => false, ]) + ->add('associations', ProductAssociationsType::class, [ + 'label' => false, + ]) ; } diff --git a/src/Sylius/Bundle/ProductBundle/Resources/config/services/form.xml b/src/Sylius/Bundle/ProductBundle/Resources/config/services/form.xml index 422006a2c1a..48deb26cf6d 100644 --- a/src/Sylius/Bundle/ProductBundle/Resources/config/services/form.xml +++ b/src/Sylius/Bundle/ProductBundle/Resources/config/services/form.xml @@ -89,6 +89,12 @@ + + + + + + %sylius.model.product_attribute.class% %sylius.form.type.product_attribute.validation_groups% @@ -199,20 +205,14 @@ - - - - - - - - - - - + + + + +