title: How do you modify entity references on a form in a custom ajax action in Drupal 8?
body: <p>I have added a custom button to a form:</p>
  
  <pre><code>    $form['actions']['autotag_content'] = [
       '#type' =&gt; 'button',
       '#value' =&gt; 'Autotag Content',
       '#ajax' =&gt; [ 
         'callback' =&gt; ['\Drupal\taxonomy_migrate\taggerService', 'tagContent'],
         'wrapper' =&gt; ['block-adminimal-theme-content'],
         'progress' =&gt; [ 
           'type' =&gt; 'throbber',
           'message' =&gt; 'Tagging content',
         ],
       ],
     ];
  </code></pre>
  
  <p>Then in the callback I want to add or remove entities from an entity reference field on the form. This would then get sent back to the browser and rerendered. I don't want the changes to be save, I just want them populated in the form and then the user can accept the changes.</p>
  
  <p>For the sake of this example, I have simplified this to just demonstrate the point. I would like to add two entity references to field_tax_subjects and have the frontend form rerender. Currently, the frontend form rerenders, but doesn't reflect the changes</p>
  
  <pre><code>public static function tagContent(array &amp;$form, FormStateInterface &amp;$form_state) {
    $node = $form_state-&gt;getFormObject()-&gt;getEntity();
  
    $node-&gt;field_tax_subjects[] = 12345;
    $node-&gt;field_tax_subjects[] = 23456;
  
    $form = \Drupal::service('entity.form_builder')-&gt;getForm($node);
    $form_state-&gt;setRebuild();
  
    return $form;
  }
  </code></pre>