phpsymfonysonata-adminsonatasymfony-sonata

Sonata ModelAutocompleteType result affected by getFilterParameters


I have this form field of a type ModelAutocompleteType that is supposed to show the result filtered by the "search" datagrid field of related admin:

class OperationAdmin extends AbstractAdmin
{
  // ...

  protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
      ->add('business', ModelAutocompleteType::class, [
        'label' => 'Business',
        'property' => 'search'
      ]);
  }

  // ...
}

In this related "business" admin I have few filters defined as:

class BusinessAdmin extends AbstractAdmin
{
  // ...

  protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  {
    $datagridMapper
      ->add('search', CallbackFilter::class, [
        'label' => 'Search',
        'show_filter' => true,
        'advanced_filter' => false,
        'callback' => function($qb, $alias, $field, $value) {
           if (!$value['value']) return;
           // ...

           return true;
        }
      ])
      ->add('state', ChoiceFilter::class, [
        'label' => 'State', 
        'show_filter' => true,
        'advanced_filter' => false,
        'field_options' => ['choices' => Business::STATES],
        'field_type' => 'choice'
      ]);
  }

  // ...
}

Now, if I set the default "state" datagrid field value using getFilterParameters to filter business list by state on initial page load:

  public function getFilterParameters()
  {
    $this->datagridValues = array_merge([
      'state' => ['type' => '', 'value' => 'active']
    ], $this->datagridValues);

    return parent::getFilterParameters();
  }

The related ModelAutocompleteType form field's result will also be filtered by "state" field even tho it's property is set to search.

How do I apply default filter values JUST to list view and nothing else? And why ModelAutocompleteType result depends on other datagrid fields even tho property is set to one?


Solution

  • In the end I left getFilterParameters method in to filter list by default, which is what I wanted:

    public function getFilterParameters()
    {
      $this->datagridValues = array_merge([
        'state' => ['type' => '', 'value' => 'active']
      ], $this->datagridValues);
    
      return parent::getFilterParameters();
    }
    

    Unfortunatelly, that was also affecting ModelAutocompleteFilter and ModelAutocompleteType results, filtering them by 'active' state too, which I did not want.

    To solve that I had to pass a callback property to ModelAutocompleteType field, to reset datagrid state value:

    class OperationAdmin extends AbstractAdmin
    {
      // ...
    
      protected function configureFormFields(FormMapper $formMapper)
      {
        $formMapper
          ->add('business', ModelAutocompleteType::class, [
            'label' => 'Business',
            'property' => 'search',
            'callback' => [$this, 'filterAllBusinessesCallback']
          ]);
      }
    
      public function filterAllBusinessesCallback(AdminInterface $admin, $property, $value)
      {
        $datagrid = $admin->getDatagrid();
        $datagrid->setValue($property, null, $value);
        $datagrid->setValue('state', null, null);
      }
    
      // ...
    }