You are here

public function FileUploadForm::buildForm in Ubercart 8.4

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

uc_file/src/Form/FileUploadForm.php, line 87

Class

FileUploadForm
Performs a file upload action.

Namespace

Drupal\uc_file\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Calculate the maximum size of uploaded files in bytes.
  $post_max_size = ini_get('post_max_size');
  if (is_numeric($post_max_size)) {

    // Handle the case where 'post_max_size' has no suffix.
    // An explicit cast is needed because floats are not allowed.
    $max_bytes = (int) $post_max_size;
  }
  else {

    // Handle the case where 'post_max_size' has a suffix of
    // 'M', 'K', or 'G' (case insensitive).
    $max_bytes = (int) substr($post_max_size, 0, -1);
    $suffix = strtolower(substr($post_max_size, -1));
    switch ($suffix) {
      case 'k':
        $max_bytes *= 1024;
        break;
      case 'm':
        $max_bytes *= 1048576;
        break;
      case 'g':
        $max_bytes *= 1073741824;
        break;
    }
  }

  // Gather list of directories under the selected one(s).
  // '/' is always available.
  $directories = [
    '' => '/',
  ];
  $files = \Drupal::database()
    ->query('SELECT * FROM {uc_files}');
  foreach ($files as $file) {
    if (is_dir($this
      ->config('uc_file.settings')
      ->get('base_dir') . '/' . $file->filename)) {
      $directories[$file->filename] = $file->filename;
    }
  }
  $form['upload_dir'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Directory'),
    '#description' => $this
      ->t('The directory on the server where the file should be put. The default directory is the root of the file downloads directory.'),
    '#options' => $directories,
  ];
  $form['upload'] = [
    '#type' => 'file',
    '#title' => $this
      ->t('File'),
    '#multiple' => TRUE,
    '#description' => $this
      ->t("You may select more than one file by holding down the Cntrl key when you click the file name. The maximum file size that can be uploaded is %size bytes. You will need to use a different method to upload the file to the directory (e.g. (S)FTP, SCP) if your file exceeds this size. Files you upload using one of these alternate methods will be automatically detected. Note: A value of '0' means there is no size limit.", [
      '%size' => number_format($max_bytes),
    ]),
  ];

  // $form['#attributes']['class'][] = 'foo';
  // $form['#attributes']['enctype'] = 'multipart/form-data';
  return parent::buildForm($form, $form_state);
}