I want to create a dynamic menu that will get it's items from a certain node type. I thought I could do this by creating a view of the titles and putting it in a block. However, when someone clicks on one of these titles I want to highlight it, and so want a way of adding an active class to the link. I know Drupal does this automatically for menus, but can I do it for开发者_开发百科 a menu based on a view?
why not use Andrews idea in conjunction with hook_form_alter? Something like this would work:
Assuming your content type is called "mycontent":
Under your content type settings set the allowed menu parents to only include the menu you want, we'll call it "custom". In your module file add the following code:
mymodule_form_alter(&form, &$form_state, $form_id){
if($form_id=="mycontent_node_form"){
$form['menu']['enabled']['#default_value'] = 1;
$form['menu']['link']['parent']['#default_value'] = "menu-custom:0";
}
}
so, $form_id should equal "[my_content_type]_node_form", and the default value of parent should equal "menu-[my_menu]:0"
If you are concerned about users not adding their content to a menu, this line will take away their control of the form item:
$form['menu']['#disabled']=true;
I'm not real sure about your requirements for a "dynamic menu" but an alternative to the view-of-titles might be to set up a custom menu, then when you create these nodes, set them to be part of that menu in the node edit form. Then display that menu in the block And you should get the highlighting of active items you want.
精彩评论