Tải một thực thể tệp bằng URI


11

Có cách nào để tải tệp bằng URI trong Drupal 8 hoặc Drupal 9 không?

Trong Drupal 7 có thể bằng ví dụ tiếp theo:

$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

Lấy từ đây https://www.drupal.org/forum/support/module-development-and-code-questions/2012-06-06/load-a-file-by-uri#comment-6493610

Câu trả lời:


18

Có một loadByPropertiesphương thức trong \Drupal\Core\Entity\EntityStorageInterfaceđó cho phép xác định điều kiện bắt buộc khi tìm kiếm thực thể tệp bằng URI tệp đã cho:

/* @var \Drupal\file\FileInterface[] $files */
$files = \Drupal::entityTypeManager()
  ->getStorage('file')
  ->loadByProperties(['uri' => $uri]);
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.