Backend Development - Menu Links & Tasks

In the previous topic, we explored the Cron API and backend automation. Now we shift focus to another important backend concept: Menu Links and Local Tasks.

While routing defines how URLs map to controllers, menu links and local tasks define how users navigate to those routes.

In Drupal backend architecture:

  • Routes define access points
  • Controllers define logic
  • Menu links expose navigation
  • Local tasks create contextual tabs

This topic continues extending the weeklydrupal_demo module.

By the end of this article, you will understand:

  • What menu links are
  • What local tasks (tabs) are
  • How to define them using YAML
  • How they connect to routes
  • Best practices for backend navigation structure

1. What Are Menu Links

A menu link connects a route to a menu in Drupal.

Common menus include:

  • Main navigation
  • Footer
  • Administration menu

Menu links do not define logic. They simply point to routes.


2. Creating a Menu Link

Menu links are defined in a YAML file:

weeklydrupal_demo.links.menu.yml

Example:

weeklydrupal_demo.admin:
  title: 'WeeklyDrupal Demo'
  description: 'Demo administration page'
  route_name: weeklydrupal_demo.page
  parent: system.admin_content
  weight: 100

3. Understanding Menu Link Keys

  • title – Label shown in menu
  • description – Tooltip description
  • route_name – Must match a defined route
  • parent – Defines hierarchy
  • weight – Ordering relative to siblings

If the route does not exist, the link will not work.


4. Connecting to Routes

Example route:

weeklydrupal_demo.page:
  path: '/admin/content/weeklydrupal-demo'
  defaults:
    _controller: '\Drupal\weeklydrupal_demo\Controller\DemoController::page'
    _title: 'WeeklyDrupal Demo'
  requirements:
    _permission: 'access content'

The route_name in the menu link must match weeklydrupal_demo.page.


5. What Are Local Tasks

Local tasks create tabs at the top of pages.

Examples in core:

  • View
  • Edit
  • Delete

Local tasks are tied to a base route and create contextual navigation.


6. Creating Local Tasks

Local tasks are defined in:

weeklydrupal_demo.links.task.yml

Example:

weeklydrupal_demo.view:
  route_name: weeklydrupal_demo.page
  base_route: weeklydrupal_demo.page
  title: 'View'

weeklydrupal_demo.settings:
  route_name: weeklydrupal_demo.settings
  base_route: weeklydrupal_demo.page
  title: 'Settings'
  weight: 10

7. Understanding Local Task Keys

  • route_name – Route to open when tab is clicked
  • base_route – Route that owns the tab set
  • title – Label of the tab
  • weight – Tab ordering

Tabs appear only when visiting the base route.


8. Adding a Settings Route

Example settings route:

weeklydrupal_demo.settings:
  path: '/admin/content/weeklydrupal-demo/settings'
  defaults:
    _controller: '\Drupal\weeklydrupal_demo\Controller\DemoController::settings'
    _title: 'WeeklyDrupal Settings'
  requirements:
    _permission: 'administer site configuration'

Now the "Settings" tab will appear next to "View".


9. Menu Links vs Local Tasks

Menu Links:

  • Appear in navigation menus
  • Define hierarchical navigation

Local Tasks:

  • Appear as tabs
  • Provide contextual navigation
  • Are tied to a base route

Both are defined in YAML and discovered automatically.


10. Best Practices

  • Keep route names consistent
  • Use clear parent hierarchy
  • Do not hardcode paths
  • Use permissions carefully
  • Avoid cluttering admin menus

Navigation structure affects usability and maintainability.


11. Learning by Building: weeklydrupal_demo

Extend the module by:

  • Creating an admin route
  • Adding a menu link under Content
  • Adding tabs for View and Settings
  • Restricting access with permissions

This connects:

  • Routing
  • Controllers
  • Permissions
  • Navigation architecture

12. Why Menu Links and Tasks Matter

Understanding navigation structure allows you to:

  • Build clean admin interfaces
  • Organize complex features
  • Improve UX for site builders
  • Design scalable backend modules

Navigation is part of backend architecture, not just frontend design.