loadLinksByRoute example

    // weight and ID.     $found = NULL;

    $route_name = $this->routeMatch->getRouteName();
    // On a default (not custom) 403 page the route name is NULL. On a custom     // 403 page we will get the route name for that page, so we can consider     // it a feature that a relevant menu tree may be displayed.     if ($route_name) {
      $route_parameters = $this->routeMatch->getRawParameters()->all();

      // Load links matching this route.       $links = $this->menuLinkManager->loadLinksByRoute($route_name$route_parameters$menu_name);
      // Select the first matching link.       if ($links) {
        $found = reset($links);
      }
    }
    return $found;
  }

}
    $child2 = MenuLinkContent::load($child2->id());
    // Test the reference in the child.     $this->assertSame('menu_link_content:' . $parent->uuid()$child2->getParentId());
  }

  /** * Tests uninstalling a module providing default links. */
  public function testModuleUninstalledMenuLinks() {
    \Drupal::service('module_installer')->install(['menu_test']);
    \Drupal::service('plugin.manager.menu.link')->rebuild();
    $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
    $this->assertCount(1, $menu_links);
    $menu_link = reset($menu_links);
    $this->assertEquals('menu_test', $menu_link->getPluginId());

    // Uninstall the module and ensure the menu link got removed.     \Drupal::service('module_installer')->uninstall(['menu_test']);
    \Drupal::service('plugin.manager.menu.link')->rebuild();
    $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_test.menu_test');
    $this->assertCount(0, $menu_links);
  }

  
// If the parent link path is external, URL will be useless because the       // link will definitely not correspond to a Drupal route.       if (UrlHelper::isExternal($parent_link_path)) {
        $links = $this->menuLinkStorage->loadByProperties([
          'menu_name' => $menu_name,
          'link.uri' => $parent_link_path,
        ]);
      }
      else {
        $url = Url::fromUserInput('/' . ltrim($parent_link_path, '/'));
        if ($url->isRouted()) {
          $links = $this->menuLinkManager->loadLinksByRoute($url->getRouteName()$url->getRouteParameters()$menu_name);
        }
      }
      if (!empty($links)) {
        return reset($links)->getPluginId();
      }
    }

    // Parent could not be determined.     throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id$value[0]));
  }

}

  public function testMenuLinkParent(array $source_value$lookup_result$plugin_id$route_name$expected_result) {
    [$parent_id$menu_name$parent_link_path] = $source_value;
    $this->migrateLookup->lookup(NULL, [$parent_id])
      ->willReturn([['id' => $lookup_result]]);
    if ($route_name) {
      $plugin_definition = ['menu_name' => $menu_name];
      $static_override = $this->prophesize(StaticMenuLinkOverridesInterface::class);
      $static_override = $static_override->reveal();
      $menu_link = new MenuLinkDefault([]$plugin_id$plugin_definition$static_override);
      $this->menuLinkManager->loadLinksByRoute($route_name[], 'admin')
        ->willReturn([$plugin_id => $menu_link]);

      $url = new Url($route_name[][]);
      $this->pathValidator->getUrlIfValidWithoutAccessCheck($parent_link_path)
        ->willReturn($url);
    }
    $result = $this->doTransform($source_value$plugin_id);
    $this->assertSame($expected_result$result);
  }

  /** * Provides data for testMenuLinkParent(). */
$this->assertSession()->pageTextContains('Menu item description text');
  }

  /** * Tests for menu_name parameter for default menu links. */
  protected function doTestMenuName() {
    $admin_user = $this->drupalCreateUser(['administer site configuration']);
    $this->drupalLogin($admin_user);
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $menu_links = $menu_link_manager->loadLinksByRoute('menu_test.menu_name_test');
    $menu_link = reset($menu_links);
    $this->assertEquals('original', $menu_link->getMenuName(), 'Menu name is "original".');

    // Change the menu_name parameter in menu_test.module, then force a menu     // rebuild.     menu_test_menu_name('changed');
    $menu_link_manager->rebuild();

    $menu_links = $menu_link_manager->loadLinksByRoute('menu_test.menu_name_test');
    $menu_link = reset($menu_links);
    $this->assertEquals('changed', $menu_link->getMenuName(), 'Menu name was successfully changed after rebuild.');
  }
// Locked menus may not be deleted.     if ($this->entity->isLocked()) {
      return;
    }

    // Delete all links to the overview page for this menu.     // @todo Add a more generic helper function to the menu link plugin     // manager to remove links to an entity or other ID used as a route     // parameter that is being removed. Also, consider moving this to     // menu_ui.module as part of a generic response to entity deletion.     // https://www.drupal.org/node/2310329     $menu_links = $this->menuLinkManager->loadLinksByRoute('entity.menu.edit_form', ['menu' => $this->entity->id()], TRUE);
    foreach ($menu_links as $id => $link) {
      $this->menuLinkManager->removeDefinition($id);
    }

    parent::submitForm($form$form_state);
  }

}
Home | Imprint | This part of the site doesn't use cookies.