Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fazni/79632cf51a51296bf5d3d4fa4964c860 to your computer and use it in GitHub Desktop.
Save fazni/79632cf51a51296bf5d3d4fa4964c860 to your computer and use it in GitHub Desktop.
$form['file'] = [
'#type' => 'managed_file',
'#title' => t('File'),
'#upload_location' => 'public://file',
'#upload_validators' => [
'file_validate_extensions' => ['csv'],
],
'#default_value' => array(2)
];
Handle the saving the file as a permanent file into the file_managed table. So add this to your submit handler.
use Drupal\file\Entity\File;
$fid = $form_state->getValue(['file', 0]);
if (!empty($fid)) {
$file = File::load($fid);
$file->setPermanent();
$file->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment