How to Create Local Task Tabs Through a Custom Module in Drupal
- LN Webworks
- Jun 26, 2024
- 3 min read

Creating local task tabs in Drupal is a powerful way to enhance your site's administrative interface, making it more intuitive and user-friendly for administrators. These tabs appear at the top of a page when logged in as an administrator, providing quick access to different sections or functionalities. In this blog post, we'll guide you through the process of creating local task tabs through a custom module in Drupal.
Understanding Local Task Tabs
Local task tabs are essentially links that appear as tabs at the top of a page, allowing users to navigate between different tasks or views. These tabs are particularly useful for administrators who need to manage various aspects of the site efficiently.
Step-by-Step Guide to Creating Local Task Tabs
1. Create the Custom Module
First, you need to create a custom module if you don't have one already. Let's assume your module is named "example".
2. Define the Local Task Links
In your custom module, you need to create a .yml file to define the local task links. The file should be named after your module and follow the format module_name.links.task.yml. For example, if your module name is "example", the file should be named example.links.task.yml.
Here's an example of what the example.links.task.yml file might look like:
example.admin:
route_name: example.admin
title: 'Example Admin'
base_route: example.settings
example.settings:
route_name: example.settings
title: 'Settings'
base_route: example.admin
weight: 0
In this example:
route_name is the name of the route you are linking to.
title is the text that will appear on the tab.
base_route is the route to which this tab will be attached.
weight determines the order in which the tabs appear.
3. Place the File in the Correct Location
Ensure that the example.links.task.yml file is placed in the root directory of your module.
4. Define Routes in the Routing File
In addition to the task links file, you need to define the routes in your module's routing file (example.routing.yml). Here's an example:
example.admin:
path: '/admin/example'
defaults:
_controller: '\Drupal\example\Controller\ExampleController::admin'
_title: 'Example Admin'
requirements:
_permission: 'administer site configuration'
example.settings:
path: '/admin/example/settings'
defaults:
_controller: '\Drupal\example\Controller\ExampleController::settings'
_title: 'Settings'
requirements:
_permission: 'administer site configuration'
In this example:
path defines the URL path for the route.
defaults specify the controller and title for the route.
requirements specify the permissions required to access the route.
5. Implement the Controller
Create a controller to handle the logic for the routes defined. Here's a basic example of a controller:
namespace Drupal\example\Controller; use Drupal\Core\Controller\ControllerBase;
class ExampleController extends ControllerBase {
public function admin() {
return [
'#markup' => $this->t('Welcome to the Example Admin page.'),
];
}
public function settings() {
return [
'#markup' => $this->t('Welcome to the Example Settings page.'),
];
}
}
6. Adding Local Tasks Through Views
Local tasks can also be added by creating a Views page. Go to the Menu settings and set the Type as Menu tab to include it as a local task.
7. Altering Existing Local Tasks
If you need to alter local tasks provided by other modules, you can use the hook_menu_local_tasks_alter() function. This allows you to modify the existing tabs.
Minimum Number of Links
For the tabs to appear, a minimum of two links are required. Ensure that you have at least two local task links defined.
Enhancing Accessibility with Admin Local Tasks Module
The Admin Local Tasks module can also be used to make Drupal's local task links more accessible for content editors, providing a better user experience
Conclusion
Creating local task tabs in Drupal through a custom module is a straightforward process that can significantly improve the administrative experience. By following the steps outlined in this guide, you can create custom local task tabs tailored to your site's specific needs. At LN Webworks, we strive to empower our clients with the tools and knowledge to make the most of their Drupal sites. Happy coding!
Comments