Argument #2 ($route) must be of type Symfony\Component\Routing\Route, null given

Created on 12 May 2025, 5 days ago

Problem/Motivation

When attempting to resolve the title in a custom controller or service using the TitleResolverInterface::getTitle() method, an error occurs if the $route_match->getRouteObject() returns NULL.
Error:
TypeError: Drupal\Core\Title\TitleResolverInterface::getTitle(): Argument #2 ($route) must be of type Symfony\Component\Routing\Route, null given

This issue arises because the custom_breadcrumbs module attempts to retrieve the current route object during cron execution, but no route is available in this context. The TitleResolver::getTitle() method expects a valid Route object, and passing null leads to the TypeError.

During cron runs, especially automated deployments, certain services or contexts like the current route may not be initialized. The custom_breadcrumbs module's BreadcrumbBuilder assumes the presence of a route, leading to the error when it's absent.

Proposed resolution

Add a NULL check for the route object before calling getTitle(). Here's the recommended updated code:

<?php
if (empty($title)) {
  try {
    $route = $route_match->getRouteObject();
    if ($route) {
      $title = $this->titleResolver->getTitle($this->currentRequest, $route);
    }
    else {
      $title = NULL;
    }
  }
  catch (\InvalidArgumentException $exception) {
    $title = NULL;
  }
}
?>

This ensures getTitle() is only called when a valid Route object is available, preventing fatal errors.

๐Ÿ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

๐Ÿ‡ง๐Ÿ‡พBelarus yurikulinkovich

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024