Drupal 7 函数 menu_tree_page_data 无法正常工作?

发布于 2024-12-19 14:41:25 字数 719 浏览 3 评论 0原文

阅读 menu_tree_page_data() 的文档,在我看来,我应该能够通过以下方式调用该函数来检索主菜单的全部内容: menu_tree_page_data('主菜单')。由于参数 2 $max_depth 默认为 NULL,因此应该递归地遍历所有级别。由于参数 3 $only_active_trail 默认为 FALSE,因此这应该检索所有链接,而不仅仅是活动路径上的链接。

但是,当调用此函数时,我最终得到以下类型的树数据。

所有链接均位于第一级深度。
所有链接均位于第二深度。
仅第三深度级别的活动链接。

这是怎么回事?我还尝试将参数显式设置为 menu_tree_page_data('main-menu', 10, FALSE) 只是为了确保我没有误解默认行为。我必须编写自己的函数来为我提供整个菜单,但它没有活动跟踪数据。现在,我发现自己处于这样一种情况:我正在混合这两个函数来创建带有活动跟踪信息的完整菜单树,而且它非常混乱。如果有一种解决方案能够奏效那就太好了。

或者,是否有一种简单的方法来确定给定菜单项的活动轨迹?如果是这样,我只需将其添加到我已经工作的树爬虫函数中,然后就到此为止了。

Reading the documentation for menu_tree_page_data(), it seems to me that I should be able to retrieve the entire contents of my main menu by calling the function in the following way: menu_tree_page_data('main-menu'). Since param 2, $max_depth, defaults to NULL, this should recursively go through all levels. Since param 3, $only_active_trail, defaults to FALSE, this should retrieve all links, not just those on the active trail.

However, when calling this function, I end up with the following type of tree data.

All links on the first level of depth.
All links on the second level of depth.
Only active links on the third level of depth.

What's going on here? I've also tried explicitly setting params as menu_tree_page_data('main-menu', 10, FALSE) just to make sure that I was not misinterpreting the default behavior. I've had to write my own function to give me the entire menu, but it doesn't have active-trail data. Now, I'm finding myself in a situation where I'm blending the two functions to create a full menu tree WITH active-trail information, and it is extremely messy. It'd be great if one solution just worked.

Alternatively, is there a simple way of determining active trail for a given menu item? If so, I'll just add that to my already-working tree-crawler function and call it a day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-12-26 14:41:25

我认为这是 menu_tree_page_data() 中的静态缓存的问题。缓存 ID 是使用(除其他外)传递到函数MENU_MAX_DEPTH(以较小者为准)的$max_depth 构建的。

由于 MENU_MAX_DEPTH 等于 9,因此当您将 10 传递给函数时,它会自动重置为 9,并且缓存 ID 是根据该值构建的。如果另一个函数已经调用 menu_tree_page_data() 并使用 MENU_MAX_DEPTH 作为 $max_depth 参数(这确实发生在 Drupal 页面的前面),并且将 $only_active_trail 设置为 TRUE 时,静态缓存的数据将仅与活动跟踪项一起返回。

我认为以下方法会起作用:

drupal_static_reset('menu_tree_page_data');
$tree = menu_tree_page_data('main-menu');

如果不起作用,您可能还需要清除所涉及的另一个函数的缓存(_menu_build_tree()) 所以试试这个:

drupal_static_reset('_menu_build_tree');
drupal_static_reset('menu_tree_page_data');
$tree = menu_tree_page_data('main-menu');

希望这是有道理的,这很棘手解释一下:)

I think it's a problem with the static caching in menu_tree_page_data(). The cache ID is built up using (among other things) the $max_depth passed into the function or MENU_MAX_DEPTH, whichever is smaller.

Since MENU_MAX_DEPTH is equal to 9, when you pass 10 into the function this is automatically reset to 9, and the cache ID is built from that. If another function has already called menu_tree_page_data() with MENU_MAX_DEPTH as the $max_depth argument (which does happen earlier on in a Drupal page), and with $only_active_trail set to TRUE then the statically cached data will be returned with only active trail items.

I think the following will work:

drupal_static_reset('menu_tree_page_data');
$tree = menu_tree_page_data('main-menu');

If it doesn't, you might also need to clear the cache for another function that's involved (_menu_build_tree()) so try this:

drupal_static_reset('_menu_build_tree');
drupal_static_reset('menu_tree_page_data');
$tree = menu_tree_page_data('main-menu');

Hope that makes sense it's quite tricky to explain :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文