Score:0

How to see if ajax has been added to a simple node form?

km flag

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.

Kevin avatar
in flag
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?
Interlated avatar
km flag
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.
Kevin avatar
in flag
So it's a widget form element not a standard form? Xdebug would show you the form element structure.
Interlated avatar
km flag
Standard content editing form. I can see the form structure.
Kevin avatar
in flag
Yes - is #ajax being added into the right part of the structure, though? See: https://drupal.stackexchange.com/questions/277008/how-to-implement-ajax-callback-on-form-alter-for-entity-field
Interlated avatar
km flag
Ah - thank you $form['field_bond_amt_issued']['widget'][0]['value']['#ajax'] = [
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.