0

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!

2
  • 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.
    – 4k4
    Oct 26 '21 at 16:50
  • 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. Oct 26 '21 at 17:24
3

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.

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.