Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass arguments to ajax callback function in drupal 7 form api

how to pass arguments to Ajax callback function in drupal 7 form api

$element['field name'] = array(
        '#type' => 'textfield',
        '#ajax' => array(
          'callback' => 'ajax_function_name_callback'/%/%/%,
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );

function ajax_function_name_callback($form,$form_state)
{
return ..
}

for example if i need to specify form element to make action using ajax i need to pass the element name to the function and make customer operation and return the result to another element form

i need passed aruguments to this callback function 'callback' => 'ajax_function_name_callback'

function ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }

2 - and how Through the form ?

thanks..

if i dont know what the $input_name it's genrated from something oprations i need to tell ajax_'function_name_callback the name of this field to make

$element[$input_name] = array(
        '#type' => 'textfield',
        '#size' => '41',
        '#ajax' => array(
//////////////////////////////////////////////////////////////////////////////////
// here how i tell the ajax call back about this arguments informationvlike parents of this field ... etc
/////////////////////////////////////////////////////////////////////////////////
              'callback' => 'ajax_'function_name_callback',
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );


function ajax_'function_name_callback($arg_position,$arg_fieldName,$form,$form_state)
{
$form[$arg_position][$arg_fieldName][#value] = anotherFunction($form[$arg_position][$arg_fieldName][#value]);
return $form[$arg_position][$arg_fieldName];
}
like image 633
user764851 Avatar asked May 22 '11 14:05

user764851


3 Answers

Use this $form_state['triggering_element'] this will tell you the name of the triggering element and give you all of its attributes.

like image 63
caboose Avatar answered Nov 16 '22 01:11

caboose


Through the form. No need for a custom callback.

The ajax callback has access to all information that is part of the form. You can for example add a form element with type hidden (sent to the browser, can be changed with JavaScript for example) or value (only used internally, can contain any kind of data like objects and can not be changed by the user).

If you can give a more detailed example of what you want to do, I can give you a more detailed explanation.

like image 45
Berdir Avatar answered Nov 16 '22 01:11

Berdir


There is no way to pass additional arguments in ajax callback function.

See includes/form.inc on line 381 for more details.

function ajax_form_callback() {
  list($form, $form_state) = ajax_get_form();
  drupal_process_form($form['#form_id'], $form, $form_state);

  // We need to return the part of the form (or some other content) that needs
  // to be re-rendered so the browser can update the page with changed content.
  // Since this is the generic menu callback used by many Ajax elements, it is
  // up to the #ajax['callback'] function of the element (may or may not be a
  // button) that triggered the Ajax request to determine what needs to be
  // rendered.
  if (!empty($form_state['triggering_element'])) {
    $callback = $form_state['triggering_element']['#ajax']['callback'];
  }
  if (!empty($callback) && function_exists($callback)) {
    return $callback($form, $form_state); // TA-DAM!
  }
}
like image 38
Oleg Sherbakov Avatar answered Nov 16 '22 03:11

Oleg Sherbakov