webform_invitation.module in Webform Invitation 8
Same filename and directory in other branches
Webform Invitation module.
File
webform_invitation.moduleView source
<?php
/**
* @file
* Webform Invitation module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\webform\WebformSubmissionInterface;
/**
* Implements hook_help().
*/
function webform_invitation_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'entity.webform.invitation':
return '<p>' . t('This page allows administrators to enable or disable invitations for the current webform.') . '</p>';
case 'entity.webform.invitation_codes':
return '<p>' . t('This page provides list of all invitation codes that have been generated for the current webform and whether or not they have been used.') . '</p>';
case 'entity.webform.invitation_generate':
return '<p>' . t('This page provides a form to generate invitation codes. You may choose the number of generated codes and their type (MD5 or custom).') . '</p>';
case 'entity.webform.invitation_download':
return '<p>' . t('This page lets you download the list of not used generated codes as an Excel sheet including code and full URL to access the webform with auto-submitting the invitation code.') . '</p>';
}
}
/**
* Implements hook_webform_submission_form_alter().
*/
function webform_invitation_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\webform\WebformSubmissionInterface $submission */
$submission = $form_state
->getFormObject();
// Add form validation only if code element is available.
if (!empty($submission
->getWebform()
->getElement('webform_invitation_code'))) {
$form['#validate'][] = 'webform_invitation_code_validate';
}
}
/**
* Invitation Code element validation.
*
* @see webform_invitation_webform_submission_form_alter()
*/
function webform_invitation_code_validate(&$form, FormStateInterface $form_state) {
$code = $form_state
->getValue('webform_invitation_code');
// Get code details from DB.
$query = \Drupal::database()
->select('webform_invitation_codes', 'c')
->fields('c');
$query
->condition('code', $code);
$result = $query
->execute()
->fetch();
// Set error on element when needed.
if (empty($result)) {
$form_state
->setErrorByName('webform_invitation_code', t('This code is not valid.'));
}
elseif (!empty($result->used)) {
// Not required, handled by webform unique option.
$form_state
->setErrorByName('webform_invitation_code', t('This code has already been used.'));
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function webform_invitation_webform_submission_insert(EntityInterface $entity) {
// Basic validation of entity.
if (!$entity instanceof WebformSubmissionInterface) {
return;
}
$elements = $entity
->getWebform()
->getElementsDecodedAndFlattened();
// Only if current webform has enabled invitations.
if (isset($elements['webform_invitation_code'])) {
$data = $entity
->getData();
// Only if code was sent with current submission.
if (!empty($data['webform_invitation_code'])) {
// Update code usage in DB.
$query = \Drupal::database()
->update('webform_invitation_codes')
->fields([
'used' => \Drupal::time()
->getRequestTime(),
'sid' => $entity
->id(),
]);
$query
->condition('code', $data['webform_invitation_code']);
$query
->execute();
}
}
}
Functions
Name | Description |
---|---|
webform_invitation_code_validate | Invitation Code element validation. |
webform_invitation_help | Implements hook_help(). |
webform_invitation_webform_submission_form_alter | Implements hook_webform_submission_form_alter(). |
webform_invitation_webform_submission_insert | Implements hook_ENTITY_TYPE_insert(). |