You are here

public function UploadForm::buildForm in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/UploadForm.php \Drupal\filebrowser\Form\UploadForm::buildForm()

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 FormInterface::buildForm

File

src/Form/UploadForm.php, line 47

Class

UploadForm

Namespace

Drupal\filebrowser\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $nid = null, $query_fid = null, $fids = null, $ajax = null) {
  $this->common = \Drupal::service('filebrowser.common');
  $this->relativeRoot = $this->common
    ->relativePath($query_fid);
  $this->node = Node::load($nid);
  $this->queryFid = $query_fid;
  $this->nid = $nid;
  $accepted = $this->node->filebrowser->accepted;

  // if this form is opened by ajax add a close link.
  if ($ajax) {
    $form['#attributes'] = [
      'class' => [
        'form-in-slide-down',
      ],
    ];
    $form['close'] = $this->common
      ->closeButtonMarkup();
  }

  // Set upload location, Replace "//" with "/" when needed.
  if ($this->node->filebrowser->folderPath && $this->relativeRoot) {
    $upload_location = preg_replace('/\\/\\/$/', '/', $this->node->filebrowser->folderPath) . $this->relativeRoot;
  }
  else {
    $upload_location = $this->node->filebrowser->folderPath . $this->relativeRoot;
  }
  $form['u_file'] = [
    '#title' => $this
      ->t('Upload file'),
    '#type' => 'filebrowser_managed_file',
    '#description' => $this
      ->t('File types accepted: @accepted', [
      '@accepted' => $accepted,
    ]) . '<br>' . $this
      ->t('You can upload multiple files.'),
    '#upload_validators' => [
      'file_validate_extensions' => [
        $this->node->filebrowser->accepted,
      ],
    ],
    '#upload_location' => $upload_location,
    '#progress_indicator' => 'bar',
    '#progress_message' => $this
      ->t('Please wait...'),
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save Upload'),
  ];
  return $form;
}