Hi All
I have a node with one field that is a node reference.This field should be set automatically in the form submit when a set of rules (eg:author keyword ,language ...etc) are submitted.
To implement this i am using a hook form alter.In the hook_form_alter I am calling a function named my_rules() with the $form and $form_state variables as parameters.This function adding a fieldset with few form elements, including a button.When the button is clicked a section of the fieldset should updated showing the list of rules as checkboxes.
My problem is that the hook_form_alter does not seem to run in the form rebuild and the fieldset is not getting updated with the list of rules.The only way i can get this to work is to reapply the the function i am running in the form alter after the form is rebuilt in the ahah callback.
My form alter function
function my_form_alter(&$form,&$form_state,$form_id){
if($form_id=='my_node_form'){
my_rules($form,$form_state):
}
}
Here is the code in the callback
function my_ahah_callback(){
// The form is generated in an include file which we need to include manually.
include_once 'modules/node/node.pages.inc';
// We're starting in step #3, preparing for #4.
$form_state = array('storage' => NULL, 'rebuild' => TRUE);
$form_build_id = $_POST['form_build_id'];
// Step #4.
$form = form_get_cache($form_build_id, $form_state);
// Preparing for #5.
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Step #5.
drupal_process_form($form_id, $form, $form_state);
// Step #6 and #7 and #8.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Step #9.
//my_rules($form, $form_state);//NOTE NOTHING WORK WHEN I COMMENT THIS LINE
$sub_form = $form['auto_feeds']['rules'];
unset($sub_form['#prefix'], $sub_form['#suffix']);
$output = theme('status_messages') . drupal_render($sub_form);
// Final rendering callback.
drupal_json(array('status' => TRUE, 'data' => $output));
exit();
}
Comments
Comment #1
faboulaws commentedIt is solved.
I am calling the function in the form alter by checking the value of arg(2) (=edit).
But in the ajax callback the value of arg(2) is different so the function is not running.
Thanks
Comment #1.0
faboulaws commentedissue details improved
Comment #2
faboulaws commented