0

I have a form element of "entity_autocomplete" type. Also I have an ajax callback of the element. Here is its code.

$form['autocomplete_places'] = [
            '#type' => 'entity_autocomplete',
            '#target_type' => 'node',
            '#selection_settings' => [
                'target_bundles' => ['place'],
            ],
            '#ajax' => [
                'callback' => '::autocompleteGetPlaces',
                'event' => 'autocompleteselect',
            ]
        ];

public function autocompleteGetPlaces(array &$form, FormStateInterface $form_state){
        $ajax_responce = new AjaxResponse();
/// some code here 
        return $ajax_responce;
    }

Unfortunately, I can't find out how to get the id of selected entity inside the ajax callback function. For example, in my case, it would be id of a node of "place" type.

I tried to debug it, but couldn't find information about it in $form_state.

Could you clarify me, how to do it ?

1 Answer 1

1

Seemingly I managed to find the solution.

The event for the autocomplete element should be "autocompleteclose" instead "autocompleteselect". And in this case it's possible to get entity_id via

$entity_id = $form_state->getValue('autocomplete_places');

if the list of values was closed without selected element then $form_state->getValue('autocomplete_places') is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.