Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChoiceFilter with default options #749

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doctrine/mongodb-odm": "^2.1",
"doctrine/mongodb-odm-bundle": "^4.3",
"doctrine/persistence": "^2.0",
"sonata-project/admin-bundle": "^4.0",
"sonata-project/admin-bundle": "^4.8",
"sonata-project/form-extensions": "^1.7.1",
"symfony/config": "^4.4 || ^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0",
Expand Down
10 changes: 7 additions & 3 deletions src/Filter/ChoiceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\AdminBundle\Filter\Model\FilterData;
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQueryInterface;

final class ChoiceFilter extends Filter
{
public function getDefaultOptions(): array
{
return [];
return [
'operator_type' => EqualOperatorType::class,
'operator_options' => [],
];
}

public function getRenderSettings(): array
{
return [DefaultType::class, [
'operator_type' => ChoiceType::class,
'operator_type' => $this->getOption('operator_type'),
'operator_options' => $this->getOption('operator_options'),
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'label' => $this->getLabel(),
Expand Down
14 changes: 13 additions & 1 deletion tests/App/Admin/CategoryAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineMongoDBAdminBundle\Filter\ChoiceFilter;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

/**
Expand Down Expand Up @@ -52,7 +54,17 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('id')
->add('name');
->add('name')
->add('type', ChoiceFilter::class, [
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => [
'Type 1' => 'type1',
'Type 2' => 'type2',
],
'expanded' => false,
],
]);
}

protected function configureShowFields(ShowMapper $show): void
Expand Down
15 changes: 14 additions & 1 deletion tests/App/Document/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ class Category
*/
private $name;

public function __construct(string $id = '', string $name = '')
/**
* @ODM\Field(type="string")
*
* @var string
*/
private $type;

public function __construct(string $id = '', string $name = '', string $type = '')
{
$this->id = $id;
$this->name = $name;
$this->type = $type;
}

public function __toString(): string
Expand All @@ -62,4 +70,9 @@ public function setName(string $name): void
{
$this->name = $name;
}

public function getType(): string
{
return $this->type;
}
}
30 changes: 5 additions & 25 deletions tests/App/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,35 @@
->set(CategoryAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'doctrine_mongodb',
'model_class' => Category::class,
'label' => 'Category',
])
->args([
'',
Category::class,
null,
])

->set(BookAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'doctrine_mongodb',
'model_class' => Book::class,
'label' => 'Book',
])
->args([
'',
Book::class,
null,
])

->set(AuthorAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'doctrine_mongodb',
'model_class' => Author::class,
'label' => 'Author',
])
->args([
'',
Author::class,
null,
])

->set(AddressAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'doctrine_mongodb',
'model_class' => Address::class,
'label' => 'Address',
])
->args([
'',
Address::class,
null,
])

->set(PhoneNumberAdmin::class)
->tag('sonata.admin', [
'manager_type' => 'doctrine_mongodb',
'model_class' => PhoneNumber::class,
'label' => 'PhoneNumber',
])
->args([
'',
PhoneNumber::class,
null,
]);
};