From a43a30b75187be5ebfd44f9130437dffbd297e36 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 15 May 2024 16:34:36 +0200 Subject: [PATCH] Custom range attribute type --- config/services.yaml | 6 ++- .../AttributeType/RangeAttributeType.php | 32 +++++++++++++++ .../Form/Type/RangeAttributeTypeType.php | 40 +++++++++++++++++++ .../Types/range.html.twig | 1 + 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Product/AttributeType/RangeAttributeType.php create mode 100644 src/Product/Form/Type/RangeAttributeTypeType.php create mode 100644 templates/bundles/SyliusAttributeBundle/Types/range.html.twig diff --git a/config/services.yaml b/config/services.yaml index aff5b92..7a3c600 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -7,7 +7,7 @@ services: # Automatically registers your services as commands, event subscribers, etc. autoconfigure: true - # Allows optimizing the container by removing unused services; this also means + # Allows optimizing the container by remcheoving unused services; this also means # fetching services directly from the container via $container->get() won't work public: false @@ -28,3 +28,7 @@ services: App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments'] + + App\Product\AttributeType\RangeAttributeType: + tags: + - { name: sylius.attribute.type, attribute_type: range, form_type: App\Product\Form\Type\RangeAttributeTypeType, label: "Range" } diff --git a/src/Product/AttributeType/RangeAttributeType.php b/src/Product/AttributeType/RangeAttributeType.php new file mode 100644 index 0000000..820d9b2 --- /dev/null +++ b/src/Product/AttributeType/RangeAttributeType.php @@ -0,0 +1,32 @@ +add('min', NumberType::class, [ + 'label' => 'sylius.ui.min', + ]) + ->add('max', NumberType::class, [ + 'label' => 'sylius.ui.max', + ]) + ->add('unit', TextType::class, [ + 'label' => 'sylius.ui.unit', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver + ->setDefaults([ + 'label' => false, + ]) + ->setRequired('configuration') + ->setDefined('locale_code') + ; + } +} diff --git a/templates/bundles/SyliusAttributeBundle/Types/range.html.twig b/templates/bundles/SyliusAttributeBundle/Types/range.html.twig new file mode 100644 index 0000000..bdc47fb --- /dev/null +++ b/templates/bundles/SyliusAttributeBundle/Types/range.html.twig @@ -0,0 +1 @@ +{{ attribute.value.min }} - {{ attribute.value.max }} {{ attribute.value.unit }}