0

I am using file_managed field having multiple set to true. I am saving the submitted files in database table in bytea format.

buildform :

$form['test_pdf_1'] = array(
            '#title' => t('Upload Field 1(PDF only)'),
            '#type' => 'managed_file',
            '#multiple' => TRUE,
            '#upload_validators' => array('file_validate_extensions' => array('pdf'), 'file_validate_size' => array(52428800)),
            '#upload_location' => 'public://node_files/',
        );

submitForm :

if(!empty($form_state->getValue('test_pdf_1'))){
            $test_pdf_1 = $form_state->getValue('test_pdf_1');
            foreach($form_state->getValue('test_pdf_1') as $test_pdf_1_val){
                array_push($to_delete_fids,$test_pdf_1_val);
                $test_pdf_1_file = \Drupal\file\Entity\File::load($test_pdf_1_val);
                $test_pdf_1_uri = $test_pdf_1_file->getFileUri();
                $test_pdf_1_contents = file_get_contents($test_pdf_1_uri);
                $test_pdf_1_file_name = $test_pdf_1_file->getFilename();
                $push_array[] = array('boiler_req_id' => $form_state->getValue('boiler_req_id'), 'type' => $form_state->getValue('type'),'field_no' => 1, 'file_content' => $test_pdf_1_contents,'file_name' => $test_pdf_1_file_name);
                file_delete($test_pdf_1_val);   
            }
        }


foreach($push_array as $val){
            $sql = \Drupal::database()->insert('node_transform')
                ->fields($val)
                ->execute();    
        }

Using the above code File content is successfully saved in table along with filename. The physical file is removed from its storage location and the file_managed entry is also deleted.

Now when regenerating the file_managed field using file_save_data function the files are generated and shown properly, but the remove selected functionality is not working on the generated file. Whereas is new file is uploaded again, remove selected is working fine on the newly uploaded file.

Db query with file fields :

$results = $query->execute()->fetchAll();
            foreach($results as $val){
                $fileContent = $val->file_content; 
                $destination = 'public://node_files/'.$val->file_name;
                $file = file_save_data(
                    $fileContent,
                    $destination,
                    FILE_EXISTS_REPLACE
                );
                $file->setTemporary();
                $file->save();
array_push($f1,$file->id());

File managed Field regenerated :

$form['test_pdf_1'] = array(
            '#title' => t('Upload Field 1(PDF only)'),
            '#type' => 'managed_file',
            '#multiple' => TRUE,
            '#upload_validators' => array('file_validate_extensions' => array('pdf'), 'file_validate_size' => array(52428800)),
            '#upload_location' => 'public://node_files/',
            '#default_value' => (!empty($f1)) ? $f1 : array(),
        );

What am i missing? The remove ajax is not working on regenerated file but working fine on new files if uploaded. Please suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.