class WebformProtectedDownloadsSettingsForm in Webform Protected Downloads 8
Class WebformProtectedDownloadsSettingsForm.
@package Drupal\webform_protected_downloads\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait- class \Drupal\webform_protected_downloads\Form\WebformProtectedDownloadsSettingsForm
 
Expanded class hierarchy of WebformProtectedDownloadsSettingsForm
1 string reference to 'WebformProtectedDownloadsSettingsForm'
File
- src/Form/ WebformProtectedDownloadsSettingsForm.php, line 17 
Namespace
Drupal\webform_protected_downloads\FormView source
class WebformProtectedDownloadsSettingsForm extends FormBase {
  /**
   * CurrentRouteMatch definition.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $routeMatch;
  /**
   * FileSystem definition.
   *
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;
  /**
   * WebformProtectedDownloadsSettingsForm constructor.
   *
   * @param \Drupal\Core\Routing\CurrentRouteMatch $routeMatch
   *   The route match.
   * @param \Drupal\Core\File\FileSystem $fileSystem
   *   The file system.
   */
  public function __construct(CurrentRouteMatch $routeMatch, FileSystem $fileSystem) {
    $this->routeMatch = $routeMatch;
    $this->fileSystem = $fileSystem;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_route_match'), $container
      ->get('file_system'));
  }
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'webform_protected_downloads_settings_form';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Get current webform .
    $webform = $this->routeMatch
      ->getParameter('webform');
    // Get form settings.
    $webform_settings = $webform
      ->getThirdPartySettings('webform_protected_downloads');
    $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['fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Webform protected files settings'),
    ];
    // Create the form.
    $form['fieldset']['enabled_protected_files'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable serving protected files after webform submit - <b>Must be checked for other options to work</b>'),
      '#default_value' => isset($webform_settings['enabled_protected_files']) ? $webform_settings['enabled_protected_files'] : FALSE,
    ];
    $form['fieldset']['container'] = [
      '#type' => 'container',
      '#states' => [
        'invisible' => [
          'input[name="enabled_protected_files"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['fieldset']['container']['expire_after'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Expire after X minutes'),
      '#default_value' => isset($webform_settings['expire_after']) ? $webform_settings['expire_after'] : '',
    ];
    $form['fieldset']['container']['enabled_onetime'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('One time visit link'),
      '#default_value' => isset($webform_settings['enabled_onetime']) ? $webform_settings['enabled_onetime'] : FALSE,
    ];
    $form['fieldset']['container']['expired_link_page'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Link expired page'),
      '#description' => t('Select a page to be routed when link expires.'),
      '#options' => $options,
      '#default_value' => isset($webform_settings['expired_link_page']) ? $webform_settings['expired_link_page'] : FALSE,
    ];
    $form['fieldset']['container']['custom_link_page'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom link page'),
      '#states' => array(
        'visible' => array(
          ':input[name="expired_link_page"]' => array(
            'value' => 'custom',
          ),
        ),
      ),
      '#default_value' => isset($webform_settings['custom_link_page']) ? $webform_settings['custom_link_page'] : '',
    );
    $defaultTokenText = 'Download file';
    $form['fieldset']['container']['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 @default', [
        '@default' => $defaultTokenText,
      ]),
      '#default_value' => isset($webform_settings['token_text']) ? $webform_settings['token_text'] : $defaultTokenText,
    ];
    $form['fieldset']['container']['error_message'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Error message'),
      '#description' => $this
        ->t('Error message to display.'),
      '#default_value' => $this
        ->t('File not found'),
    );
    $form['fieldset']['container']['protected_file'] = [
      '#name' => 'protected_file',
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Choose a file for protected download'),
      '#multiple' => FALSE,
      '#theme_wrappers' => [],
      '#upload_validators' => [
        'file_validate_extensions' => isset($webform_settings['protected_file_extensions']) ? [
          $webform_settings['protected_file_extensions'],
        ] : [
          'gif png jpg jpeg',
        ],
      ],
      '#error_no_message' => TRUE,
      '#upload_location' => 'private://webform_protected_downloads/',
      '#default_value' => isset($webform_settings['protected_file']) ? $webform_settings['protected_file'] : NULL,
    ];
    $form['fieldset']['container']['protected_file_extensions'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Valid File extensions'),
      '#description' => $this
        ->t("Seperate extensions with ,"),
      '#default_value' => isset($webform_settings['protected_file_extensions']) ? $webform_settings['protected_file_extensions'] : '',
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
    ];
    // Print an error if private folder is not set.
    $private_folder = $this->fileSystem
      ->realpath('private://');
    if (!$private_folder) {
      $this
        ->messenger()
        ->addError($this
        ->t('Private files folder is not set! Please setup private folder to use this module correctly.'));
    }
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // @todo Implement some form checks!
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Get current webform .
    $webform = $this->routeMatch
      ->getParameter('webform');
    // Save/update settings.
    $values = $form_state
      ->getValues();
    foreach ($values as $key => $value) {
      if ($key == 'submit' || $key == 'op') {
        continue;
      }
      elseif ($key == 'protected_file_extensions') {
        // Remove white spaces and replace , white whitespace.
        trim($value);
        $value = str_replace(',', ' ', $value);
      }
      $webform
        ->setThirdPartySetting("webform_protected_downloads", $key, $value);
    }
    // Set file status to TRUE or file will get deleted after cron.
    if ($values['protected_file']) {
      $fileId = current($values['protected_file']);
      File::load($fileId)
        ->set('status', TRUE)
        ->save();
    }
    if ($values['enabled_protected_files'] == 0) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Make sure to also remove webform protected downloads token instances after disabling this.'));
    }
    $webform
      ->save();
    $this
      ->messenger()
      ->addStatus($this
      ->t('Settings saved.'));
  }
}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 | |
| FormBase:: | protected | property | The config factory. | 1 | 
| FormBase:: | protected | property | The request stack. | 1 | 
| FormBase:: | protected | function | Retrieves a configuration object. | |
| FormBase:: | protected | function | Gets the config factory for this form. | 1 | 
| FormBase:: | private | function | Returns the service container. | |
| FormBase:: | protected | function | Gets the current user. | |
| FormBase:: | protected | function | Gets the request object. | |
| FormBase:: | protected | function | Gets the route match. | |
| FormBase:: | protected | function | Gets the logger for a specific channel. | |
| FormBase:: | protected | function | Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | |
| FormBase:: | public | function | Resets the configuration factory. | |
| FormBase:: | public | function | Sets the config factory for this form. | |
| FormBase:: | public | function | Sets the request stack object to use. | |
| LinkGeneratorTrait:: | protected | property | The link generator. | 1 | 
| LinkGeneratorTrait:: | protected | function | Returns the link generator. | |
| LinkGeneratorTrait:: | protected | function | Renders a link to a route given a route name and its parameters. | |
| LinkGeneratorTrait:: | public | function | Sets the link generator service. | |
| LoggerChannelTrait:: | protected | property | The logger channel factory service. | |
| LoggerChannelTrait:: | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait:: | public | function | Injects the logger channel factory. | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| RedirectDestinationTrait:: | protected | property | The redirect destination service. | 1 | 
| RedirectDestinationTrait:: | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait:: | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait:: | public | function | Sets the redirect destination service. | |
| 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. | |
| UrlGeneratorTrait:: | protected | property | The url generator. | |
| UrlGeneratorTrait:: | protected | function | Returns the URL generator service. | |
| UrlGeneratorTrait:: | public | function | Sets the URL generator service. | |
| UrlGeneratorTrait:: | protected | function | Generates a URL or path for a specific route based on the given parameters. | |
| WebformProtectedDownloadsSettingsForm:: | protected | property | FileSystem definition. | |
| WebformProtectedDownloadsSettingsForm:: | protected | property | CurrentRouteMatch definition. Overrides FormBase:: | |
| WebformProtectedDownloadsSettingsForm:: | public | function | Form constructor. Overrides FormInterface:: | |
| WebformProtectedDownloadsSettingsForm:: | public static | function | Instantiates a new instance of this class. Overrides FormBase:: | |
| WebformProtectedDownloadsSettingsForm:: | public | function | Returns a unique string identifying the form. Overrides FormInterface:: | |
| WebformProtectedDownloadsSettingsForm:: | public | function | Form submission handler. Overrides FormInterface:: | |
| WebformProtectedDownloadsSettingsForm:: | public | function | Form validation handler. Overrides FormBase:: | |
| WebformProtectedDownloadsSettingsForm:: | public | function | WebformProtectedDownloadsSettingsForm constructor. | 
