How to add an existing path in your custom module to Drupal Menu system
Here we are going to discuss how to show custom routes created in custom module in Drupal menu system.
Go to Structure-> Menu under path admin/structure/menu
You can see below menu links,
All menu links goes under these menus, here we are going to assign our custom module link to Main navigation
While click edit of any of Main Navigation you can see below URL in address bar.
/admin/structure/menu/manage/main
Here in last part of path you can see main, this will use as menu name in coming section.
So here we have a student form in our custom module dn_menu
We have student form in path /src/Form/StudentForm.php
We have below entry in dn_menu.routing.yml.
dn_menu.add_menu:
path: '/menu/students/add'
defaults:
_title: 'Add Students Menu'
_form: '\Drupal\dn_menu\Form\StudentForm'
requirements:
_access: 'TRUE'
So in order to load to this form as a link in Main Menu, we are creating file dn_menu.links.menu.yml in our custom module root path with below content.
dn_menu.students:
title: 'Student Form Menu'
description: 'link student entiries'
parent: main
menu_name: main
route_name: dn_menu.add_menu
Parent is optional and menu name is the name of Menu navigation that we took from edit url of Main navigation menu.
Clear the cache, under Main Navigation, you can see Student Form Menu, which is the title of our custom menu. Link of this Menu is the path mentioned in routing.yml.
Click on edit on this menu, you can see like path and title cannot be edited.
You are free to change the parent link, also other options are editable.