Skip to main content

When building a theme in Drupal, it's often necessary to adapt your site’s UI dynamically based on the context in which a user is navigating. One such piece of dynamic content is the menu title. Achieving this can be a bit tricky due to the layered nature of its routing and menu systems. This reference article will walk you through the process of fetching the current menu title within the `hook_preprocess_page` function in your Drupal theme's `.theme` file.

 

Understanding Drupal's route and menu systems

Before diving into the code, it’s important to understand a couple of key components of Drupal’s architecture:

Routes – In Drupal, routes define paths in your application and are associated with various functionalities and controllers.
Route Match – This service provides information about the current route based on the ongoing request.
Title Resolver – This service is used to retrieve the title for a given route, considering access permissions and other contextual factors that might affect the title.

 

Implementation

Here’s an approach to fetch the current menu title in your theme

 

Fetch the current route match

To begin, obtain the current route match object which contains data about the route being accessed.

$route_match = \Drupal::routeMatch();

 

Get the route object

From the route match, retrieve the actual route object. This object includes detailed information about the current route.

$route = $route_match->getRouteObject();

 

Retrieve the route title

Using the title resolver service, fetch the title for the current route. This method ensures that the title is accurate and considers all relevant contextual details.

$title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route);

If the route does not have a title, you might want to define a fallback or default title.

if (!isset($title)) {
   $title = t('No title found');
}

 

Pass the Title to Your Twig Template

Finally, add the retrieved title to the `$variables` array. This makes it available in your Twig templates, where you can display it as needed.

$variables['menu_title'] = $title;

 

Displaying the Title in a Twig Template

Once you’ve passed the title to the Twig template, displaying it is straightforward:

{{ menu_title }}

Place this line in your Twig file where you want the menu title to appear.

 

The wrap

This method leverages Drupal’s powerful routing and title resolution services to dynamically display the current menu title. It’s a solution that enhances the contextual responsiveness of your Drupal theme. By understanding and using these systems, you can make your site’s UI significantly more dynamic and user-friendly, adapting seamlessly to how content is navigated and consumed.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...