Score:-1

How do I add ajax callback to a form element which is created in a foreach loop

cn flag

Please see the following code:

foreach ($formaddresses as $key => $val) {
  // Add edit buttons to custom client-side created addresses.
  if (!isset($val['accountnumber'])) {
    $form['orderinfo']['billshipaddresses']['billingaddresses'][$key]['#description'] = '<a class="edit-stored-address edit-stored-address-target-' . $key . '">Edit</a>';
    $form['orderinfo']['billshipaddresses']['shippingaddresses'][$key]['#description'] = '<a class="edit-stored-address edit-stored-address-target-' . $key . '">Edit</a>';
  }
}

Here this loop is creating some anchors in my form. I just need to attach/add '#ajax' callback to it. How can I achieve this?

Something like the following code:

$form['orderinfo']['billshipaddresses']['shippingaddresses'] = [
  '#type' => 'radios',
  '#title' => 'Shipping Address',
  '#options' => $formaddressesOptions,
  '#default_value' => '',
  '#ajax' => [
    'callback' => '::buildShipBillAddresses',
    'wrapper' => 'shippingaddresses',
    'event' => 'change'
  ],
];

Here we can see if the above radio is change then an ajax callback will work and will call this method buildShipBillAddresses Thanks!

Score:1
us flag

Actually, the anchor element to which you want to add an AJAX callback isn't a form element. The Ajax API page says how to trigger AJAX for a link.

Add class 'use-ajax' to a link. The link will be loaded using an Ajax call. When using this method, the href of the link can contain '/nojs/' as part of the path. When the Ajax JavaScript processes the page, it will convert this to '/ajax/'. The server is then able to easily tell if this request was made through an actual Ajax request or in a degraded state, and respond appropriately.

This means that for a link, the following code would work.

foreach ($formaddresses as $key => $val) {
  // Add edit buttons to custom client-side created addresses.
  if (!isset($val['accountnumber'])) {
    $form['orderinfo']['billshipaddresses']['billingaddresses'][$key]['#description'] = '<a href="/nojs/edit-billing-address" class="use-ajax edit-stored-address edit-stored-address-target-' . $key . '">Edit</a>';
    $form['orderinfo']['billshipaddresses']['shippingaddresses'][$key]['#description'] = '<a href="/nojs/edit-shipping-address" class="use-ajax edit-stored-address edit-stored-address-target-' . $key . '">Edit</a>';
  }
}

The module needs to define two routes for those links. I cannot tell you the code for those route callbacks, but they should render the Edit form to make it appear on that form.

Score:0
de flag

This isn't really a Drupal question so much as a PHP question, but to answer your question:

foreach ($formaddresses as $key => $val) {
  // Add edit buttons to custom client-side created addresses.
  if (!isset($val['accountnumber'])) {
    $form['orderinfo']['billshipaddresses']['billingaddresses'][$key]['#ajax'] = [
      'callback' => '::buildShipBillAddresses',
      'wrapper' => 'shippingaddresses',
      'event' => 'change'
    ];
  }
}
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.