If a route doesn't exist, we shouldn't WSOD the site

Created on 29 June 2023, over 1 year ago
Updated 12 April 2024, 10 months ago

It is possible that someone could delete a view or other route that we are assuming is there. We need to fence off the routes in case they are not available.

Create a function:

function routeExists($name)
{
    // I assume that you have a link to the container in your twig extension class
    $router = \Drupal::service('router');
    return (null === $router->getRouteCollection()->get($name)) ? false : true;
}
...
    'manage_content' => [
      '#title' => t('Manager Role and Content'),
      '#description' => t('Ensure managers have the ability to manage content.'),
      'handbook_page' => [
        '#text' => t('Site User Roles'),
        '#url' => routeExists('user.admin_permissions') ? Url::fromRoute('user.admin_permissions') : '',
      ],
    ],

or you could do something like this and use the routeExists function

/**
 * Generates and returns section 6 for launch_checklist.
 */
function section_06_content($checklist_routes) {
  $sections = [];
  ...
  if (routeExists('user.admin_permissions')) {
    $sections['manage_content'] = [
      '#title' => t('Manager Role and Content'),
      '#description' => t('Ensure managers have the ability to manage content.'),
      'handbook_page' => [
        '#text' => t('Site User Roles'),
        '#url' => Url::fromRoute('user.admin_permissions'),
      ],
    ];
  }
  ...
  // Add the rest of the routes checking to see if they exist.
  return $sections;
}

OR, a try/catch without the need for the function.

/**
 * Generates and returns section 6 for launch_checklist.
 */
function section_06_content($checklist_routes) {
  $sections = [];
  ...
  try {
    $sections['manage_content'] = [
      '#title' => t('Manager Role and Content'),
      '#description' => t('Ensure managers have the ability to manage content.'),
      'handbook_page' => [
        '#text' => t('Site User Roles'),
        '#url' => Url::fromRoute('user.admin_permissions'),
      ],
    ];
  } catch (\Exception $e) {}
  ...
  // Add the rest of the routes checking to see if they exist.
  return $sections;
}
πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States thejimbirch Cape Cod, Massachusetts

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024