0

This very simple #ajax customisation to a node form is not working. How can I tell whether the callback has been added? I want to calculate a field if the first field is changed.

function cbi_currency_form_node_bond_edit_form_alter(&$form, FormStateInterface 
   $form_state, $form_id) {

    $form['field_bond_amt_issued']['#ajax'] = [
    'callback' => 'cbi_currency_convert_usd',
    'event' => 'change',
    'wrapper' => 'edit-field-bond-usd-amt-issued-wrapper',
    'progress' => [
      'type' => 'throbber',
       'message' => t('Converting to USD...'),
     ],
  ];
 $form['field_bond_amt_issued']['#prefix'] = t('checking to see an ajax element.');
}

Take the value from field_bond_amt_issued and calculate a value for field_bond_usd_amt_issued.

The callback is simply

function cbi_currency_convert_usd(array &$form, FormStateInterface $form_state) {
  $form['output']['#value'] = 33;
  return $form['output'];
}

I don't see any xhr requests when reviewing in the network console in the browser.

6
  • Using xdebug to step through the code would be the easiest way to see the callback called. But that prefix looks odd? What is "$form['output']" related to?
    – Kevin
    Commented Sep 20, 2021 at 8:11
  • I've had hit and miss in the past with x-debug on the callback. I can't see it being hit in the network view, so am assuming it isn't even being triggered. Xdebug definitely not picking it up.
    – Interlated
    Commented Sep 20, 2021 at 9:05
  • So it's a widget form element not a standard form? Xdebug would show you the form element structure.
    – Kevin
    Commented Sep 20, 2021 at 11:39
  • Standard content editing form. I can see the form structure.
    – Interlated
    Commented Sep 20, 2021 at 21:00
  • Yes - is #ajax being added into the right part of the structure, though? See: drupal.stackexchange.com/questions/277008/…
    – Kevin
    Commented Sep 20, 2021 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.