Drupal Example of Ajax on Field group multiple using hook_form_alter

This CodeLet let you konw how to implement ajax on field group multiple module.
"fgm_node_YOURNODE_form_group_YOURGROUPNAME" where "YOURNODE" will be the name of your content type and "YOURGROUPNAME" will be the name of your field group,
Given example is for text field, ajax call on blur event.

CodeLet: 

function mymodule_form_alter(&$form, $form_state, $form_id) {

if($form_id == 'group_node_form'){

$ar__field_item = $form['fgm_node_YOURNODE_form_group_YOURGROUPNAME']['fields']['items'];
// here content type is sales and group name is sl_multiple_product_group
foreach ($ar__product_item as $key => $value) {
$form['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'][$key]['field_sl_quantity']['und']['value'][''] = array(
'callback' => 'get_unit_price',
'wrapper' => 'unit_price_callback_replace_' . $key,
);
$form['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'][$key]['field_sl_product_price']['und']['value'][''] = array(
'callback' => 'get_unit_price',
'wrapper' => 'unit_price_callback_replace_' . $key,);

$form['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'][$key]['field_sl_unit_price']['und'][''] = 'unit price will be here';

}

}

}

/**
* ajax call back
*/

function get_unit_price($form, $form_state) {
$ar__product_item = $form_state['values']['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'];
$arr__ajax_replace = array();
foreach ($ar__product_item as $key => $value) {

$sn__quantity = $form_state['values']['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'][$key]['field_sl_quantity']['und']['value'];
$sn__price = $form_state['values']['fgm_node_sales_form_group_sl_multiple_product_group']['fields']['items'][$key]['field_sl_product_price']['und']['value'];
$sn__unit_price = $sn__quantity * $sn__price ;
$arr__ajax_replace[$key] = ajax_command_replace('' . $key, '' . $sn__unit_price . '');
}

return array(
'' => 'ajax',
'' => $arr__ajax_replace
);

}

Drupal use fo ajax on field group multiple module

By saru1683 on Sat, 22/06/2013 - 12:11pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback