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:In the site I am developing, the private files directory is outside the document root.
I have a content type (Chapter) with a file field that stores its files in the private files directory. When a Chapter node is published, its file is accessible to users, but when the node is unpublished, its file is not accessible to users. They get an access denied page.
- 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:How can I control the access to the attached file and allow users to access the attached file when the node is unpublished, in a custom module?
- 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.