Skip to content

Commit

Permalink
Custom range attribute type
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 15, 2024
1 parent 997f7b9 commit a43a30b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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" }
32 changes: 32 additions & 0 deletions src/Product/AttributeType/RangeAttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Product\AttributeType;

use Sylius\Component\Attribute\AttributeType\AttributeTypeInterface;
use Sylius\Component\Attribute\Model\AttributeValueInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class RangeAttributeType implements AttributeTypeInterface
{
public const TYPE = 'range';

public function getStorageType(): string
{
return AttributeValueInterface::STORAGE_JSON;
}

public function getType(): string
{
return self::TYPE;
}

public function validate(
AttributeValueInterface $attributeValue,
ExecutionContextInterface $context,
array $configuration,
): void {
return;
}
}
40 changes: 40 additions & 0 deletions src/Product/Form/Type/RangeAttributeTypeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Product\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class RangeAttributeTypeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->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')
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ attribute.value.min }} - {{ attribute.value.max }} {{ attribute.value.unit }}

0 comments on commit a43a30b

Please sign in to comment.