Score:1

Entity reference auto complete field is missing value on ajax callback

ph flag

I have an entity reference field on my content type using the autocomplete widget.

I'm adding an ajax callback to that field using this in a form alter:

$form['field_er']['widget'][0]['target_id']['#ajax'] = [
  'callback' => '::ajaxCallback',
  'event' => 'autocompleteselect',
  'wrapper' => 'nm-resend-selector',
  'progress' => [
    'type' => 'throbber',
    'message' => t('Updating.'),
  ],
];

The ajax call triggers when I select an entity, but in my form alter when I use $formState->getValue('field_er') the target_id is null, when it should be the id of the entity I selected.

How do I get at the selected entity?

Edit: I looked into FormState::input to see if I could get the value from there, but the value there is 'a' which is the text I entered to trigger the autocomplete.

Score:1
ph flag

Looks like I was just using the wrong event, switching the event to 'autocompleteclose' gets me the value I need.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.