How to add visible menu item from php code (drupal)
I have a content type with important "created" date field. And I have a menu link (in primary links) which is link to page that shows list of all nodes of my content type. (http://example.com/mycontenttype) I want to have menu links (visible in primary links) to each year when nodes of my content type are displayed (http://example.com/mycontenttype/2010). And I want to add these menu links from mymodule_nodeapi function, when the node is creating and only if i开发者_开发百科t has a "created" date of new year. I know that I can create a pathes in mymodule_menu function, but it doesn't create a visible menu item. (Maybe I can somehow set parent_link_id or something else to do it?)
You can create a visible menu item with hook_menu() if you set it's 'type' to MENU_NORMAL_ITEM. Also, you can nest these menu items by defining a path that includes the path of the parent item. For instance, 'mycontenttype/2010' would be a child menu item of 'mycontenttype' (I believe you can also manually set the parent/child relationship using Drupal's menu admin form. You can also set the menu the item should be added to using the 'menu-item' value to automatically add it to Primary Links instead of Navigation.
As for creating a new menu item whenever a node is created with a new year, you'll have to play around with it a little. I think it'd be easy to determine what unique years are available for nodes of that type (using a view or a querying the database manually). To update the menu tables when a new menu item is added, you can call menu_rebuild().
I think the way to hook it all together would be to write your hook_menu function to query the database to find unique created date years and create a menu item for each year. In your hook_nodeapi, check whether or not the created year is new (there must be ways to optimize this so it doesn't get called every single time you create the node, but i'll leave that to you). If it is a new year, then clear the menu tables (menu_rebuild) to recreate the menu items and include one for the new year.
精彩评论