View source
<?php
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\webform_protected_downloads\Entity\WebformProtectedDownloads;
use Drupal\file\Entity\File;
function webform_protected_downloads_theme() {
return [
'webform_protected_downloads_token' => [
'variables' => [
'link' => '',
],
],
'webform_handler_webform_protected_downloads_summary' => [
'variables' => [
'settings' => NULL,
'handler' => [],
],
],
];
}
function webform_protected_downloads_webform_submission_insert($submission) {
$webform = $submission
->getWebform();
$protected_downloads_settings = $webform
->getThirdPartySettings('webform_protected_downloads');
if (!isset($protected_downloads_settings['enabled_protected_files']) || !$protected_downloads_settings['enabled_protected_files']) {
return;
}
elseif (!isset($protected_downloads_settings['protected_file']) || empty($protected_downloads_settings['protected_file'])) {
return;
}
WebformProtectedDownloads::create([
'webform_submission' => $submission,
'file' => File::load(current($protected_downloads_settings['protected_file'])),
'hash' => Crypt::hashBase64($submission
->id() . time()),
'active' => $protected_downloads_settings['enabled_protected_files'],
'expire' => !$protected_downloads_settings['expire_after'] ? 0 : time() + $protected_downloads_settings['expire_after'] * 60,
'onetime' => $protected_downloads_settings['enabled_onetime'],
])
->save();
}
function webform_protected_downloads_token_info() {
$submission['protected_download_url'] = array(
'name' => t("Protected download link"),
'description' => t("Download link for webform protected downloads."),
);
return array(
'tokens' => array(
'webform_submission' => $submission,
),
);
}
function webform_protected_downloads_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if (isset($tokens['protected_download_url']) && isset($data['entity'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'protected_download_url':
$webformSubmission = $data['entity'];
$webformProtectedDownloadsManager = \Drupal::service('webform_protected_downloads.manager');
$webformProtectedDownload = $webformProtectedDownloadsManager
->getSubmissionByUuid($webformSubmission
->uuid());
$hash = $webformProtectedDownload
->getHash();
if (is_null($hash)) {
$replacements[$original] = t("Download link not available");
return $replacements;
}
$webform = $webformSubmission
->getWebform();
$webformSettings = $webform
->getThirdPartySettings('webform_protected_downloads');
$url = Url::fromRoute('webform_protected_downloads.download', [
'hash' => $hash,
], [
'absolute' => TRUE,
]);
$link = Link::fromTextAndUrl(t($webformSettings['token_text']), $url)
->toString();
$element = [
'#theme' => 'webform_protected_downloads_token',
'#link' => $link,
];
$link = \Drupal::service('renderer')
->render($element);
$replacements[$original] = $link;
break;
}
}
}
return $replacements;
}