如何在drupal8中通过fid加载文件?

时间:2016-08-26 03:14:07

标签: php drupal-8

这是我的代码

    $fid=$node->get("field_vocabulary")->audio;
    $file =\Drupal\file\Entity\File::load($fid);
    $path = $file->getFileUri();
    $json_arr["audio"]=$path;

但它没有用! 为什么呢?

显示此错误

Fatal error: Call to a member function getFileUri() on null in /Applications/MAMP/htdocs/houdun/drupal/modules/article_json/src/Controller/ArticleJsonController.php on line 91

我需要帮助!谢谢!

1 个答案:

答案 0 :(得分:3)

我修好了!

    $fid=$node->get("field_vocabulary")->audio;
    $file =File::load($fid);
    $path = file_create_url($file->getFileUri());
    $arr["audio"]=$path;

    $fid=$node->get("field_vocabulary")->audio;
    $file =File::load($fid);
    $path = $file->url();
    $arr["audio"]=$path;