Skip to content

Commit

Permalink
Add default form options
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 10, 2024
1 parent cac7acd commit 84dada8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
24 changes: 22 additions & 2 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,30 @@ private function buildForm(): FormInterface
}

foreach ($this->getFilters() as $filter) {
// NEXT_MAJOR: Keep the if part.
if (method_exists($filter, 'getFormOptions')) {
$type = FilterDataType::class;
$options = $filter->getFormOptions();

// NEXT_MAJOR: Keep the if part.
if (method_exists($filter, 'getLabelTranslationParameters')) {
$labelTranslationParameters = $filter->getLabelTranslationParameters();
} else {
@trigger_error(
'Not implementing "getLabelTranslationParameters()" is deprecated since sonata-project/admin-bundle 4.30'
.' and will throw an error in 5.0.',
\E_USER_DEPRECATED
);

$labelTranslationParameters = $filter->getOption('label_translation_parameters');
}

$defaultFormOptions = [
'label' => $filter->getLabel(),
'label_translation_parameters' => $labelTranslationParameters,
'translation_domain' => $filter->getTranslationDomain(),
'field_type' => $filter->getFieldType(),
'field_options' => $filter->getFieldOptions(),
];
$options = array_merge($defaultFormOptions, $filter->getFormOptions());
} else {
@trigger_error(
'Not implementing "getFormOptions()" is deprecated since sonata-project/admin-bundle 4.15'
Expand Down
29 changes: 23 additions & 6 deletions tests/Datagrid/DatagridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Sonata\AdminBundle\Filter\FilterInterface;
use Sonata\AdminBundle\Form\Type\Filter\FilterDataType;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -318,7 +319,7 @@ public function testEmptyResults(): void
public function testBuildPager(): void
{
$filter1 = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter1->expects(static::once())
->method('getName')
Expand All @@ -329,14 +330,17 @@ public function testBuildPager(): void
$filter1
->method('isActive')
->willReturn(false);
$filter1
->method('getFieldType')
->willReturn(TextType::class);
$filter1
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz1']]);

$this->datagrid->addFilter($filter1);

$filter2 = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter2->expects(static::once())
->method('getName')
Expand All @@ -347,6 +351,9 @@ public function testBuildPager(): void
$filter2
->method('isActive')
->willReturn(true);
$filter2
->method('getFieldType')
->willReturn(TextType::class);
$filter2
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz2']]);
Expand Down Expand Up @@ -374,12 +381,13 @@ public function testApplyFilter(?string $type, ?string $value, int $applyCallNum
$this->datagrid->setValue('fooFormName', $type, $value);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())->method('getName')->willReturn('foo');
$filter->method('getFormName')->willReturn('fooFormName');
$filter->method('isActive')->willReturn(false);
$filter->method('getFormOptions')->willReturn(['operator_options' => ['help' => 'baz2']]);
$filter->method('getFieldType')->willReturn(TextType::class);
$filter->expects(static::exactly($applyCallNumber))->method('apply');

$this->datagrid->addFilter($filter);
Expand Down Expand Up @@ -430,7 +438,7 @@ public function applyFilterDataProvider(): iterable
public function testBuildPagerWithException(): void
{
$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -443,6 +451,9 @@ public function testBuildPagerWithException(): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down Expand Up @@ -475,7 +486,7 @@ public function testBuildPagerWithSortBy(): void
$this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, [DatagridInterface::SORT_BY => $sortBy]);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -486,6 +497,9 @@ public function testBuildPagerWithSortBy(): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down Expand Up @@ -526,7 +540,7 @@ public function testBuildPagerWithPage(int $page, int|array $perPage): void
$this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, [DatagridInterface::SORT_BY => $sortBy, DatagridInterface::PAGE => $page, DatagridInterface::PER_PAGE => $perPage]);

$filter = $this->getMockBuilder(FilterInterface::class)
->addMethods(['getFormOptions'])
->addMethods(['getFormOptions', 'getLabelTranslationParameters'])
->getMockForAbstractClass();
$filter->expects(static::once())
->method('getName')
Expand All @@ -537,6 +551,9 @@ public function testBuildPagerWithPage(int $page, int|array $perPage): void
$filter
->method('isActive')
->willReturn(false);
$filter
->method('getFieldType')
->willReturn(TextType::class);
$filter
->method('getFormOptions')
->willReturn(['operator_options' => ['help' => 'baz']]);
Expand Down

0 comments on commit 84dada8

Please sign in to comment.