0

I have the following custom submit handler.

function example_form_submit(&$form, FormStateInterface $form_state) {
    $file = file_load($form_state['values']['file_fid']);//This works in 7
}

Now I want to have uploaded file information like fid, name etc. in D8. I am not able to get that.

UPDATE: I have also tried using the following code from here but no luck.

if ($form_state->hasFileElement())
{
  drupal_set_message("This form has a file element.");
  $video_file_file_array = $form_state->getValue('video_file');
  if (is_array($video_file_file_array))
  {
    if (isset($video_file_file_array[0]))
    {
      $video_file_file_id = $video_file_file_array[0];
      $file = \Drupal\file\Entity\File::load($video_file_file_id);
      if ($file != NULL)
      {
        $filename = $file->getFilename();
        drupal_set_message('Filename: ' . $filename);
      }
    }
  }
}
1

You need to use following code to get it

$target_id =$form_state->getValue('field_name');//get the target_id from it 
    $file = \Drupal\file\Entity\File::load($target_id); //load the file and its attribute.
            $file_image= '<img src = "' . \Drupal\image\Entity\ImageStyle::load('large')->buildUrl($file->getFileUri()) . '" />';//load the large image 
0

For filename you can use getFilename()

$file->getFilename()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.