How do I save a uploaded file with status equal to 1 in the file_managed table, in Drupal 8?
Whenever I upload a file, it is stored in the file_managed table with status value 0.
I have used File::load( $form_state->getValue('image'))
to load the file. What do I need to do next?
In Drupal 7, I would use $file->status = FILE_STATUS_PERMANENT
. What is the equivalent code for Drupal 8?
class AddBannerForm extends FormBase {
public function getFormId() {
return 'add_banner_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['image'] = [
'#type' => 'managed_file',
'#title' => t('Choose Image File'),
'#upload_location' => 'public://images/',
'#default_value' => '',
'#description' => t('Specify an image(s) to display.'),
'#states' => [
'visible' => [
':input[name="image_type"]' => ['value' => t('Upload New Image(s)')],
],
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save image'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
File::load($form_state->getValue('image'));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}