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 Fletcher07 Jan 2025
Resolving Twig syntax errors in Drupal
The release of Drupal 10.4.0 sees stricter validation rules being applied to Twig templates, which can result in unexpected errors after an upgrade. One such issue involves the use of regular expressions within Twig's matches operator, leading to syntax errors that can break template rendering.This...
Andrew Fletcher11 Nov 2024
How to resolve the "filemtime(): stat failed" warning in Drupal 10 / 11
If you’re running a website on Drupal 10.x or 11.x, you might encounter a warning like this in your logs:> "Warning: filemtime(): stat failed for sites/default/files/php/twig/67312f8c6d7ee_paragraph.html.twig_oBJkUYn5Hj1Gltpsh3AXvAcSC/ErOs4_HSnzbWqmYFWDDuM3htp2ANqUUtX84lTbfu2Bg.php in...