title: Drupal 8 Form alter the ajax callback is not working
body: <p>I am getting my ajax callback in normal custom form, but on form alter its not working.</p>
  
  <pre><code>function sample_ajax_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if ($form_id === 'node_sampleajax_form' || $form_id === 'node_sampleajax_edit_form') {
     $form['field_nametrain']= array(
     '#title' =&gt; t('training name'),
     '#type' =&gt; 'select',
     '#options' =&gt; _load_training(),
     '#required' =&gt; FALSE,
     '#ajax' =&gt; [
      'callback' =&gt; [$this, 'changeOptionsAjax'],
      // 'callback' =&gt; '::changeOptionsAjax',
      'wrapper' =&gt; 'second_field_wrapper',
    ],
   );
  
   $form['field_namedomain'] = [
    '#type' =&gt; 'select',
    '#title' =&gt; t('Domain program'),
    '#options' =&gt; $this-&gt;getOptions($form_state),
      '#prefix' =&gt; '&lt;div id="second_field_wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
     ];
  
   return $form; 
   }
  }
  
   function _load_training() {
   $training = array('- Select domain training -');
   $query = db_select("node__field_trainingname", "a");
   $query-&gt;fields("a", array('field_trainingname_value', 'entity_id'));
   $query-&gt;orderBy("a.field_trainingname_value");
   $result = $query-&gt;execute();
  
   while($row = $result-&gt;fetchObject()){
    $training[$row-&gt;entity_id] = $row-&gt;field_trainingname_value;
    }
     return $training;
    }
  
    function changeOptionsAjax(array &amp;$form, FormStateInterface $form_state)   {
     return $form['field_namedomain'];
     } 
  
     function getOptions(array &amp;$form, FormStateInterface $form_state) {
             $cvvv = $form_state-&gt;getValue('field_nametrain');
          &lt;!-- return ["shgsh", $form_state-&gt;get(['field_nametrain'])]; --&gt;
    $options = array('- Select subdomain category -');
     $query  = db_select("node__field_trainingname", "a");
     $query-&gt;fields("a", array('field_trainingname_value', 'entity_id'));
     $query = db_select("node__field_cms", "b");
     $query-&gt;fields("b", array('field_cms_value', 'entity_id'));
     $query-&gt;join("node__field_trainingname", "b", "b.entity_id=a.entity_id");
     $query-&gt;condition("a.entity_id", $cvvv);
     $result = $query-&gt;execute();
  
    while($row = $result-&gt;fetchObject()){
      $options[$row-&gt;entity_id] = $row-&gt;field_cms_value;
    }
  
     return $options;
     }
  </code></pre>
  
  <p>On using $this->getOptions($form_state) it represent the error log it is not an object and throws website encounter error in front end. But on custom form no error came only in formalter it throws error.</p>
  
  <p>Kindly suggest me ideas to apply in form_alter of Drupal 8</p>