In the context of Drupal module development, providing easy navigation through clear and accessible menu links is essential. This lesson focuses on adding menu links to your custom module using the .links.menu.yml
file, giving users intuitive access to your module's features and configurations.
Understanding the .links.menu.yml File
The .links.menu.yml
file in Drupal is a declarative way to define menu links that appear in various navigation menus across your site. By specifying these links, you help users access key functionalities and settings presented by your custom module.
Setting Up Menu Links
Let's explore how to define these menu links step-by-step:
Step 1: Create the .links.menu.yml File
Inside your module's directory, you'll need to create a .links.menu.yml
file. This YAML file will contain the definitions for your menu links. For example, navigate to your module directory and create this file:
touch my_rest_module.links.menu.yml
Step 2: Define Menu Links in YAML
Open my_rest_module.links.menu.yml
and insert the following configuration:
my_rest_module.admin:
title: 'My REST Module'
description: 'Access configuration settings for the My REST Module'
route_name: my_rest_module.settings_form
parent: system.admin_config_services
weight: 100
my_rest_module.content:
title: 'Manage Custom Content'
description: 'Access and manage custom content provided by the My REST Module.'
route_name: my_rest_module.content
parent: system.admin_content
weight: 10
In this configuration, we define two menu links:
- my_rest_module.admin: Points to the settings form for the module and appears under the "Configuration" menu section in the admin interface.
- my_rest_module.content: Provides access to content management features specific to the custom module, showing under the "Content" section.
Field Descriptions
- title: The text displayed for the menu link in the Drupal interface.
- description: Additional context about the link, shown in the administrative overview and helpful for editors.
- route_name: The internal route that the link redirects to when clicked. This should match the route you define in your module's routing file.
- parent: Indicates the menu or section this link belongs to, such as system menus like the admin or settings.
- weight: Dictates the order of links; smaller numbers place links higher up in lists.
Associating Routes with Menu Links
Ensure that each route_name
corresponds to a defined route in your module's .routing.yml
file. For example, supplement your my_rest_module.routing.yml
with routes like these:
my_rest_module.settings_form:
path: '/admin/config/services/my-rest-module'
defaults:
_form: '\Drupal\my_rest_module\Form\SettingsForm'
_title: 'My REST Module Settings'
requirements:
_permission: 'administer site configuration'
my_rest_module.content:
path: '/admin/content/custom-content'
defaults:
_controller: '\Drupal\my_rest_module\Controller\ContentController::overview'
_title: 'Manage Custom Content'
requirements:
_permission: 'access content overview'
These routes must exist to link directly from the menu items effectively. Each route_name
should correspond to a path managed by your module, ensuring valid redirections.
Testing Your Menu Links
After configuring your .links.menu.yml
file and corresponding routes, clear your Drupal cache to apply changes:
drush cr
Navigate to your Drupal admin interface and verify if the menu items appear as expected in the designated sections. Click each link to ensure they direct users to the correct pages or forms.
Conclusion
Menu links are key components of a Drupal site's navigation system, enhancing usability and accessibility for site users. By understanding how to define and manage these links through the .links.menu.yml
file, you are empowered to create intuitive interfaces that align with your module's functionality and overall user experience.
What’s Next?
Enhancing Drupal’s usability isn't just about menu links. Next, we'll look into Defining Local Tasks in .links.task.yml, where we'll explore how to add tabbed navigation to your custom module interfaces, allowing for a more organized and structured user flow.