Situation:
- My private files directory is outside the document root.
- A content type
chapter
includes a file fieldpdf
that stores its files in the private files directory. - When a given
chapter
node is published, itspdf
file 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
chapter
node is unpublished, itspdf
file 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
pdf
file for achapter
node 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.