通过URI加载文件实体


12

有没有办法在Drupal 8或Drupal 9中通过URI加载文件?

在下一个示例中,在Drupal 7中是可能的:

$uri = 'public://file.xyz';

// Take a look at: file.inc::file_load_multiple
$files = file_load_multiple(array(), array('uri' => $uri));
$file = reset($files); // If empty, $file will be false, otherwise will contain the required file

从这里获取https://www.drupal.org/forum/support/module-development-and-code-questions/2012-06-06/load-a-file-by-uri#comment-6493610

Answers:


19

中的loadByProperties方法\Drupal\Core\Entity\EntityStorageInterface允许通过给定的文件URI定义搜索文件实体时所需的条件:

/* @var \Drupal\file\FileInterface[] $files */
$files = \Drupal::entityTypeManager()
  ->getStorage('file')
  ->loadByProperties(['uri' => $uri]);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.