Filter - Create Custom Filter
1. We need to create 2 new files:
- FormType for processing the Input Data
- And a FilterConditionProcessorInterface, which checks if a cart fulfills the condition.
namespace App\CoreShop\Form\Type\Condition;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
final class MyFilterCondition extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('myData', IntegerType::class, [
'constraints' => [
new NotBlank(['groups' => ['coreshop']]),
new Type(['type' => 'numeric', 'groups' => ['coreshop']]),
],
])
;
}
}
namespace App\CoreShop\Filter;
use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CarrierInterface;
class MyFilterCondition extends FilterConditionProcessorInterface
{
public function prepareValuesForRendering(FilterConditionInterface $condition, FilterInterface $filter, ListingInterface $list, $currentFilter)
{
//Prepare values for rendering HTML
}
public function addCondition(FilterConditionInterface $condition, FilterInterface $filter, ListingInterface $list, $currentFilter, ParameterBag $parameterBag, $isPrecondition = false)
{
//Add Condition to Listing
return $currentFilter;
}
}
2. Register MyFilterCondition as service with tag coreshop.filter.condition_type
, type and form. We are also adding the autoconfigure: false
to prevent autoconfiguration of the service. (This is only needed if you use autoconfiguration in your project)
App\CoreShop\Filter\MyFilterCondition:
autoconfigure: false
tags:
- { name: coreshop.filter.condition_type, type: app-my-filter, form-type: App\Filter\Form\Type\Condition\MyFilterCondition}