You are here

function uc_ajax_multiplex in Ubercart 7.3

Ajax callback multiplexer.

Processes a set of Ajax commands attached to the triggering element.

1 string reference to 'uc_ajax_multiplex'
uc_ajax_process_form in uc_store/includes/uc_ajax_attach.inc
Form process callback to allow multiple Ajax callbacks on form elements.

File

uc_store/includes/uc_ajax_attach.inc, line 156
Contains logic to aid in attaching multiple ajax behaviors to form elements on the checkout and order-edit forms.

Code

function uc_ajax_multiplex($form, $form_state) {
  $element = $form_state['triggering_element'];
  if (!empty($element['#ajax']['list'])) {
    $commands = array();
    foreach ($element['#ajax']['list'] as $wrapper => $callback) {
      if (!empty($callback) && function_exists($callback) && ($result = $callback($form, $form_state, $wrapper))) {
        if (is_array($result) && !empty($result['#type']) && $result['#type'] == 'ajax') {

          // If the callback returned an array of commands, simply add these to the list.
          $commands = array_merge($commands, $result['#commands']);
        }
        elseif (is_string($wrapper)) {

          // Otherwise, assume the callback returned a string or render-array, and insert it into the wrapper.
          $html = is_string($result) ? $result : drupal_render($result);
          $commands[] = ajax_command_replace('#' . $wrapper, trim($html));
          $commands[] = ajax_command_prepend('#' . $wrapper, theme('status_messages'));
        }
      }
    }
  }
  if (!empty($commands)) {
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
}