Skip to main content

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'private://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],
];

Then in submit:

use Drupal\file\Entity\File;

$fid$form_file = $form_state->getValue(['test_CERTIFICATE''test_CERTIFICATE', 0]0);
if (isset($form_file[0]) && !empty($fid$form_file[0])) {
  $file = File::load($fid$form_file[0]);
  $file->setPermanent();
  $file->save();
}

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'private://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],
];

Then in submit:

use Drupal\file\Entity\File;

$fid = $form_state->getValue(['test_CERTIFICATE', 0]);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save();
}

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'private://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],
];

Then in submit:

use Drupal\file\Entity\File;

$form_file = $form_state->getValue('test_CERTIFICATE', 0);
if (isset($form_file[0]) && !empty($form_file[0])) {
  $file = File::load($form_file[0]);
  $file->setPermanent();
  $file->save();
}

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'public'private://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],
];

Then in submit:

use Drupal\file\Entity\File;

  $fid = $form_state->getValue(['test_CERTIFICATE', 0]);
  if (!empty($fid)) {
    $file = File::load($fid);
    $file->setPermanent();
    $file->save();
  }

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'public://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],

Then in submit:

use Drupal\file\Entity\File;

  $fid = $form_state->getValue(['test_CERTIFICATE', 0]);
  if (!empty($fid)) {
    $file = File::load($fid);
    $file->setPermanent();
    $file->save();
  }

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'private://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],
];

Then in submit:

use Drupal\file\Entity\File;

$fid = $form_state->getValue(['test_CERTIFICATE', 0]);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save();
}

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['BUCKAROO_CERTIFICATE']$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'public://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],

Then in submit:

use Drupal\file\Entity\File;

  $fid = $form_state->getValue(['BUCKAROO_CERTIFICATE'['test_CERTIFICATE', 0]);
  if (!empty($fid)) {
    $file = File::load($fid);
    $file->setPermanent();
    $file->save();
  }

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['BUCKAROO_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'public://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],

Then in submit:

use Drupal\file\Entity\File;

  $fid = $form_state->getValue(['BUCKAROO_CERTIFICATE', 0]);
  if (!empty($fid)) {
    $file = File::load($fid);
    $file->setPermanent();
    $file->save();
  }

I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' => 'managed_file',
  '#title' => $this->t('Certificate'),
  '#upload_location' => 'public://certfiles',
  '#upload_validators' => [
    'file_validate_extensions' => ['pem'],
  ],

Then in submit:

use Drupal\file\Entity\File;

  $fid = $form_state->getValue(['test_CERTIFICATE', 0]);
  if (!empty($fid)) {
    $file = File::load($fid);
    $file->setPermanent();
    $file->save();
  }
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217
Loading