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

如何按URL或菜单路径查询Drupal 8.x/9.x的Shortcut快捷方式

问题根因

直接调用loadByProperties传入link作为查询条件报错,是因为Shortcut实体的link属于Link复合字段类型,底层存储会拆分为link.uri、link.title、link.options三个独立数据库列,loadByProperties仅支持查询单值顶层字段,不支持直接传入复合字段名查询。

正确实现方案

使用实体查询(EntityQuery)指定复合字段的子属性link.uri作为查询条件,即可精准匹配路径,不受快捷方式标题、翻译的影响:

// 旧路径URI,与数据库中shortcut__link表的link_uri存储格式保持一致
$old_path_uri = 'internal:/admin/timeline-management';
// 替换为你的新路径URI
$new_path_uri = 'internal:/admin/你的新路径';

// 查询所有指向旧路径的快捷方式
$shortcut_storage = \Drupal::entityTypeManager()->getStorage('shortcut');
$shortcut_ids = $shortcut_storage->getQuery()
  ->condition('link.uri', $old_path_uri)
  ->accessCheck(FALSE) // 更新钩子必须关闭权限检查,避免漏查
  ->execute();

// 批量更新路径
$shortcuts = $shortcut_storage->loadMultiple($shortcut_ids);
foreach ($shortcuts as $shortcut) {
  $shortcut->set('link', ['uri' => $new_path_uri]);
  $shortcut->save();
}

注意事项

  • 如果快捷方式指向的是Drupal路由,也可以直接匹配route:路由名格式的uri,只要和数据库存储的值完全一致即可
  • 若需要保留原快捷方式的自定义链接属性,赋值时可以保留原link字段的options、title值,仅替换uri字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 05:45:03