I have added a menu item to my Drupal website via the following:


  title: 'Browse Members'
  description: 'Browse Business Listings'
  parent: main
  menu_name: main
  route_name: business_listing.view_all

This works fine.

I would now like to add sub items to this menu (make it s dropdown) programmatically. How would I go about doing this and where would I place the code?? I have tried the following:


function business_listing_preprocess_menu__main(&$variables) {

 $items = getMenuItems();

 foreach($items as $nid => $title) {
  $menu_link = MenuLinkContent::create([
    'title' => $title,
    'link' => ['uri' => 'internal:/node/' . $nid],
    'menu_name' => 'main',
    'expanded' => TRUE,

This worked in that it successfully adds the items to the main menu, but the items are duplicated whenever the page is refreshed. I also do not know how to make these items, sub menu items (create a drop down) of the menu link described above???

Any advice would be greatly appreciated

Much love,


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.