By sops on
Hello, I'm currently working on creating custom error pages on Drupal 9.
I know I can easily add customized pages for 404 and 403, but how can I use custom pages for all possible errors like 401, 403, 500, 502, and etc.?
I found this module: https://www.drupal.org/project/error_page but I couldn't see the result, not so sure which part I messed up with. And some say I could add custom hooks - but I'm not familiar with php and twig, so it was very hard for me to follow with no detailed guidance.
Can someone help me with either using that module or teach me if there's any easier way to configure those?
Thank you.
Comments
Try this in your .theme
Where should I add these file?
@dariemlazaro Thank you so much! Where exactly I'd need to add these files? And does this mean, i could create page--500.html.twig and etc?
The function needs to be added to the [THEME].theme file
it needs a change for the error 500.
Optimized the code in the function making it less verbose. Apart from the 500 it is equivalent to the proposed code. Assume it works, didn't look at that.
Where to add this function?
Thank you. So, I assume that from here:
I can create a page name starting with page__500.html.twig something like that for customization?
But I wondered where should I add this function? Like under which folder and what page?
The question about where is already answered twice
You add the function to your theme !
Thanks!
Thank you! So, I'm using custom theme - have four folders including templates folder: this folder has just one file page.html.twig
and I have two files info.yml and libraries.yml, where should I add this function?
Should I add it to page.html.twig? and also create new files for page__(error number).html twig for each error like 401, 403, 503, and etc and add that function?
No, to the theme
Please read this topic again and process the information provided. You have been told where to add this now 4 times!
For the core theme olivero, it would be olivero.theme.
Thank you
Thank you! Sorry, I'm pretty new to drupal -- so everything seems pretty confusing to me. And I'm not using olivero theme, i'm using my own custom theme, so I was confused. As, I don't have that {themename}.theme file :/ so it seems like I'll have to create a file first.
But how about creating a custom error html page? Can I create page__{errorname}.html.twig in my custom theme folder?
And thank you so much for your help.
Olivero was just an example, could have used any core theme
Having a custom theme suggests familiarity with themes. Indeed, when that file doesn't exist you need to add yourself.
Twig templates go in .... indeed in templates. Suggest to look at existing themes.
Oh great
Oh thanks, I didn't know you could do that in a switch() and yes now it is much cleaner and legible.
Need some changes
I get this error with your code
the full code working now
and if you need more error pages just add a case to the switch with the error code before
$suggestions[] = 'page__' . $error;That is why I said:
Common mistake in Drupal. Assuming things rather that checking. Agree with the solution, still propose something different.
This is due to the DRY principle. In this case use $exception as a pointer to exception. So it doesn't need to be executed twice.