自定义菜单帮助

发布于 2024-11-01 22:45:35 字数 877 浏览 1 评论 0原文

Drupal 7 hook_menu() 让我很困惑;我已经尝试了一切,但似乎无法让它发挥作用。

我需要什么:在自定义模块中,我想创建一个新菜单,并向该菜单添加大约四个链接。听起来很简单,但我很挣扎。我已经能够使用 .install 文件中的 $menu 数组创建菜单本身,但向该菜单添加项目没有意义。

有效代码:

$menu = array(
  'menu_name' => 'project-menu', 
  'title' => $t('Project Menu'), 
  'description' => 'Project Menu',
); 

menu_save($menu);

无效代码:

$items = array();

$items['project-menu/%'] = array(
  'title' => 'Test Link',
  'page callback' => 'dc_project_page',
  'page arguments' => array(1),
  'access callback' => TRUE,
  'type' => MENU_LOCAL_TASK,
);

return $items;

这些全部位于 dc_project.install 文件中的 dc_project_menu() 函数下。希望我只是做了一些愚蠢的事情,非常感谢任何和所有的帮助。即使只是向我指出一个可以干净地完成此操作的模块作为示例,谢谢。我确实查看了示例项目,但无法获得任何内容,无法将链接添加到我的新菜单工作。

Drupal 7 hook_menu() is confusing me; I have tried everything and I can't seem to get this to work.

What I need: In a custom module, I'd like to create a new menu, and add about four links to that menu. It sounds simple, but I am struggling. I've been able to create the menu itself using the $menu array in the .install file, but adding items to that menu doesn't make sense.

Code that is working:

$menu = array(
  'menu_name' => 'project-menu', 
  'title' => $t('Project Menu'), 
  'description' => 'Project Menu',
); 

menu_save($menu);

Code that isn't working:

$items = array();

$items['project-menu/%'] = array(
  'title' => 'Test Link',
  'page callback' => 'dc_project_page',
  'page arguments' => array(1),
  'access callback' => TRUE,
  'type' => MENU_LOCAL_TASK,
);

return $items;

This is all in the dc_project.install file under the dc_project_menu() function. Hopefully I'm just doing something stupid, any and all help is extremely appreciated. Even just pointing to me to a module that does this cleanly as an example, thanks. I did look at the example project, haven't been able to get anything as far as adding links to my new menu working.

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

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

发布评论

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

评论(1

飘然心甜 2024-11-08 22:45:35

传递到 menu_save() $items 的内容不起作用,因为 menu_save() 只接受包含 menu_name 的数组, 标题描述
$items 中使用的是一个描述模块实现的菜单回调的数组,所有模块实现的菜单回调的定义并不保存在“menu_custom”中(< code>menu_save()),但缓存在 Drupal 缓存表中。

如果您尝试更改另一个模块定义的菜单回调,那么您应该实现 hook_menu_alter();否则,如果您只想定义模块的菜单回调,则应该实现 hook_menu()

两个钩子实现(hook_menu()hook_menu_alter())都必须位于模块文件中(在您的情况下,位于 dc_project.module 中),而不是位于 dc_project.install 中。 Drupal在正常加载启用的模块时不会加载安装文件;它会在更新(或安装)模块时加载安装文件,但在其他情况下不会加载它。
menu_save()保存菜单的代码可以在安装文件中,在hook_install()hook_update_N()。它也可以放入

Passing to menu_save() the content of $items doesn't work because menu_save() accepts only an array containing menu_name, title, and description.
What you use in $items is an array describing the menu callbacks implemented by a module, and the definitions of the menu callbacks implemented by all the modules are not saved in "menu_custom" (the table used from menu_save()) but are cached in a Drupal cache table.

If you are trying to change the menu callbacks defined by another module, then you should implement hook_menu_alter(); otherwise, if you just want do define the menu callbacks of your module, you should implement hook_menu().

Both the hooks implementations (hook_menu() and hook_menu_alter()) must be in the module file (in your case, in dc_project.module), not in dc_project.install. Drupal doesn't load the installation file when it normally loads the enabled modules; it loads the installation file when a module is being updated (or installed), but it doesn't load it in other cases.
The code that saves the menu with menu_save() can be in the installation file, in the implementation of hook_install() or hook_update_N(). It could also be put in the implementation of hook_enable(); in that case, the code (which is executed when the module is enabled) should first verify the menu has not been already added. (hook_enable() and hook_disable() should be placed in the installation file.)

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