I want to upload a file and I have to use form element type as 'file'. In submit function I am not able to get any proper data related to the file which I am uploading. SO Can anyone please help me to resolve this. Please find the below code:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['test_CERTIFICATE'] = [
'#type' => 'file',
'#title' => $this->t('Certificate'),
'#description' => $this->t('Your Certificate (.pem file)').': '.\Drupal::state()->get('test_CERTIFICATE_NAME'),
];
return parent::buildForm($form, $form_state);
}
Submit function:
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$validators = array('file_validate_extensions' => array('pem'));
$files = file_save_upload('test_CERTIFICATE', $validators, 'public://certfiles', FILE_EXISTS_REPLACE);
$file = File::load($files[0]);
if ($file) {
kint($files); exit;
// here control is not coming
$file->setPermanent();
$file->save();
}
}