I have a file URI something like private://somefolder/myfile.pdf. Certain users don’t have access to the file so I am trying to change that using hook_file_download($uri). In that code I need to load the file and the examples I have seen suggest:
$files = Drupal::entityTypeManager() ->getStorage('file') ->loadByProperties(('uri' => $uri));
but when I do this, $files is empty.
The file is there, and as an admin I can open the file. Also, if I use this code:
$files = Drupal::entityTypeManager() ->getStorage('file') ->loadByProperties(('fid' => 12345));
it does load the file.
Also, if I strip off the private://somefolder/ and loadBy filename, it also works.
What am I missing here?
Possible answer and bug in core?
Although the $uri which is passed to hook_file_download($uri) is in the format mentioned above (which does not work); if I try to load by uri using this:
private://somefoldermyfile.pdf (note last last slash in Windows format)
then it does work.