Skip to content

Commit

Permalink
Fixed errors with EntityFilterType
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Apr 3, 2023
1 parent aa4159c commit 1ffe011
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/Filter/Type/EntityFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EntityFilterType extends AbstractFilterType
public function apply(ProxyQueryInterface $query, FilterData $data, FilterInterface $filter, array $options): void
{
$operator = $data->getOperator() ?? Operator::EQUALS;
$value = (array) $data->getValue();
$value = $data->getValue();

try {
$expressionBuilderMethodName = $this->getExpressionBuilderMethodName($operator);
Expand Down
4 changes: 1 addition & 3 deletions src/Filter/FiltrationData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
class FiltrationData
{
/**
* @var array<FilterData>
* @param array<FilterData> $filters
*/
// private array $filters = [];

public function __construct(private array $filters = [])
{
foreach ($filters as $filter) {
Expand Down
35 changes: 34 additions & 1 deletion src/Filter/Form/Type/FiltrationDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use Kreyu\Bundle\DataTableBundle\Filter\FiltrationData;
use Kreyu\Bundle\DataTableBundle\Filter\Type\SearchFilterTypeInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FiltrationDataType extends AbstractType
class FiltrationDataType extends AbstractType implements DataMapperInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand All @@ -36,6 +37,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'empty_data' => new FilterData,
]));
}

$builder->setDataMapper($this);
}

public function finishView(FormView $view, FormInterface $form, array $options): void
Expand Down Expand Up @@ -103,4 +106,34 @@ public function configureOptions(OptionsResolver $resolver): void
->setAllowedTypes('filters', FilterInterface::class . '[]')
;
}

public function mapDataToForms(mixed $viewData, \Traversable $forms): void
{
if (null === $viewData) {
return;
}

$forms = iterator_to_array($forms);

foreach ($forms as $name => $form) {
$filterData = $viewData->getFilterData($name);

if ($filterData && $filterData->hasValue()) {
$form->setData($filterData);
}
}
}

public function mapFormsToData(\Traversable $forms, mixed &$viewData): void
{
if (null === $viewData) {
$viewData = new FiltrationData();
}

$forms = iterator_to_array($forms);

foreach ($forms as $name => $form) {
$viewData->setFilterData($name, $form->getData());
}
}
}

0 comments on commit 1ffe011

Please sign in to comment.