You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal中获取指定路由对应菜单链接项返回空数组问题咨询

问题排查&解决方案

核心原因

  • 你当前使用的 menu_link_content 实体存储仅覆盖后台手动创建的自定义菜单链接,如果该菜单是模块通过 *.links.menu.yml 定义的配置型菜单,不会被存储在这个实体表中,自然返回空结果。
  • 你填写的link.uri匹配规则错误:entity: 前缀仅适配实体类型的默认canonical路径(例如entity:node/123对应ID为123的节点详情页),普通自定义内部路径的菜单链接,存储的uri前缀为internal:,你要查询的正确uri值应为internal:/people/scientists。

修复后的代码示例

场景1:仅查询手动创建的自定义菜单链接

$menu_link = \Drupal::entityTypeManager()->getStorage('menu_link_content')
                ->loadByProperties(['link.uri' => 'internal:/people/scientists']);

场景2:查询所有类型的菜单链接(含yml定义的配置型菜单)

如果不确定菜单类型,建议使用Drupal菜单链接插件管理器查询,适配所有场景:

// 先将路径转换为路由信息
$target_path = '/people/scientists';
$url = \Drupal\Core\Url::fromUserInput($target_path);
$menu_links = [];
if ($url->isRouted()) {
  // 按路由名和参数查询所有匹配的菜单链接
  $menu_links = \Drupal::service('plugin.manager.menu.link')
    ->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters());
}

内容的提问来源于stack exchange,提问作者supersize

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 06:06:06