How to populate an Entity Reference field from a modal AjaxResponse? I have an entity ref autocomplete field (reference to bundle A) in one of my bundles (bundle B). I want to add a button below this field which will open a modal for a bundle A create form.

This code works great:

  $form['#attached']['library'][] = 'core/jquery.form';
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $form['field_ref_to_bundle_a']['#suffix'] = '
    &lt;div&gt;&lt;a href="/node/add/bundle_a"
      class="use-ajax button"
      data-dialog-type="modal"
      data-dialog-options="{&amp;quot;width&amp;quot;:800,&amp;quot;height&amp;quot;:500}"&gt;' .
        t('Add New Bundle A') .
    '&lt;/a&gt;&lt;/div&gt;';


When I hit Create in the modal, the new entity is created; but I need to assign this new entity to the field on the parent form (bundle B). I have added a #submit callback with a form alter on the modal form and in that callback I have:

  $response = new AjaxResponse();
  $response-&gt;addCommand(new CloseModalDialogCommand());
  $form_state-&gt;setResponse($response);


which closes the modal. In this call i know the NID of the newly created entity and I am guessing there must be some way to set the value on that field in the parent form.

Although i don't think it should be necessary, i have seen code suggesting i can use a $response->addCommand(new ReplaceCommand()) to swap out the entire original field with one that is populated with the default value; but i have had no luck in doing this. I have been able to use this $response call to swap out the entire autocomplete with a text string; so does seem like i am on the right path. All i really need to do is set the default value of the autocomplete field; but not sure how to do this through ajax.

Going with the idea that i need to replace the entire field (as opposed to just setting the default value); i found this code:

  $node = Node::load($nid);
  $elem = [
    '#type' =&gt; 'entity_autocomplete',
    '#target_type' =&gt; 'node',
    '#default_value' =&gt; $node,
    '#attributes' =&gt; [
      'id' =&gt; ['#edit-field-ref-to-bundle-a-0-target-id'],
    ],
  ];
  $renderer = \Drupal::service('renderer');
  $renderedField = $renderer-&gt;render($elem);


Although this does render something:

&lt;div class="js-form-item form-item js-form-type-entity-autocomplete form-type-entity-autocomplete js-form-item- form-item- form-no-label"&gt;
&lt;input id="#edit-field-ref-to-bundle-a-0-target-id" type="text" size="60" maxlength="128" class="form-text" /&gt;
&lt;/div&gt;


It doesn't have a default value set. But perhaps I am close??
Figured out it was the InvokeCommand i needed and to set "val" to the referenced entity title with id in ().

  $nid = $form_state-&gt;getValue('nid');
  $node = Node::load($nid);
  $value = $node-&gt;getTitle() . ' (' . $nid . ')';

  $response = new AjaxResponse();
  $response-&gt;addCommand(new CloseModalDialogCommand());
  $response-&gt;addCommand(new InvokeCommand('#edit-field-ref-to-bundle-a-0-target-id', 'val', [$value]));
  $form_state-&gt;setResponse($response);