deleteLinksInMenu example

'id' => 'menu_test',
      'label' => 'Test menu',
      'description' => 'Description text',
    ])->save();
  }

  /** * Create a simple hierarchy of links. */
  public function createLinkHierarchy($module = 'menu_test') {
    // First remove all the menu links in the menu.     $this->menuLinkManager->deleteLinksInMenu('menu_test');

    // Then create a simple link hierarchy:     // - parent     // - child-1     // - child-1-1     // - child-1-2     // - child-2     $base_options = [
      'title' => 'Menu link test',
      'provider' => $module,
      'menu_name' => 'menu_test',
    ];


  /** * {@inheritdoc} */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage$entities);
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    foreach ($entities as $menu) {
      // Delete all links from the menu.       $menu_link_manager->deleteLinksInMenu($menu->id());
    }
  }

  /** * {@inheritdoc} */
  public function save() {
    $return = parent::save();
    \Drupal::cache('menu')->invalidateAll();
    // Invalidate the block cache to update menu-based derivatives.     if (\Drupal::moduleHandler()->moduleExists('block')) {
      
\Drupal::entityTypeManager()->getStorage('menu')->create(['id' => 'menu2'])->save();

    \Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
    \Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
    \Drupal::entityTypeManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu2', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();

    $output = $this->linkTree->load('menu1', new MenuTreeParameters());
    $this->assertCount(2, $output);
    $output = $this->linkTree->load('menu2', new MenuTreeParameters());
    $this->assertCount(1, $output);

    $this->menuLinkManager->deleteLinksInMenu('menu1');

    $output = $this->linkTree->load('menu1', new MenuTreeParameters());
    $this->assertCount(0, $output);

    $output = $this->linkTree->load('menu2', new MenuTreeParameters());
    $this->assertCount(1, $output);
  }

  /** * Tests creating links with an expected tree structure. */
  
Home | Imprint | This part of the site doesn't use cookies.