Score:0

How to inject/append child element to a radios element and give it an AJAX callback?

cn flag

How can I add independent child element to a radios element in a form? The radio elements are created by the following code.

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

In options, I set an associative array to make all radio. In them, I need to inject an Edit link/button with an AJAX callback, as in the following screenshot.

screenshot

This is the code I am using for the Edit link:

private function formAddressesToOptions($formaddresses) {
$formaddressesOptions = [];
if (!empty($formaddresses)) {
  foreach ($formaddresses as $key => $formaddress) {
    if (isset($formaddress['accountnumber'])) {
      $formaddressesOptions[$key] = $this->formatAddress($formaddress);
    } else {
      $formaddressesOptions[$key] = $this->formatAddress($formaddress) . "<span class='edit-newly-created-address'>Edit</span>";
    }
  }
}
return $formaddressesOptions;
}

The Edit link is shown below each radio, but it has no AJAX callback on its own. How can I achieve that properly?

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.