Add data transformation interface for filters

Introduced a new DataTransformerFilterInterface that allows transforming filter's form data before it is processed. Updated the FilterType file to add a view transformer if the filter implements this new interface. This new transformation process caters to transforming existing data in saved exports and replacing it with default values.
This commit is contained in:
2024-06-14 13:58:48 +02:00
parent 5ca222b501
commit 90b615c5b2
3 changed files with 39 additions and 0 deletions

View File

@@ -11,8 +11,10 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form\Type\Export;
use Chill\MainBundle\Export\DataTransformerFilterInterface;
use Chill\MainBundle\Export\FilterInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -43,6 +45,15 @@ class FilterType extends AbstractType
]);
$filter->buildForm($filterFormBuilder);
if ($filter instanceof DataTransformerFilterInterface) {
$filterFormBuilder->addViewTransformer(new CallbackTransformer(
fn (?array $data) => $data,
function (?array $data) use ($filter) {
return $filter->transformData($data);
},
));
}
$builder->add($filterFormBuilder);
}