1

I have a form with a radio buttons (inside div#edit-field-nomination-und). I need to call a following AJAX callback:

function _onfit_ajax_get_term_description_callback($term) {
  return array(
    '#type' => 'ajax',
    '#commands' => array(
      ajax_command_html('#nomination-desc', $term->description)));
}

function onfit_menu() {
  // ...
  $items['ajax/onfit/taxonomy/term/%taxonomy_term/description'] = array(
    'title' => 'Get taxonomy term description by AJAX',
    'page callback' => '_onfit_ajax_get_term_description_callback',
    'page arguments' => array(4),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'delivery callback' => 'ajax_deliver',
  );
  return $items;
}

Here is an attachment code (I dislike it and I want to use #ajax['path']):

Drupal.behaviors.onfitExampleBehavior = {
  attach : function(context, settings) {
    $('#edit-field-nomination-und input', context)
        .once('edit-field-nomination-und', function() {
          var url = '/ajax/onfit/taxonomy/term/' + $(this).val() + '/description'
          new Drupal.ajax('nomination-desc-wrapper', $(this), { url: url, event: 'change' });
        });
  }
};

Is it possible to attach AJAX callback to a radio buttons by the means of hook_form_alter? I don't want to use #ajax['callback'], because the entire form will be send during each AJAX call. I think #ajax['path'] must be a more lightweighted. But the problem is that I don't understand how to pass a selected radio item id to a #ajax['path'].

0
1

From the docs:

It is possible to replace ajax_form_callback() with your own functions. If you do so, ajax_form_callback() would be the model for the replacement. In that case, you would change #ajax['path'] from the default 'system/ajax' and set up a menu entry in hook_menu() to point to your replacement path.

So you need to

  1. Set up a menu callback (looks like you've already done that)
  2. Copy ajax_form_callback() to that path's page callback function
  3. Make the changes you need to. The function is very short so it should be obvious which bits you need.
  4. In your form elements use this:

    $form['foo'] = array(
      ...
      '#ajax' => array(
        ...
        'path' => 'path/to/new/callback',
      ),
    );
    

You shouldn't write any JS code to invoke this manually, Drupal takes care of that for you.

Note that this method does load the full form; that's unavoidable using Drupal's built-in AJAX functionality.

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.