Search code examples
drupaldrupal-7drupal-modules

Show a confirmation page before complete function call


I have written a module for our club. What i have now is a page that displays a html table showing the availability of club room

I have added a link to the last column of that table [reference code sample] that when clicked will let the user change the value from yes to no. Shocking I got the updating of the query working, passing the parameters, but now I am stuck on the flow.

The way it is working right now is:

  • User clicks link mentioned above
  • it goes to my module/function, run its, which changes the value from 'Y' to 'N'
  • then it goes to my change_availability_test that list in the module/function

what I want is:

  • User clicks link
  • goes to [change_availability_test] page that asks, Are you sure you want to change the availability?
  • User has the option to click a "Change availability" button, that then runs the module/function and they are returned to the original page that's displaying the html table or if they click a "No" no action taken, just returned to the original page that's displaying the html table.

List item

$items['/change_availability/%'] = array(
       'title' => 'Change Availability',
       'page callback' => 'change_availability_test',
       'access arguments' => array('user_access'),
       'page arguments' => array(2),
       'access callback' => TRUE,
      'type' => MENU_CALLBACK,
         );

 return $items;
}

   /**
    * Change space availability status.
    */
   function change_availability_test($rid = NULL, ) {

     //This function runs the db update query.
   }

    if ($form['rid']) {
       foreach (element_children($form['rid']) as $key) {
         $rows[] = array(
           'data' => array(
             drupal_render($form['rid'][$key]),
             drupal_render($form['squad_name'][$key]),
             drupal_render($form['task_id'][$key]),
             drupal_render($form['email'][$key]),
             drupal_render($form['club_location'][$key]),
             drupal_render($form['rating'][$key]['grade']),
             l(t('click'), "change_availability/".$key),
           ),
           'class' => $form['status'][$key]['#value'],
         );
         }
       $headers = array(
         array('data' => t('id')),
         array('data' => t('squad_name')),
         array('data' => t('task_id')),
         array('data' => t('email')),
         array('data' => t('Club Location')),
         array('data' => t('Rating')),
         array('data' => t('Available?')),
       );

       $output = theme('table', $header, $rows, array('id' => 'room-listing'));
       $output .= drupal_render($form);
     }
     return $output;
   }

Solution

  • You can add confirmation form to your page. There is a Drupal API function for this - confirm_form(), that helps you create such form.

    Change your code to like this:

    // In your hook_menu().
    $items['change_availability/%'] = array(
      'title' => t('Change Availability'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('change_availability_test_form', 1),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
    );
    return $items;
    
    
    /**
     * Form callback for 'change_availability/%' path.
     */
    function change_availability_test_form($form, $form_state, $account) {
      $form['account'] = array(
        '#type' => 'value',
        '#value' => $account,
      );
    
      return confirm_form($form, t('Are you sure you want to change the availability?'), 'here_goes_your_backurl_for_cancel_link');
    }
    
    /**
     * Submit callback for 'change_availability_test_form' form.
     */
    function change_availability_test_form_submit($form, &$form_state) {
      $account = $form_state['values']['account'];
      // Call your function
      change_availability_test($account);
      // Redirect back to te table.
      $form_state['redirect'] = 'your_backurl';
    }