0
votes

I am getting my ajax callback in normal custom form, but on form alter its not working.

function sample_ajax_form_alter(&$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' => t('training name'),
   '#type' => 'select',
   '#options' => _load_training(),
   '#required' => FALSE,
   '#ajax' => [
    'callback' => [$this, 'changeOptionsAjax'],
    // 'callback' => '::changeOptionsAjax',
    'wrapper' => 'second_field_wrapper',
  ],
 );

 $form['field_namedomain'] = [
  '#type' => 'select',
  '#title' => t('Domain program'),
  '#options' => $this->getOptions($form_state),
    '#prefix' => '<div id="second_field_wrapper">',
    '#suffix' => '</div>',
   ];

 return $form; 
 }
}

 function _load_training() {
 $training = array('- Select domain training -');
 $query = db_select("node__field_trainingname", "a");
 $query->fields("a", array('field_trainingname_value', 'entity_id'));
 $query->orderBy("a.field_trainingname_value");
 $result = $query->execute();

 while($row = $result->fetchObject()){
  $training[$row->entity_id] = $row->field_trainingname_value;
  }
   return $training;
  }

  function changeOptionsAjax(array &$form, FormStateInterface $form_state)   {
   return $form['field_namedomain'];
   } 

   function getOptions(array &$form, FormStateInterface $form_state) {
           $cvvv = $form_state->getValue('field_nametrain');
        <!-- return ["shgsh", $form_state->get(['field_nametrain'])]; -->
  $options = array('- Select subdomain category -');
   $query  = db_select("node__field_trainingname", "a");
   $query->fields("a", array('field_trainingname_value', 'entity_id'));
   $query = db_select("node__field_cms", "b");
   $query->fields("b", array('field_cms_value', 'entity_id'));
   $query->join("node__field_trainingname", "b", "b.entity_id=a.entity_id");
   $query->condition("a.entity_id", $cvvv);
   $result = $query->execute();

  while($row = $result->fetchObject()){
    $options[$row->entity_id] = $row->field_cms_value;
  }

   return $options;
   }

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.

Kindly suggest me ideas to apply in form_alter of Drupal 8

2

2 Answers

0
votes

The .module file, where your form alter hook is located, is not a class, therefore there is no $this. Your custom form however is a class (usually in your_module/src/Form/YourForm.php), that's why it works there but not in the .module file.

Further reading: http://www.php.net/manual/en/language.oop5.basic.php and What does the variable $this mean in PHP?

In your case you should be able to just call

'#options' => getOptions($form, $form_state),

And more on a side note: I would strongly recommend to do some code refactoring.

0
votes

In your custom submit handler, firt get the form object from the form state.

$formObj = $formState->getFormObject();

then call submitForm() on the form object and pass the form and form state variables.

$formObj->submitForm($form, $formState);

and finally, you just need to simply trigger the save() function on the object.

$formObj->save($form, $formState);

So the whole solution is something like

function YOR_CUSTOM_SUBMIT_HANLDLER(array $form, FormStateInterface $form_state) {
  /** @var Drupal\user\RegisterForm $entity */
      $formObj = $form_state->getFormObject();
      $formObj->submitForm($form, $form_state);
      $formObj->save($form, $form_state);
}