Confirming the requested action

Our next bit of code is going to utilize the Drupal Form's API to build our action confirmation page. This simple page will ask the user whether they are sure they wish to decline a speaker's presentation. For reference purposes we will display the speaker's name, surname, and presentation title also. Users will then either click on the Decline speaker application confirmation button and decline the presentation, or they will click on Cancel to return to the submission View or Edit page they came from.

Confirming the requested action

How to do it...

Here is the function that will draw our confirmation form. Let us add the code to our webform_custom_action.module file.

/**
* Form confirming the declining of a speakers presentation.
*/
function webform_custom_action_speaker_declined($form, $form_state, $node, $submission) {
// Get submitted speaker name and surname.
$speaker_name = $submission->data[1]['value'][0] . ' ' . $submission->data[2]['value'][0];
// Get submitted presentation title.
$presentation_title = $submission->data[4]['value'][0];
// Render the admin UI breadcrumb.
webform_set_breadcrumb($node, $submission);
$form['#tree'] = TRUE;
$form['#node'] = $node;
$form['#submission'] = $submission;
$form['actions'] = array('#type' => 'actions'),
$form['actions']['text'] = array('#markup' => '<p>' . t('Are you sure you want to Decline this speaker application: <em>' . $speaker_name . ' - ' . $presentation_title . '</em>?') . '</p>'),
$form['actions']['declined'] = array(
'#type' => 'submit',
'#value' => t('Decline speaker application'),
);
$form['actions']['cancel'] = array(
'#type' => 'markup',
'#markup' => l(t('Cancel'), isset($_GET['destination']) ? $_GET['destination'] : 'node/' . $node->nid . '/submission/' . $submission->sid),
);
return $form;
}

How it works...

The first few lines retrieve some of the speaker's personal and presentation details from the submitted data object (we should remember to modify the $submission->data component identifiers if ours differ from the example code). These data will be used to contextualize the confirmation form for the benefit of our users.

After rendering the Webform administrative user interface breadcrumb, we store the current Webform node and submission objects. These will be passed into the form submit function when the user clicks on the Decline speaker application confirmation button.

The remaining code causes the confirmation question, the Decline speaker application confirmation button, and the Cancel link to be rendered. Form done.

Drupal's Forms API is extremely powerful, and once we understand its rudiments, will save us hours of coding. For a taste of what the API enables, we may visit the Form API Quickstart Guide at http://drupal.org/node/751826.

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

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