You are here

public function S3fsCorsImageWidget::settingsMaxFilesizeValidate in S3 File System CORS Upload 8

Validate the submiited value for max filesize.

File

src/Plugin/Field/FieldWidget/S3fsCorsImageWidget.php, line 59

Class

S3fsCorsImageWidget
Plugin implementation of the 's3fs_cors_widget' widget.

Namespace

Drupal\s3fs_cors\Plugin\Field\FieldWidget

Code

public function settingsMaxFilesizeValidate($element, FormStateInterface $form_state) {
  $submitted_value = $form_state
    ->getValue($element['#parents']);
  if ($submitted_value) {
    $matches = [];
    if (preg_match('/(\\d+)(.*)/', $submitted_value, $matches)) {
      $suffix = strtoupper(trim($matches[2]));
      if ($suffix && !in_array($suffix, [
        'B',
        'KB',
        'MB',
        'GB',
      ])) {
        $form_state
          ->setError($element, $this
          ->t('Invalid numeric value or size suffix. Specify an integer followed by "KB", "MB" or "GB".'));
        return;
      }
    }
    $max_filesize = Bytes::toInt($submitted_value);
    if (!$max_filesize) {
      $form_state
        ->setError($element, $this
        ->t('Invalid max filesize. Enter a value like "512" (bytes), "80 KB" (kilobytes), "50 MB" (megabytes) or "2 GB" (gigabytes).'));
    }
    if ($max_filesize > Bytes::toInt('5 GB')) {
      $form_state
        ->setError($element, $this
        ->t('Invalid max filesize. 5 GB is largest file size current supported.'));
    }
  }
}