[development] unable to change ahah property of submit button on form rebuild

nitin gupta nitingupta.iitg at gmail.com
Thu Oct 22 13:28:04 UTC 2009


Hi,

I was working on this module where I needed to change the submit actions of
a submit button, after the submit action of another button. Let us say, I
have two submit buttons, sb1 and sb2:

$form['sb1'] = array(
    '#type' => 'submit',
    '#value' => t('sb1'),
    '#submit' => array('test_module_sb1_submit'),
  );

$form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('sb2'),
    '#submit' => array('test_module_sb2_submit'),
  );

  $form['sb2']['#ahah'] = array(
      'path' => 'test_module/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );


Now when sb1 is submitted and form is rebuild, I am changing the submit
actions of sb2 to these,

$form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('sb2'),
    '#submit' => array('test_module_sb2_change_submit'),
  );

  $form['sb2']['#ahah'] = array(
      'path' => 'test_module_change/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );

The submit function is successfully changed to
'test_module_sb2_change_submit', but the '#ahah' property of 'sb2' remains
the same. Is this the expected behavior or a bug? I could not find any
documentation on this. How this can be achieved? How this can be achieved if
the first button 'sb1' is also 'ahah' processed?

Thanks, I am attaching the test code for quick look on what I expect and
what I get.

test_module.module
------------------------------

function test_module_menu() {

$items = array();

$items['test'] = array(
    'title' => 'Test form resubmit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('test_module_test'),
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );
$items['test_module/js'] = array(
    'title' => 'Ahah submit',
    'page callback' => 'test_module_js',
    'access callback' => '_test_module_access_js',
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );

  $items['test_module_change/js'] = array(
    'title' => 'Ahah submit',
    'page callback' => 'test_module_change_js',
    'access callback' => '_test_module_access_js',
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function _test_module_access_js() {
    return TRUE;
}

function test_module_test(&$form_state) {

    $form['sb1'] = array(
    '#type' => 'submit',
    '#value' => t('SB1'),
    '#submit' => array('test_module_sb1_submit'),
  );

  $form['val'] = array('#value' => '<div id="test_module_replace"> This is
it </div>');

   $form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('SB2'),
    '#submit' => array('test_module_sb2_submit'),
  );

   $form['sb2']['#ahah'] = array(
      'path' => 'test_module/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );

    if($form_state['resub']) {

    $form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('SB2'),
    '#submit' => array('test_module_sb2_change_submit'),
  );

  $form['sb2']['#ahah'] = array(
      'path' => 'test_module_change/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );

    }

    return $form;



}

function test_module_sb1_submit(&$form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['resub'] = TRUE;
}

function test_module_sb2_submit(&$form, &$form_state) {
drupal_set_message(t("In submit 1"));
}

function test_module_sb2_change_submit(&$form, &$form_state) {
drupal_set_message(t("changed to submit 2"));
}


function test_module_js() {

 $form_state = array('storage' => NULL, 'submitted' => FALSE);
 $form_build_id = $_POST['form_build_id'];
 $form = form_get_cache($form_build_id, $form_state);
 $form_state['post'] = $form['#post'] = $_POST;
 $form['#programmed'] = $form['#redirect'] = FALSE;
 $args = $form['#parameters'];
 $form_id = array_shift($args);
 drupal_process_form($form_id, $form, $form_state);
 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
 //Get HTML for the replacement form. Only these elements will be
AHAH-refreshed.
 $new_form = array();

 $new_form['element'] = array('#value' => "Addition by submit 1");
    $output = theme('status_messages') . drupal_render($new_form);
    //Return the results.
    drupal_json(array('status' => TRUE, 'data' => $output));

}
function test_module_change_js() {

 $form_state = array('storage' => NULL, 'submitted' => FALSE);
 $form_build_id = $_POST['form_build_id'];
 $form = form_get_cache($form_build_id, $form_state);
 $form_state['post'] = $form['#post'] = $_POST;
 $form['#programmed'] = $form['#redirect'] = FALSE;
 $args = $form['#parameters'];
 $form_id = array_shift($args);
 drupal_process_form($form_id, $form, $form_state);
 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
 //Get HTML for the replacement form. Only these elements will be
AHAH-refreshed.
 $new_form = array();

 $new_form['element'] = array('#value' => "Never gets added");
    $output = theme('status_messages') . drupal_render($new_form);
    //Return the results.
    drupal_json(array('status' => TRUE, 'data' => $output));

}

test_module.info
-----------------------

name = "test module"
core = 6.x


--
Regards,
Nitin Kumar Gupta
http://publicmind.in/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20091022/3007b936/attachment-0001.html 


More information about the development mailing list