I've been searching low an high to do this. I want to create a normal menu item programatically that links to its parent. Similar to the concept of default menu task, but just a normal menu item. Has anyone ever done this?
I want something that I can do with one of the drupal hooks.
You just need to create a new menu-item, that has the same path as its parent.
$items['parent'] = array(
'title' => 'I am parent',
'page callback' => 'drupal_get_form',
'page arguments' => 'get_parent',
'access arguments' => array('access parent'),
'type' => MENU_NORMAL_ITEM,
);
$items['parent/child'] = array(
'title' => 'I am child link of parent',
'page callback' => 'drupal_get_form',
'page arguments' => 'get_children_for_thisparent',
'access arguments' => array('access children of parent'),
'type' => MENU_NORMAL_ITEM,
);
This should do the trick :)
Suppose you want to place it under Main menu,
$item['form_example'] = array(
'title' => 'Example menu',
'description' => 'This is an example menu item',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'custom_function',
'menu_name' => 'main-menu',
);
return $item;
You will then get the menu item under Main menu (clearing cache). The trick is adding a key 'menu_name'.
To get the menu_name please open the menu administration page, and click on 'edit menu' on any root menu. URL will be like 'SITE_URL/admin/structure/menu/manage/main-menu/edit'. Look at the url segment just before the last(here main-menu).
For the second case you might want to place it under Home of main menu. The code is.
$item['form_example'] = array(
'title' => 'Example menu',
'description' => 'This is an example menu item',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'custom_function',
'menu_name' => 'main-menu',
'plid' => 218,
);
return $item;
Here, I have added a key plid to associative array, it is the mlid of the menu item('Home' here) for which it will be a child. For this case it will be child of Home menu.
To get the mlid you have to do the same as described above, goto menu administration page and click list links and then click edit on the menu item, e.g. SITE_URL/admin/structure/menu/item/218/edit, thus you can get the mlid and thus its done.
Note. If you change this menu hierarchy or other settings from back-end, you will always have reset option to reset it, and after resetting it will behave like described in the code.
Related
In Drupal 7 module development, supposedly by using the hook_menu function, how can I add menu link to the user menu?
Here.
I found easily it in the administration but I can't find how to do it programmatically, I haven't found any menu type that would fit this situation.
Thanks.
You could specify the menu_name in the data returned by your hook_menu():
function MYMODULE_menu() {
$items['example'] = array(
'title' => 'Example Page',
'page callback' => 'example_page',
'menu_name' => 'user-menu', // << Menu name
'weight' => 12, // << position
'access arguments' => array(
'access content',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
By default, it is added in the Navigation menu.
I'm building a fairly involved module for my company with a number of different configuration pages. I'd like there to be a menu item in the admin bar across the top that has all the sub menu items as well. I know how to add a single item to that menu through the UI, but there will be enough pages I'd just prefer to do it through the module itself. So, how can I add an item with a submenu to sit up alongside 'Dashboard', 'Content', 'Structure', etc. in the admin menu in my module file. I assumed it had to be in hook_menu(), but I can't figure it out.
This could be achieved by adding a 'page callback' of system_admin_menu_block_page to your hook_menu implementation:
So, lets say you want to create a structure like the following :
Custom main menu (will appear on the toolbar, besides other items like Structure, Modules, etc)
Sub menu item 1
Sub menu item 2
The hook implementation would be something like :
function MODULE_menu() {
$items['admin/main'] = array(
'title' => 'Custom main menu',
'description' => 'Main menu item which should appear on the toolbar',
'position' => 'left',
'weight' => -100, // Less weight so that it will appear to the extreme left, before dashboard.
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/main/sub-menu-1'] = array(
'title' => 'Sub menu item 1',
'description' => 'Child of the menu appearing in toolbar.',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_form'),
'access arguments' => array('custom permission'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/main/sub-menu-2'] = array(
'title' => 'Sub menu item 2',
'description' => 'Child of the menu appearing in toolbar.',
'page callback' => 'custom_page_callback',
'access arguments' => array('custom permission'),
'type' => MENU_NORMAL_ITEM,
);
}
P.S - After enabling the module, or adding this code to the hook_menu implementation, you'll have to flush the cache so that Drupal picks up the new menu structure.
I have a content type "activities", which has three fields:
1- Programs
2- Implementation
3- Project Stories
How can I display each field in the node in a separate tab?
Thanks!
I have found an easier way to do it using the field_group module. From "Manage Display" the fields can be added to horizontal tabs fieldgroups and then the horizontal tabs fieldgroups can be added to a horizontal tabs group. See the image for further information.
In my opinion there are two ways you can achieve this.
1) Using the hook_menu() to create the tabs for your content type.
Here you will have to write your own module and the code will look something like this
/**
* Implements hook_menu().
*/
function pages_menu() {
$items['pages'] = array(
'title' => 'Menu system examples',
'description' => 'Menu system example that returns a string.',
'page callback' => 'pages_string',
'access callback' => TRUE,
);
$items['pages/default'] = array(
'title' => 'String',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['pages/render-array'] = array(
'title' => 'Render array',
'description' => 'Menu system example using a render array.',
'page callback' => 'pages_render_array',
'access arguments' => array('access content'),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
$items['pages/render-array/tab1'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'Tab 1',
);
$items['pages/render-array/tab2'] = array(
'title' => 'Tab 2',
'description' => 'Demonstrating secondary tabs.',
'page callback' => 'pages_render_array',
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
You then use the call back function to do your think in each of the tabs
2) Using Css and jquery to style the content in a way that it looks like a tab.
here is a great working demo for you. http://www.99points.info/2010/08/create-sexy-animated-tabs-using-jquery-and-css/
Cheers,
Vishal
A quick search on Drupal moduels gets this:
D6 - http://drupalmodules.com/module/cck-fieldgroup-tabs
D7 - http://drupal.org/project/field_group
One other module to consider, if anyone else comes looking for an answer: http://drupal.org/project/node_subpages
[[shameless plug]]
I'm creating a module that let's your users add feeds.
So i want my code to provide tabs that can be overwritten at the theme layer.
I thought this could be done with the hook_menu:
$items['tab_add_feed'] = array(
'title' => 'Add Feed',
'page callback' => 'xml_parser_add_feed',
'access callback' => 'user_access',
'access arguments' => array('manage own feeds'),
'type' => MENU_LOCAL_TASK,
);
something like the above.
But I'm using it on the front side of the site.
How can i add tabs or links on top of my page the drupal way?
//edit
There are none tabs present at the moment, maybe i have to make them visible?
fixed it by adding add feed to the main page callback function
But this is ugly, hard coded and not theme-able. waiting for a better solution.
//edit
This is the code i am using now
function xml_parser_menu() {
$items = array();
$items['xml_parser'] = array(
'path' => 'xml_parser',
'title' => t('Feed'),
'description' => t('Add/edit feeds'),
'page callback' => 'xml_parser_manage_overview',
'access callback' => 'user_access', // get user access
'access arguments' => array('manage own feeds'), // check user if premission is set
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'primary-links', // add to primary menu
);
$items['xml_parser/add_feed'] = array(
'path' => 'xml_parser/add_feed',
'title' => 'Add Feed',
'page callback' => 'xml_parser_add_feed',
'access callback' => 'user_access',
'access arguments' => array('manage own feeds'),
//'access' => user_access('maintain own subscriptions'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
I think the name of the item would be something like 'user/%/add_feed', with the % argument being the user id. Also, the access callback is spelled incorrectly, should be user_access. This should create a tab for a user on the user profile page. You could also do node/%/add_feed to add a tab to a node.
While developing this module, you may find it useful to also use this:
function mymodule_init() {
cache_clear_all();
menu_router_build();
}
Until you get the menu straight.
If you want to just add an arbitrary tab to a page to add a feed, it would probably be an autonomous menu or a themed link. I would need to know more about the context of the feeds you are trying to provide and how users are subscribing.
You should be able to add a tab to the front page. Remember that the front page of your website is not really "/", it is "/node" by default (unless you changed it).
I'm just guessing because I haven't tried this, but your code should work if you change the key for your tab in $items to node/tab_add_feed.
If you have something else as your front page (ie. a view or panel), this would not apply.
I want to add a tab to Drupal node like in the following picture:
The picture has 3 tabs, Views, CVS Instructions, Revisions. I want to add another tab "Translation". What module should I use?
The picture was taken from http://drupal.org/project/panels_tabs
Thank you.
I would create a simple small module that has a hook_menu implementing the tab.
See the example here:
http://drupal.org/node/678984
As for the rest of your implementation, I don't know what you are trying to achieve, but this will add tabs.
Not sure if this is relevant but if you are actually looking to translate node content then have you investigated the Internationalization module?
The translation tab is handled by the module "Content translation" that depends from "Locale"; once you enabled the module, you need also to set which content types can be translated, and other settings that change the way a node of that content type is translated.
Not quite what was asked, but here is code for hook_menu in a custom module that sets up an administration menu option with 2 tabs.
/***************************************************************
* hook menu
*/
function acme_viewer_setup_menu(){
$items = array();
// administration setting - call from URL
$items['admin/settings/acme_viewer_setup'] = array(
'title' => 'Acme Misc Setup - viewer and Blog', // title in Admin menu
'description' => 'Acme Misc Setup: acme viewer & Blog',
'page callback' => 'drupal_get_form', // Retrieves form 'acme_viewer_setup_admin'
'page arguments' => array('acme_viewer_setup_admin'),
'access arguments' => array('access administration pages'), // only users who can access admin pages
'type' => MENU_NORMAL_ITEM,
);
// tab 1 - viewer
$items['admin/settings/acme_viewer_setup/viewer'] = array(
'title' => 'Configure viewer', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('acme_viewer_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
// tab 2 - blog
$items['admin/settings/acme_viewer_setup/blog'] = array(
'title' => 'Configure Blog', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('blog_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}