You are here

public function WebformProtectedDownloadsSettingsForm::submitForm in Webform Protected Downloads 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/WebformProtectedDownloadsSettingsForm.php, line 182

Class

WebformProtectedDownloadsSettingsForm
Class WebformProtectedDownloadsSettingsForm.

Namespace

Drupal\webform_protected_downloads\Form

Code

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.'));
}