Creating a conditional action

As explained in the requirements specification at the start of the chapter, we only need our action to show up on the submission View and Edit pages if the current submission has a status of Accepted. We will utilize the hook_webform_submission_actions() hook exposed by Webform to add this programming logic.

How to do it...

Let us add the programming hook to our webform_custom_action.module file.

/**
* Implements hook_webform_submission_actions().
*/
function webform_custom_action_webform_submission_actions($node, $submission) {
$actions = array();
// Does this submission already have Declined status?
$declined = FALSE;
if (isset($submission->data[19]['value'][0])) {
if ($submission->data[19]['value'][0] == 'Declined') {
$declined = TRUE;
}
}
// Show action if user has results access and submission not already declined.
if (webform_results_access($node) && !$declined) {
$actions['declined'] = array(
'title' => t('Decline speaker'),
'href' => 'node/' . $node->nid . '/submission/' . $submission->sid . '/declined',
'query' => drupal_get_destination(),
);
}
return $actions;
}

We may find that our individual sites have a different CID (component identifier) for the Status component, thus we should amend $submission->data[19] accordingly. We need to ensure that the number in square brackets matches the CID we obtained from the component overview page earlier.

How it works...

The first portion of logic checks the $submission->data array to determine whether it contains a value for the Status component. If the component data is present and has been set to the value Declined, then we set a Boolean to TRUE to indicate that the current submission has already been declined.

In the second section we add our declining action to the array of Webform submission actions, if it is needed. This will only occur when the current user has the Access all webform results permissions and the Status component value is not already set to Declined. When either (or both) of these conditions are not met, the action is simply not added to the actions array and will, consequently, not be available on the submission View and Edit pages.

There's more...

The $actions array bears some examination. We can see that we have added an associative array element named declined (that is, $actions['declined']) but what are the array of entries within that for?

$actions['declined']['title']

This is the title of our action link as it will appear on the submission View and Edit pages. The title will be displayed next to the Resend emails link.

$actions['declined']['href']

Here we specify the Drupal path to the confirmation form. It is a good practice to allow the user to confirm an action, rather than summarily carrying it out. As we will see a little later, the confirmation form will permit users to gracefully back out of the action before they accidentally decline the wrong speaker.

$actions['declined']['query']

This parameter allows us to specify where Drupal should return to after the requested action has been carried out. The Drupal core drupal_get_destination() function, when called with no parameters within the brackets, will return to the URL of the current page. In our case this means that Drupal will return to the submission View or Edit page from which the action was originally requested.

Adding more actions

We are free to add as many actions as our particular applications require. The main thing to bear in mind is the naming of the action array. If we were to specify another $actions['declined'] entry it would over-write (replace) the existing one. Each action must have its own unique name and its own href URL.

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

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