Menu links have been moved out into a separate YAML file. To create menu links, add a {module}.links.menu.yml file in the root directory of your module. To create a menu link for the test page we created earlier, you would make a mastering_drupal_8.links.menu.yml file that looks like:
mastering_drupal_8.test_page:
title: 'Test'
description: 'Test page'
route_name: mastering_drupal_8.test_page
weight: 100
menu_name: main
For each menu link element, you have the following attributes:
- title: Title shown in the menu link
- description: Title attribute for the menu link
- route_name: Machine name of menu route
- weight: Determines the order of the menu links; higher weight links appear later
- menu_name: Machine name of the menu that the link is shown in
- parent: Machine name of the menu link that is the parent in the menu tree
One thing to note is that you cannot create a menu link to a route with named parameters that do not have defaults.
You are also able to create menu links dynamically. For example, the Views menu links YAML file looks like:
views_view:
class: Drupal\views\Plugin\Menu\ViewsMenuLink
form_class: Drupal\views\Plugin\Menu\Form\ViewsMenuLinkForm
deriver: \Drupal\views\Plugin\Derivative\ViewsMenuLink
The deriver class extends \Drupal\Component\Plugin\Derivative\DeriverBase and the getDerivativeDefinitions() function returns an array in the same format as the menu links YAML element.