Drupal 7 not showing sub menu - drupal

i am new to drupal and creating custom theme, i am using Main Menu but it is now showing sub-pages, i am using following code to show.
print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix', 'main-menu'))));
Please let me know what to do ?
thanks in advance.

Using this method will not render the sub items of a menu item. In order to have a menu with multiple levels you can either:
Use the available Main menu block available under admin/structure/block
Change the $main_menu variable passed to the template by using a preprocess function
In template.php of your theme:
function YOURTHEME_process_page(&$variables) {
$menu_tree = menu_tree_all_data('main-menu');
$variables['main_menu'] = menu_tree_output($menu_tree);
}
In your template file (page.tpl.php)
<?php print render($main_menu); ?>

Related

Place a custom post type under a custom menu items

I'm trying to place a custom post type under the menus I made, Here is the screenshot of the menus. I want to add the custom post type together with that Menus. I used sandbox_theme_menu, but the custom post type is not appearing,
I tried adding this line in my register_post_type but still not appearing. Hmm any idea?
'show_in_menu' => 'edit.php?page=sandbox_theme_menu',
The problem comes because custom post_types should be created on WP init, but custom menu items cannot be added before the admin_menu hook.
While you know the position of the post_type in the menu, you can force it to be placed in another menu like this
Creating a new top menu item :
<?php
add_action('admin_menu', 'add_menu');
function add_menu(){
add_menu_page(
'Menu name',
'Menu name',
'publish',
'your_menu_slug',
'your_function_name'
);
}
?>
creating a custom post type :
(I only put here usefull parameters for the example)
<?php
add_action('init', 'create_cpt');
function create_cpt(){
register_post_type(
'your_cpt',
array(
'label' => 'Your CPT',
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 901 //(or any not used numeric value)
)
);
}
?>
At this point, your menu should show an item for the custom post type and another one for the custom page.
To move the custom post type under the custom page, add change the add_menu function like this :
<?php
function add_menu(){
add_menu_page(
'Menu name',
'Menu name',
'publish',
'your_menu_slug',
'your_function_name'
);
// get the global menu object
global $menu,$submenu;
// Add the top menu item to your custom page
$submenu['your_menu_slug'][] = $menu[901];
//(must be the same numeric value as in register_post_type)
// Finnaly, remove the initial menu item
unset($menu[901]);
}
?>
Now your custom post type should appear under the custom page menu item.
You have to use
'show_ui' => true
as an argument to register_post_type function to make 'show_in_menu' work. More information is available here.

Drupal 7: Make a custom module create a menu item in the user menu

Just like the title says. If you add an item to the hook_menu function and declare it of type 'MENU_NORMAL_ITEM' it shows up in the navigation menu. Is there a way to create an item there that shows up in the user menu?
See the drupal documentation for the hook_menu function. with your custom menu items you can declare 'menu_name', and you pass the name of the menu you want your item added to. In your case it would be 'user-menu':
<?php
function mymodule_menu(){
$items['mymodulepath'] = array(
'title' => 'My Module User Item',
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'user-menu',
);
return $items;
}

Use node-menu on page

I would like to use the node-menu (see image) on a page in Drupal.
Is this possible and, if yes, how?
If the page you are referring is a custom page output from your module, and "mymodule/page" is the path for that page, for which you want the tabs "View" and "Edit," then you should implement hook_menu() using code similar to the following one:
function mymodule_menu() {
$items = array();
$items['mymodule/page'] = array(
'page callback' => 'mymodule_page_view',
'access arguments' => array('view mymodule page'),
);
$items['mymodule/page/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['mymodule/page/edit'] = array(
'title' => 'Edit',
'page callback' => 'mymodule_page_edit',
'access arguments' => array('edit mymodule page'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
If the page you are referring is a page that is shown at example.com/mymodule/page, and that should show what you see at example.com/node/7, then you can implement the following code, in Drupal 7:
function mymodule_url_inbound_alter(&$path, $original_path, $path_language) {
if (preg_match('|^mymodule/page|', $path)) {
$path = 'node/7';
}
}
The equivalent for Drupal 6 is writing the following code in the settings.php file:
function custom_url_rewrite_inbound(&$result, $path, $path_language) {
if (preg_match('|^mymodule/page|', $path)) {
$result = 'node/7';
}
}
I didn't write the symmetric code for hook_url_outbound_alter(), and custom_url_rewrite_outbound() as I suppose you are not interested in rewriting example.com/node/7 to make it appear as example.com/mymodule/page, but you are interested in making appear example.com/mymodule/page the same as example.com/node/7.
Okay; a node page usually has those View and Edit tabs. So my next question is to wonder why they aren't appearing on your node page already. Have you by chance created a custom page template for this node type and removed the code that prints the tabs? Or is there a chance you're logged in as a user that doesn't have permission to edit this type of node?
There must be a reason why you're not getting those tabs; they should be there, by default.
If you do have a custom page template for this node type, look for code that looks something like this:
<?php if ($tabs): ?>
<div class="tabs"><?php print $tabs; ?></div>
<?php endif; ?>
If you don't see code like that, try adding it.
If you DO see code like that, try to isolate what's different about this content type compared to other content types where you DO see those tabs.

How to print a custom menu in Drupal 7?

I have created a menu in Drupal 7 and created links to pages under that menu.
I named my new menu "Site Menu"
In my page.tpl.php where I want my menu to appear I have put this in place:
<?php print theme('links', menu_navigation_links('menu-site-menu')); ?>
After I have cleared my cache and refreshed my page my menu doesn't appear.
I am stumped. Any help would be greatly appreciated.
Berdir answer is correct. Drupal 7 theme_links function also more vastly uses arrays. For example if you would like to add another class name to the so that it is you would code it like this:
<?php print theme('links', array('links' => menu_navigation_links('menu-site-menu'), 'attributes' => array('class'=> array('links', 'site-menu')) ));?>
theme() now recieves an array of arguments. For example:
<?php
print theme('links', array('links' => menu_navigation_links('menu-site-menu')));
?>
Well, it is bit confusing from above solutions to print menu. But below code worked for me, hope this will work for y'all,
$search_menu_name = "menu-search-box-menu";
print theme('links', array('links' => menu_navigation_links($search_menu_name), 'attributes' => array('id' => $search_menu_name, 'class'=> array('links', 'inline'))));
The above code is like this, "menu-search-box-menu" is my custom menu name/ID. You can find it in that particular menu edit link.
Enjoy. :)

Drupal programmatically adding an item to a menu

I wish to conditionally add an item to a menu. I have a custom module and a menu called "links". How would I add an item to a menu in my module code?
You need to implement hook_menu in your module. Example:
<?php
function mymodule_menu() {
$items['mymodule/links'] = array(
'title' => 'Links',
'page callback' => 'mymodule_links_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
?>
The 'type' => MENU_SUGGESTED_ITEM, part makes it optional, so it can be enabled by the end user - is that what you meant with "conditionally"? If not, please explain what kind of "conditionally" you're looking for.
Or you can use the 'type' => MENU_NORMAL_ITEM, since it is enabled by default, but can be disabled any time. This of course depends on your preferences. See http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7 for further reference.
Another good thing to know when using module defined menu items in custom menus might be how to programmatically create the menu you want to use so that everything is created "out of the box". Simply add a mymodule.install-file where you put the following code:
<?php
function mymodule_install() {
$menu = array(
'menu_name' => 'links',
'title' => 'My Custom Links',
'description' => 'Descriptive text.',
);
menu_save($menu);
}
?>
If you have an uninstall function, don't forget to not only deactivate the module, but also to uninstall it. Reenable the module, flush your caches and the menu item should be there!
You can dynamically show or hide a menu item based on a condition ( access callback ).
Here is an example from https://drupal.org/project/examples:
<?php
function mymodule_menu() {
$items = array();
$items['my-menu-item'] = array(
'title' => 'My Menu',
'description' => 'My description',
'page callback' => 'my_page_link_callback_function_name',
'access callback' => 'can_the_user_see_this_item',
'expanded' => TRUE,
'weight' => -100,
'menu_name' => 'primary-links',
);
return $items;
}
// Here we determine if the user can or can not see the item.
function can_the_user_see_this_item(){
if (MY_CONDITION){
return TRUE;
}
else {
return FALSE;
}
}
The menu system is cached, so you can't add or remove menu items as you please based on user, page viewed, custom logic etc. That is you can't do it without having to clear the menu cache which would cause a severe performance hit.
What you could do, to create this effect is to create some custom logic to define the access control on the menu item. Since Drupal hides menu items that users doesn't have access to, you could under certain circumstances deny permission to hide the menu item. This is a bit hackish solution.
Another solution which I would prefer, would be to use js or css to hide or show the menu. You could dynamically add/remove a class on the body to determine if the menu item should be shown or not. This would quickly become unmanagable if you need several of these kind of menu items, however.
Use menu_link_save() function
Saves a menu link.
After calling this function, rebuild the menu cache using menu_cache_clear_all().
Parameters
$item: An associative array representing a menu link item, with elements:
link_path: (required) The path of the menu item, which should be normalized first by calling drupal_get_normal_path() on it.
link_title: (required) Title to appear in menu for the link.
menu_name: (optional) The machine name of the menu for the link. Defaults to 'navigation'.
weight: (optional) Integer to determine position in menu. Default is 0.
expanded: (optional) Boolean that determines if the item is expanded.
options: (optional) An array of options, see l() for more.
mlid: (optional) Menu link identifier, the primary integer key for each menu link. Can be set to an existing value, or to 0 or NULL to insert a new link.
plid: (optional) The mlid of the parent.
router_path: (optional) The path of the relevant router item.
$existing_item: Optional, the current record from the {menu_links} table as an array.
$parent_candidates: Optional array of menu links keyed by mlid. Used by _menu_navigation_links_rebuild() only.
Return value
The mlid of the saved menu link, or FALSE if the menu link could not be saved.

Resources