From 152ec7a915472ae9760c57a97c54518149782299 Mon Sep 17 00:00:00 2001 From: Mark Conroy Date: Wed, 15 Jan 2025 16:36:44 +0000 Subject: [PATCH 1/5] adds default content for 403 and 404 pages --- localgov_base.theme | 31 +++++++++++++++++++ .../layout/status-pages/page--403.html.twig | 9 ++++++ .../layout/status-pages/page--404.html.twig | 9 ++++++ .../layout/status-pages/page-status.html.twig | 1 + 4 files changed, 50 insertions(+) diff --git a/localgov_base.theme b/localgov_base.theme index 3ea68d10..abd3c0fa 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -146,6 +146,37 @@ function localgov_base_preprocess_page(&$variables): void { if (theme_get_setting('localgov_base_show_back_to_top_link')) { $variables['back_to_top'] = TRUE; } + + // Load the site configuration. + $site_config = \Drupal::config('system.site'); + $site_403 = $site_config->get('page.403'); + $site_404 = $site_config->get('page.404'); + + // Custom 403 and 404 pages. + // We have a variable in page.html.twig called default_status_content. + // This is used to determine if we should show the default 403/404 content, + // or if we should show the content of the node that is set as the 403/404 + // page. + $variables['default_status_content'] = true; + $route_match = Drupal::routeMatch(); + $route = $route_match->getRouteName(); + if ($route === 'entity.node.canonical') { + $node = $route_match->getParameter('node'); + $node_id = $node->id(); + $is_404_page_node = 'node/' . $node_id === $site_404; + $is_403_page_node = 'node/' . $node_id === $site_403; + if ($is_404_page_node && empty($site_404)) { + $variables['default_status_content'] = true; + } else { + $variables['default_status_content'] = false; + } + if ($is_403_page_node && empty($site_403)) { + $variables['default_status_content'] = true; + } else { + $variables['default_status_content'] = false; + } + } + } /** diff --git a/templates/layout/status-pages/page--403.html.twig b/templates/layout/status-pages/page--403.html.twig index b01f7b4c..f4b49aa2 100644 --- a/templates/layout/status-pages/page--403.html.twig +++ b/templates/layout/status-pages/page--403.html.twig @@ -1 +1,10 @@ +{% if default_status_content %} + {% set default_status_content %} +

{{ "Sorry, you do not have access to this page"|t }}

+ {% trans %} +

Perhaps you need to login to the site.

+ {% endtrans %} + {% endset %} +{% endif %} + {% extends "@localgov_base/layout/status-pages/page-status.html.twig" %} diff --git a/templates/layout/status-pages/page--404.html.twig b/templates/layout/status-pages/page--404.html.twig index b01f7b4c..c7073fc9 100644 --- a/templates/layout/status-pages/page--404.html.twig +++ b/templates/layout/status-pages/page--404.html.twig @@ -1 +1,10 @@ +{% if default_status_content %} + {% set default_status_content %} +

{{ "We couldn't find the page you're looking for. It's possible you entered the address incorrectly or you're looking for a page we've moved or deleted."|t }}

+

{{ "If you typed the web address, check it is correct."|t }}

+

{{ "If you pasted the web address, check you copied the entire address."|t }}

+

{{ "You can browse from the homepage or use the search box above to find the information you need."|t }}

+ {% endset %} +{% endif %} + {% extends "@localgov_base/layout/status-pages/page-status.html.twig" %} diff --git a/templates/layout/status-pages/page-status.html.twig b/templates/layout/status-pages/page-status.html.twig index 257735a3..0a907f2c 100644 --- a/templates/layout/status-pages/page-status.html.twig +++ b/templates/layout/status-pages/page-status.html.twig @@ -113,6 +113,7 @@
+ {{ default_status_content }} {{ page.content }}
From 1a30696ef1f7e256853ebdf5c26eb02693012dc5 Mon Sep 17 00:00:00 2001 From: Mark Conroy Date: Wed, 15 Jan 2025 16:42:38 +0000 Subject: [PATCH 2/5] update variable declaration --- localgov_base.theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index abd3c0fa..b853674d 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -163,8 +163,8 @@ function localgov_base_preprocess_page(&$variables): void { if ($route === 'entity.node.canonical') { $node = $route_match->getParameter('node'); $node_id = $node->id(); - $is_404_page_node = 'node/' . $node_id === $site_404; - $is_403_page_node = 'node/' . $node_id === $site_403; + $is_404_page_node = $site_404 === 'node/' . $node_id; + $is_403_page_node = $site_403 === 'node/' . $node_id; if ($is_404_page_node && empty($site_404)) { $variables['default_status_content'] = true; } else { From c221cdf4345f532e54af1cb0533246024b33746e Mon Sep 17 00:00:00 2001 From: Mark Conroy Date: Wed, 15 Jan 2025 16:53:15 +0000 Subject: [PATCH 3/5] coding standards --- localgov_base.theme | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index b853674d..48d6c1b6 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -157,7 +157,7 @@ function localgov_base_preprocess_page(&$variables): void { // This is used to determine if we should show the default 403/404 content, // or if we should show the content of the node that is set as the 403/404 // page. - $variables['default_status_content'] = true; + $variables['default_status_content'] = TRUE; $route_match = Drupal::routeMatch(); $route = $route_match->getRouteName(); if ($route === 'entity.node.canonical') { @@ -166,14 +166,16 @@ function localgov_base_preprocess_page(&$variables): void { $is_404_page_node = $site_404 === 'node/' . $node_id; $is_403_page_node = $site_403 === 'node/' . $node_id; if ($is_404_page_node && empty($site_404)) { - $variables['default_status_content'] = true; - } else { - $variables['default_status_content'] = false; + $variables['default_status_content'] = TRUE; + } + else { + $variables['default_status_content'] = FALSE; } if ($is_403_page_node && empty($site_403)) { - $variables['default_status_content'] = true; - } else { - $variables['default_status_content'] = false; + $variables['default_status_content'] = TRUE; + } + else { + $variables['default_status_content'] = FALSE; } } From 5afeb304effee91b2cc066d069ccf480185ada7e Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 5 Feb 2025 21:04:40 +0800 Subject: [PATCH 4/5] Fix php static analysis problem with using empty() on the $site_403 and $site_404 variables. --- localgov_base.theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index 48d6c1b6..b2fc5a58 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -165,13 +165,13 @@ function localgov_base_preprocess_page(&$variables): void { $node_id = $node->id(); $is_404_page_node = $site_404 === 'node/' . $node_id; $is_403_page_node = $site_403 === 'node/' . $node_id; - if ($is_404_page_node && empty($site_404)) { + if ($is_404_page_node && is_null($site_404)) { $variables['default_status_content'] = TRUE; } else { $variables['default_status_content'] = FALSE; } - if ($is_403_page_node && empty($site_403)) { + if ($is_403_page_node && is_null($site_403)) { $variables['default_status_content'] = TRUE; } else { From 5ea7eed790aaa2b3a793cae54045b0e60eb63ea2 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 5 Feb 2025 21:28:01 +0800 Subject: [PATCH 5/5] Change the word "login" (noun) to "log in" (verb). --- templates/layout/status-pages/page--403.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/layout/status-pages/page--403.html.twig b/templates/layout/status-pages/page--403.html.twig index f4b49aa2..6fe25950 100644 --- a/templates/layout/status-pages/page--403.html.twig +++ b/templates/layout/status-pages/page--403.html.twig @@ -2,7 +2,7 @@ {% set default_status_content %}

{{ "Sorry, you do not have access to this page"|t }}

{% trans %} -

Perhaps you need to login to the site.

+

Perhaps you need to log in to the site.

{% endtrans %} {% endset %} {% endif %}