Add submenu link

If you want to add a submenu link you first have to load the parent link and then create a submenu link:

use Drupal\menu_link_content\Entity\MenuLinkContent;

function MY_MODULE_update_8001() {
  $menu_link_parents = \Drupal::entityTypeManager()
    ->getStorage('menu_link_content')
    ->loadByProperties([
      'title' => 'Dashboard',
      'menu_name' => 'main',
    ]);

  $menu_link_parent = reset($menu_link_parents);

  if ($menu_link_parent) {
    MenuLinkContent::create([
      'title' => 'Add tasks',
      'link' => ['uri' => 'internal:/add-task'],
      'menu_name' => 'main',
      'weight' => -50,
      'parent' => $menu_link_parent->getPluginId(),
    ])->save();
  }
}
Terug naar het overzicht
Ik wil graag weten hoeveel bezoeker ik heb en wat ze doen op mijn site (via Google Analytics). Om dit te doen gebruik ik cookies. De data wordt anoniem opgeslagen en ik respecteer de Do Not Track voorkeuren van uw browser. Wilt u een cookie van mij?