0

I tried to mimic the multiple user deletion feature of user.module but for some reason the confirm form submit handler never get called. I don't know where the problem lies in. Following is a working sample of module except for the function delete_multiple_server_confirm_form_submit which never gets executed.

function mymodule_menu() { $items['testservers'] = array( 'title' => 'Manage Servers', 'page callback' => 'display_servers_callback', 'page arguments' => array('delete'), 'access callback' => true, ); return $items; } function display_servers_callback($args = '') { $op = isset($_POST['op']) ? $_POST['op'] : $args; $op = strtolower($op); $build_form = array(); if (!empty($_POST['table']) && isset($_POST['operation']) && ($_POST['operation'] == 'delete')) { $options = $_POST['table']; $build_form['delete_multiple_server_confirm_form'] = drupal_get_form('delete_multiple_server_confirm_form', $options); } else { $build_form['display_servers_form'] = drupal_get_form('display_servers_form'); } return $build_form; } function delete_multiple_server_confirm_form($form, &$form_state, $servers) { $path = current_path(); return confirm_form($form, t('Are you sure you want to delete %title?'), $path, t('This action cannot be undone.'), t('Delete'), t('Cancel') ); } function display_servers_form($form, $form_state) { $header = array( 'server_name' => array('data' => t('Server Name')), 'location' => array('data' => t('Location')), 'protocols' => array('data' => t('Protocols')), 'status' => array('data' => t('server status ')), ); $servers = array( array('server_name' => 'us1', 'location' => 'us', 'protocols' => 'test', 'status' => 'ok'), array('server_name' => 'us2', 'location' => 'us', 'protocols' => 'test', 'status' => 'ok'), array('server_name' => 'us3', 'location' => 'us', 'protocols' => 'test', 'status' => 'ok'), ); $options = array(); for ($index = 0; $index < count($servers); $index++) { $options[$index] = array( 'server_name' => $servers[$index]['server_name'], 'location' => $servers[$index]['location'], 'protocols' => $servers[$index]['protocols'], 'status' => $servers[$index]['status'], ); } $form['table'] = array( '#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => t('No servers found'), ); $options = array(); $operations = array('delete' => array( 'label' => t('delete servers'), 'callback' => 'delete_multiples', ), 'suspend' => array( 'label' => t('suspend servers'), ), ); foreach ($operations as $operation => $array) { $options[$operation] = $array['label']; } $form['options']['operation'] = array( '#type' => 'select', '#title' => t('Operation'), '#title_display' => 'invisible', '#options' => $options, '#default_value' => 'delete', ); $form['options']['submit'] = array( '#type' => 'submit', '#value' => t('Update'), ); return $form; } function delete_multiple_server_confirm_form_submit($form, &$form_state, $servers) { drupal_set_message('working'); } 

4 Answers 4

1

After careful rereading of the user module, i realize that 2 more hidden form values need to be added so the same delete_multiple_server_confirm_form will get called again after confirming action,otherwise the display_server_form will be rendered.

$form['table'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); foreach($servers as $id) { $form['table'][$id] = array( '#type' => 'hidden', '#value' => $id, ); } 
0

What about:

$form['options']['submit'] = array( '#type' => 'submit', '#value' => t('Update'), '#submit' => array('delete_multiple_server_confirm_form_submit'), ); 
2
  • Thanks but this makes no difference Commented Mar 29, 2014 at 16:29
  • This would work if you did it in a form_alter hook (and changed it to $form['actions']['submit']), but if you look at the code for confirm_form() you can see it initialises $form['actions']['submit'] explicitly, overwriting anything you might already have added in the original form function Commented Mar 29, 2014 at 17:01
0

Change you function name from delete_multiple_server_confirm_form_submit to display_servers_form_submit

1
  • no, that will defeat its purpose, and i want it to have its own submit handler. the user module has a user_multiple_cancel_confirm and user_multiple_cancel_confirm_submit which work just like this Commented Mar 29, 2014 at 16:43
-1

If you are rendering your form using form--anyformname.tpl.php template file then make sure in that template file you are rendering these three thing at the end:

 <div> <?php print render($form['form_id']); ?> <?php print render($form['form_build_id']);?> <?php print render($form['form_token']); ?> </div> 
1
  • The OP is NOT using any theme to render the form. The question is: Why isn't the form submission handler of my module invoked? Commented Jul 30, 2015 at 12:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.