-1

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!

1

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.

0

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'
    ];
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.