4

I need to redirect my user to CRUD index where filter "STATUS = ACTIVE" is applied.

I've this:

$url = $this->adminUrlGenerator
            ->setController(Customer::class)

            ->generateUrl();

return $this->redirect($url);

But I can't find a way to add a filter to it. I've tried searching for something like:

->setFilter('Status', 'ACTIVE')

but without any luck. There is nothing in the docs. How to do it?

Tim
  • 75
  • 1
  • 7

1 Answers1

5

EasyAdmin handle filters in your url by adding multiple options to handle each filters case.

  1. value to be compared with
  2. value2 (Example: between value and value2)
  3. comparison for "equal", "less than", "greater than" etc...

Filtering by Status ACTIVE would modify your url with

&filters[Status][comparison]=%3D&filters[Status][value]=ACTIVE

Note that here %3D is = encoded for the url, but using = would work as well.

So when using EA AdminUrlGenerator, you can use ->set to modify options.

You would get:

$url = $this->adminUrlGenerator
            ->setController(Customer::class)
            ->set('filters[Status][value]', 'ACTIVE')
            ->set('filters[Status][comparison]', '=')
            ->generateUrl();
            

I kept the case on Status, but if your property is in lowercase, do it here as well.

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • It works like a charm. Is it possible to apply it to the Dashboard controller, more beautifully than this? MenuItem::linkToUrl('Zawieszone','fa fa-hourglass', $this->adminurlgenerator ->setController(KlientCrudController::class) ->setAction('index') ->set('filters[Status][value]', 'Zawieszone') ->set('filters[Status][comparison]', '=') ->generateUrl()), – Tim Apr 25 '22 at 16:56
  • There is an easyadmin constant for 'index', something like Crud::PAGE_INDEX or something similar which would be a little better. Also, you should not need to use adminUrlGenerator for an easy admin link, don't use linkToUrl (I don't recall the exact method so look into others method to link to an action) – Dylan KAS Apr 25 '22 at 18:43
  • Nice to know, it may depend on what kind of filter your are using ? I tried again with a text filter and it's `=` (or in the url `%3D`) – Dylan KAS May 19 '22 at 11:26