Situation:
- My private files directory is outside the document root.
- A content type
chapterincludes a file fieldpdfthat stores its files in the private files directory. - When a given
chapternode is published, itspdffile is accessible to users viamysite/system/files/filename.pdf. This is expected; it’s the way private file access works in Drupal. - If a given
chapternode is unpublished, itspdffile is not accessible to non-priviledged users. They get an access denied page. Again, this is expected.
Problem:
- I want to control access to the
pdffile for achapternode based on values in certain fields of the node as well as on user permissions and other conditions.
Question:
- Using a custom module, how can I override the default handling of private files to control when a file is accessible? I can write the code to make the checks; I don’t know how to override the default handler. Since some aspects of file handling have changed in Drupal 9, I’ll mention that this is the version I’m using, in case it matters.