There are certain cases when built-in field types are not enough. Sylius Grids allows to define new types with ease!
All you need to do is create your own class implementing FieldTypeInterface and register it as a service.
<?php
namespace App\Grid\FieldType;
use Sylius\Component\Grid\Definition\Field;
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CustomType implements FieldTypeInterface
{
public function render(Field $field, $data, array $options = [])
{
// Your rendering logic... Use Twig, PHP or even external api...
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'dynamic' => false
])
->setAllowedTypes([
'dynamic' => ['boolean']
])
;
}
public function getName(): string
{
return 'custom';
}
}
That is all. Now register your new field type as a service.
app.grid_field.custom:
class: App\Grid\FieldType\CustomType
tags:
- { name: sylius.grid_field, type: custom }