class WebformProtectedDownloadsHandler in Webform Protected Downloads 8
Handler for protected downloads
Plugin annotation
@WebformHandler(
id = "webform_protected_downloads",
label = @Translation("Webform Protected Downloads (DO NOT USE)"),
category = @Translation("Downloads"),
description = @Translation("Provides an access controlled link to a file/page of files."),
cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED,
submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\webform\Plugin\WebformHandlerBase implements WebformHandlerInterface uses WebformPluginSettingsTrait
- class \Drupal\webform_protected_downloads\Plugin\WebformHandler\WebformProtectedDownloadsHandler
- class \Drupal\webform\Plugin\WebformHandlerBase implements WebformHandlerInterface uses WebformPluginSettingsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformProtectedDownloadsHandler
File
- src/
Plugin/ WebformHandler/ WebformProtectedDownloadsHandler.php, line 30
Namespace
Drupal\webform_protected_downloads\Plugin\WebformHandlerView source
class WebformProtectedDownloadsHandler extends WebformHandlerBase {
/**
* @var FileStorageInterface
*/
protected $fileStorage;
/**
* @var FileUsageInterface
*/
protected $fileUsage;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$entityTypeManager = $container
->get('entity_type.manager');
$instance->fileStorage = $entityTypeManager
->getStorage('file');
$instance->fileUsage = $container
->get('file.usage');
return $instance;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'expiration_one_time' => false,
'expiration_time' => 0,
'expiration_page' => 'page_reload',
'expiration_page_custom' => '',
'expiration_error_message' => 'This link has expired.',
'token_text' => 'Download',
'protected_files' => [],
'protected_files_allowed_extensions' => 'gif png jpg jpeg',
'debug' => false,
];
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$configuration = $this
->getConfiguration();
$settings = $configuration['settings'];
return [
'#settings' => $settings,
] + parent::getSummary();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Expiration
$form['expiration'] = [
'#type' => 'details',
'#title' => $this
->t('Expiration'),
'#open' => TRUE,
];
$form['expiration']['expiration_onetime'] = [
'#type' => 'checkbox',
'#title' => $this
->t('One time visit link'),
'#default_value' => $this->configuration['expiration_onetime'],
];
$form['expiration']['expiration_time'] = [
'#type' => 'number',
'#title' => $this
->t('Expire after X minutes'),
'#default_value' => $this->configuration['expiration_time'],
'#required' => TRUE,
'#description' => 'Set this to 0 for unlimited.',
];
$options = [
'404' => $this
->t('404 page'),
'homepage' => $this
->t('Homepage with error message'),
'page_reload' => $this
->t('Form page with error message'),
'custom' => $this
->t('Custom page'),
];
$form['expiration']['expiration_page'] = [
'#type' => 'radios',
'#title' => $this
->t('Link expired page'),
'#required' => TRUE,
'#description' => t('Select a page to be routed when link expires.'),
'#options' => $options,
'#default_value' => $this->configuration['expiration_page'],
'#attributes' => [
'name' => 'field_expiration_page',
],
];
$form['expiration']['expiration_page_custom'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom link page'),
'#options' => $options,
'#default_value' => $this->configuration['expiration_page_custom'],
'#states' => [
'visible' => [
':input[name="field_expiration_page"]' => [
'value' => 'custom',
],
],
'required' => [
':input[name="field_expiration_page"]' => [
'value' => 'custom',
],
],
],
];
$form['expiration']['expiration_error_message'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Error message'),
'#description' => $this
->t('Error message to display.'),
'#default_value' => $this->configuration['expiration_error_message'],
'#states' => [
'visible' => [
':input[name="field_expiration_page"]' => [
[
'value' => 'homepage',
],
[
'value' => 'page_reload',
],
],
],
'required' => [
':input[name="field_expiration_page"]' => [
[
'value' => 'homepage',
],
[
'value' => 'page_reload',
],
],
],
],
);
// Tokens
$form['token'] = [
'#type' => 'details',
'#title' => $this
->t('Tokens'),
'#open' => TRUE,
];
$form['token']['token_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Token text title'),
'#description' => $this
->t('This title will be shown when token is replaced, default title is Download'),
'#default_value' => $this->configuration['token_text'],
];
// Files
$form['protected'] = [
'#type' => 'details',
'#title' => $this
->t('Protected Files'),
'#open' => TRUE,
];
$form['protected']['protected_files'] = [
'#name' => 'protected_files',
'#type' => 'managed_file',
'#title' => $this
->t('Choose file(s) for protected download'),
'#multiple' => TRUE,
'#upload_validators' => [
'file_validate_extensions' => $this->configuration['protected_files_allowed_extensions'],
],
'#upload_location' => 'private://webform_protected_downloads/[date:custom:Y]-[date:custom:m]',
'#default_value' => $this->configuration['protected_files'],
];
$form['protected']['protected_files_allowed_extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Valid File extensions'),
'#description' => $this
->t("Separate extensions with a space."),
'#default_value' => $this->configuration['protected_files_allowed_extensions'],
'#required' => TRUE,
];
// Development.
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
'#open' => true,
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, debugging will be displayed onscreen to all users.'),
'#return_value' => true,
'#default_value' => $this->configuration['debug'],
];
return $this
->setSettingsParents($form);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration = $this
->defaultConfiguration();
parent::submitConfigurationForm($form, $form_state);
$this
->applyFormStateToConfiguration($form_state);
// Set the files as permanent through file usage
$protectedFiles = $form_state
->getValue('protected_files');
foreach ($protectedFiles as $protectedFile) {
/** @var FileInterface $file */
$file = $this->fileStorage
->load($protectedFile);
if (!empty($file)) {
$this->fileUsage
->add($file, 'webform_protected_downloads', 'webform', $this
->getWebform()
->id());
}
}
}
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
// only act on the insert
if (!$update) {
return;
}
// only process if there are files added to the configuration
if (count($this->configuration['protected_files']) > 0) {
return;
}
$expiration = 0;
if ($this->configuration['expiration_time'] > 0) {
$expiration = time() + $this->configuration['expiration_time'] * 60;
}
// TODO
// $webformProtectedDownload = WebformProtectedDownloads::create([
// 'webform_submission' => $webform_submission,
// 'files' => $this->con,
// 'hash' => Crypt::hashBase64($webform_submission->uuid() . time()),
// 'active' => TRUE,
// 'expire' => $expiration,
// 'onetime' => $this->configuration['expiration_onetime'],
// ]);
//
// $webformProtectedDownload->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformHandlerBase:: |
protected | property | The webform handler's conditions. | |
WebformHandlerBase:: |
protected | property | The webform handler's conditions result cache. | |
WebformHandlerBase:: |
protected | property | The webform submission (server-side) conditions (#states) validator. | |
WebformHandlerBase:: |
protected | property | The configuration factory. | 1 |
WebformHandlerBase:: |
protected | property | The webform handler ID. | |
WebformHandlerBase:: |
protected | property | The webform handler label. | |
WebformHandlerBase:: |
protected | property | The logger factory. | |
WebformHandlerBase:: |
protected | property | The webform variant notes. | |
WebformHandlerBase:: |
protected | property | The webform handler status. | |
WebformHandlerBase:: |
protected | property | The webform submission storage. | |
WebformHandlerBase:: |
protected | property | The webform token manager. | 6 |
WebformHandlerBase:: |
protected | property | The webform. | |
WebformHandlerBase:: |
protected | property | The webform submission. | |
WebformHandlerBase:: |
protected | property | The weight of the webform handler. | |
WebformHandlerBase:: |
public | function |
Controls entity operation access to webform submission. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Controls entity operation access to webform submission element. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Alter webform element. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Alter webform submission webform elements. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Alter webform submission form. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
protected | function | Apply submitted form state to configuration. | |
WebformHandlerBase:: |
protected | function | Build token tree element. | 2 |
WebformHandlerBase:: |
public | function |
Returns the webform handler cardinality settings. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Check handler conditions against a webform submission. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Confirm webform submission form. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a element after it has been created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been created and added to webform. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a element after it has been deleted. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been removed. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Returns the webform handler description. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Disables the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Validate form that should have tokens in it. | |
WebformHandlerBase:: |
public | function |
Enables the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the conditions the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the unique ID representing the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the label of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Get webform or webform_submission logger. | |
WebformHandlerBase:: |
public | function |
Returns notes of the webform variant. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get configuration form's off-canvas width. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Returns the status of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get the webform that this handler is attached to. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Get the webform submission that this handler is handling. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the weight of the webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Determine if the webform handler requires anonymous submission tracking. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Determine if this handle is applicable to the webform. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler disabled indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler enabled indicator. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Checks if the handler is excluded via webform.settings. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform submission is optional indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform submission is required indicator. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Returns the webform handler label. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Log a webform handler's submission operation. | |
WebformHandlerBase:: |
public | function |
Alter/override a webform submission webform settings. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission after it is created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on deleted a webform submission before the delete hook is invoked. Overrides WebformHandlerInterface:: |
4 |
WebformHandlerBase:: |
public | function |
Acts on loaded webform submission. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on webform submissions after they are purged. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Changes the values of an entity before it is created. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission before they are deleted and before hooks are invoked. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on an webform submission about to be shown on a webform submission form. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Prepares variables for webform confirmation templates. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on webform submissions before they are purged. Overrides WebformHandlerInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Acts on a webform submission before the presave hook is invoked. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
protected | function | Replace tokens in text with no render context. | |
WebformHandlerBase:: |
public | function |
Sets the conditions for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
WebformHandlerBase:: |
public | function |
Sets the id for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the label for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set notes for this webform variant. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
protected | function | Set configuration settings parents. | |
WebformHandlerBase:: |
protected | function | Set configuration settings parents. | |
WebformHandlerBase:: |
public | function |
Sets the status for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set the webform that this is handler is attached to. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Set the webform submission that this handler is handling. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Sets the weight for this webform handler. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Submit webform submission form. Overrides WebformHandlerInterface:: |
4 |
WebformHandlerBase:: |
public | function |
Determine if webform handler supports conditions. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Determine if webform handler supports tokens. Overrides WebformHandlerInterface:: |
|
WebformHandlerBase:: |
public | function |
Acts on a element after it has been updated. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Acts on handler after it has been updated. Overrides WebformHandlerInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
3 |
WebformHandlerBase:: |
public | function |
Validate webform submission form. Overrides WebformHandlerInterface:: |
2 |
WebformHandlerBase:: |
public | function |
Constructs a WebformHandlerBase object. Overrides PluginBase:: |
7 |
WebformHandlerInterface:: |
constant | Value indicating a single plugin instances are permitted. | ||
WebformHandlerInterface:: |
constant | Value indicating unlimited plugin instances are permitted. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions are not processed (i.e. email or saved) by the handler. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions are processed (i.e. email or saved) by the handler. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions do not have to be stored in the database. | ||
WebformHandlerInterface:: |
constant | Value indicating webform submissions must be stored in the database. | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformProtectedDownloadsHandler:: |
protected | property | ||
WebformProtectedDownloadsHandler:: |
protected | property | ||
WebformProtectedDownloadsHandler:: |
public | function |
Form constructor. Overrides WebformHandlerBase:: |
|
WebformProtectedDownloadsHandler:: |
public static | function |
Creates an instance of the plugin. Overrides WebformHandlerBase:: |
|
WebformProtectedDownloadsHandler:: |
public | function |
Gets default configuration for this plugin. Overrides WebformHandlerBase:: |
|
WebformProtectedDownloadsHandler:: |
public | function |
Returns a render array summarizing the configuration of the webform handler. Overrides WebformHandlerBase:: |
|
WebformProtectedDownloadsHandler:: |
public | function |
Acts on a saved webform submission before the insert or update hook is invoked. Overrides WebformHandlerBase:: |
|
WebformProtectedDownloadsHandler:: |
public | function |
Form submission handler. Overrides WebformHandlerBase:: |