Score:0

substitute for \Drupal\file\Entity\File::load($fid) in Drupal 8.9

mx flag

My goal is to load a file and get its properties.

The method 'Load' does not exist any more in Drupal 8.9 (in the File.php file)

\Drupal\file\Entity\File::load($fid);

What would be the substitute for this function?

I'm using <theme_name>preprocess_views<field_name> hook to override the output of a view. From here I'm able to retrieve the file id by using

$fid = $variables['field']->getValue($variables['row'])[0];

With the file id, my plan is to load the file and retrieve all its properties, such as label, uri, etc.

Searched the web and all recommendations are to use \Drupal\file\Entity\File::load($fid);

however, this method no longer exists Drupal 8.9

How would I go to load this file and get its properties??

Thanks!

4uk4 avatar
cn flag
It hasn't changed. load() was never in File.php but in one of the base classes. All entities in Drupal support the load() method.
user3891775 avatar
mx flag
Thank you so much for your response. Thanks @Clive! Using '$storage' worked! Tried making as the correct answer, but I don't have enough points to do so.
Score:3
cn flag

All entity types, of which \Drupal\file\Entity\File is one, have a static load() method. This comes from EntityBase::load(), and is present throughout all versions of Drupal 8 and 9.

You can keep using that method, it's not going anywhere, but alternatively you can use the entity type manager service, e.g.

$storage = \Drupal::entityTypeManager()->getStorage('file');
$file = $storage->load($fid);

Note that the service should be injected if your code is in a context which allows it.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.