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:
what I want is:
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;
}
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';
}