Form filter by field on entity_autocomplete I have a form with this field.
the user can save one or more results. 
I want to filter by the foreign key who is on the user table but i don't know how to do this. 

My code : 

$form['referent_business'] = [
  '#title' =&gt; 'Référent',
  '#type' =&gt; 'entity_autocomplete',
  '#target_type' =&gt; 'user',
  '#required' =&gt; TRUE,
  '#tags' =&gt; TRUE,
  '#validate_reference ' =&gt; TRUE,
  '#autocreate' =&gt; false
 ]


So I tried another solution : 

$form['referent_business'] = [
  '#title' =&gt; 'Référent',
  '#type' =&gt; 'textfield',
  '#autocomplete_route_name' =&gt; 'jds.autocomplete_referent_ce',
  '#autocomplete_route_parameters' =&gt; [
    'business' =&gt; $entity-&gt;id()
  ],
  '#required' =&gt; TRUE,
  '#tags' =&gt; TRUE,
  '#validate_reference ' =&gt; TRUE,
  '#autocreate' =&gt; false,
  '#access' =&gt; $user-&gt;hasRole('administrator') ? TRUE : FALSE,
];


With the function

public function autocompleteReferentCE(Request $request, $business)
{
    $matches = [];
    if($string = $request-&gt;query-&gt;get('q')) {
        $query = \Drupal::entityQuery('user')
            -&gt;condition('status', 1)
            -&gt;condition('id_business', $business)
            -&gt;condition('last_name', '%'.db_like($string).'%', 'LIKE');
        $idUsers = $query-&gt;execute();
        $users = User::loadMultiple($idUsers);  
        foreach($users as $user) {
            $matches[] = [
                'value' =&gt; $user-&gt;get('first_name')-&gt;value . ' ' . $user-&gt;get('last_name')-&gt;value,
                'label' =&gt; $user-&gt;get('first_name')-&gt;value . ' ' . $user-&gt;get('last_name')-&gt;value
            ];
        }
    }
    return new JsonResponse($matches);
}


The custom entity_autocomplete function works. When the user saves several results, the return isn't an array, but a string.

So, someone knows how to filter with the first solution or return an array of result with the second solution ? Or another solution ? 

Thanks in advance
The core autocomplete feature expects the label of the returned matches contain the id of the referenced entity like Superuser (8) which is parsed by the drupal.autocomplete behavior and submitted through the actual form input. The easy solution its to ensure that your response returns the expected values. But you can always implement your own. See Drupal\entity_reference\EntityReferenceController to see how the core implenents it.