Problem/Motivation
We have custom form access implemented with hook_ENTITY_TYPE_access(). We would like to customize the resulting "Access Denied" pages.
For anonymous users, it will need to say something like "You must login to view this form" with a login link. For authenticated users, it will sometimes need to say something like "You must logout to view this form" and sometimes give a reason why the user has no access.
Proposed resolution
I think the easiest way to do this would be if Webform::buildAccessDenied() had an alter hook allowing modules to provide their own messages. If you agree, I will implement this. Do you have another suggestion for how to accomplish this?
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | webform-theme-access-denied-3247771-23.patch | 950 bytes | liam morland |
| #15 | webform-webform_access_denied-3247771-15-D8.patch | 4.98 KB | liam morland |
| #14 | webform-webform_access_denied-3247771-14-D8.patch | 4.48 KB | liam morland |
| #10 | 3247771-10.patch | 4.98 KB | jrockowitz |
| #8 | 3247771-8.patch | 1.57 KB | jrockowitz |
Issue fork webform-3247771
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3247771-access-denied
changes, plain diff MR !105
- 6.1.x
compare
- 3247771-add-alter-hook
changes, plain diff MR !67
Comments
Comment #3
liam morlandCreated hook_webform_build_access_denied_alter().
Comment #5
paulocsI like the idea for the new hook. I added documentation for it.
Comment #6
liam morlandThanks. I've made a few tweaks to the documentation.
Comment #7
jrockowitz commented@Liam I am sorry I did not respond sooner.
When I have run into similar issue where something outputted to an end-user needs to be alterable I have opted for adding a theme template so that modules and themes can alter the output
Comment #8
jrockowitz commentedAttached is a POC, I am hoping all the existing tests pass.
Comment #10
jrockowitz commentedComment #11
liam morlandThank you. I will do a full test on Monday.
Comment #12
liam morlandThe patch works. I was able to change the output using hook_preprocess_HOOK(). One coding standards issue:
Or the array could be broken-up with one element per line.
Tests that fail in #10 are failing on the branch as well.
Would you like me to fix the coding standards and put this in the merge request?
Comment #13
jrockowitz commentedIf you have time, please clean up the patch.
Comment #14
liam morlandComment #15
liam morlandSorry, the last one was missing the new template.
Comment #16
paulocsLooks good!
Comment #18
jrockowitz commentedComment #20
jrockowitz commentedLOL, the DA is slowly nudging me to move from patches to merge requests.
Comment #22
liam morlandI'm trying to use this in our real-world application now. Inside hook_preprocess_webform_access_denied(), I can change the "access denied" message. However, for my use case, I need access to the Webform object. Is there any way to get this or would buildAccessDenied() need to include $webform as a property in $build?
Comment #23
liam morlandThe attached patch does what we need. Is there another way that a hook_preprocess_webform_access_denied() implementation could get the current Webform object?
Comment #24
jrockowitz commentedComment #25
liam morlandComment #28
jrockowitz commented