You are here

protected function WebformSubmissionForm::validateUploadedManagedFiles in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionForm.php \Drupal\webform\WebformSubmissionForm::validateUploadedManagedFiles()

Validate uploaded managed file limits.

Parameters

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

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

See also

\Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::validateManagedFileLimit

1 call to WebformSubmissionForm::validateUploadedManagedFiles()
WebformSubmissionForm::validateForm in src/WebformSubmissionForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…

File

src/WebformSubmissionForm.php, line 1981

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function validateUploadedManagedFiles(array $form, FormStateInterface $form_state) {
  $file_limit = $this
    ->getWebform()
    ->getSetting('form_file_limit') ?: $this->configFactory
    ->get('webform.settings')
    ->get('settings.default_form_file_limit') ?: '';
  $file_limit = Bytes::toInt($file_limit);
  if (!$file_limit) {
    return;
  }

  // Validate file upload limit.
  $fids = $this
    ->getUploadedManagedFileIds();
  if (!$fids) {
    return;
  }
  $file_names = [];
  $total_file_size = 0;

  /** @var \Drupal\file\FileInterface[] $files */
  $files = $this->entityTypeManager
    ->getStorage('file')
    ->loadMultiple($fids);
  foreach ($files as $file) {
    $total_file_size += (int) $file
      ->getSize();
    $file_names[] = $file
      ->getFilename() . ' - ' . format_size($file
      ->getSize(), $this->entity
      ->language()
      ->getId());
  }
  if ($total_file_size > $file_limit) {
    $t_args = [
      '%quota' => format_size($file_limit),
    ];
    $message = [];
    $message['content'] = [
      '#markup' => $this
        ->t("This form's file upload quota of %quota has been exceeded. Please remove some files.", $t_args),
    ];
    $message['files'] = [
      '#theme' => 'item_list',
      '#items' => $file_names,
    ];
    $form_state
      ->setErrorByName(NULL, $this->renderer
      ->renderPlain($message));
  }
}