1

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

B.lakshman
  • 401
  • 1
  • 6
  • 19

2 Answers2

0

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.

Community
  • 1
  • 1
Frank Drebin
  • 1,063
  • 6
  • 11
  • ( $cvvv = $form_state->getValue('field_nametrain');) it returns the empty value ('#options' => getOptions($form, $form_state) on using your suggestion also – B.lakshman Feb 15 '17 at 19:57
  • Your function getOptions is called **before** even rendering the form, so it's normal that it is empty. You're trying to display different options for the second field dependent on the first field, right? I think it's not a good solution to query the database via Ajax on every change of the select field. I would suggest you build the complete select lists before rendering and handle the hiding/showing on the client side via JS/CSS. And even if somebody hacked the options in the browser console, you can still validate them in the backend. – Frank Drebin Feb 16 '17 at 09:53
  • You are right. I am using two content types one for input(save value) and another form is bringing value as a select list. The first field entity_id condition set is dependent on the second field. Can you suggest me some example link for drupal 8 form alter ajax – B.lakshman Feb 20 '17 at 07:01
0

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);
}
Yuseferi
  • 7,931
  • 11
  • 67
  • 103