Updating a submission

In the instance where the user clicks on the Decline speaker application confirmation button our action needs to perform two tasks. Firstly, we must update the Status component to indicate that the submission has been declined. Secondly, we must send a notification e-mail to the speaker.

These actions will be driven by the confirmation form submit function.

As we will notice in the following code, the Drupal Form API will automatically look for a function named after the form generating function, where the function name ends with _submit.

In our implementation of hook_menu() we told Drupal that our form would be defined in a function called webform_custom_action_speaker_declined. This is the function we just coded.

When a visitor to our form clicks on the Submit button on the form, Drupal looks for a function called webform_custom_action_speaker_declined_submit to process the posted data.

How to do it...

Let's code the function that will process our custom actions activities into our webform_custom_action.module file.

/**
* Submit function for the decline speaker form.
*/
function webform_custom_action_speaker_declined_submit($form, &$form_state) {
// Retrieve node and submission.
$node = $form_state['build_info']['args'][0];
$submission = $form_state['build_info']['args'][1];
// Set Status as declined.
$submission->data[19]['value'][0] = 'Declined';
// Update the submission.
if (webform_submission_update($node, $submission)) {
drupal_set_message(t('Speaker application declined.'));
}
else {
drupal_set_message(t('Could not decline this speaker application.'), 'error'),
return;
}
// Send notification email.
webform_custom_action_speaker_status_email($submission);
}

Again, we should remember to modify component identifiers as required.

How it works...

Our first action in this function is to retrieve the $node and $submission objects from the $form_state array. Effectively, all we are doing here is shortening the variable names and making the code a little more legible.

After setting the Status component value to Declined we call Webform's own submission update function to effect the update. If the update succeeds, then Drupal will print a notification message that the update was successful and processing will continue to the functions that will send the notification e-mail.

In the unlikely event that the update fails, Drupal will print an error message and processing will not attempt to send the notification e-mail. Clearly we do not want the system sending a notification e-mail if the Status component does not reflect that the submission has been declined.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset