Drupal add menu link to user menu - drupal

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.

Related

Dynamically creating a link to a custom made page with Drupal Module

So I want my module to make a page first. But the link to the simple page should be user entered.
I`m doing this on Drupal 7.
I made a settings form in the _menu function.
$items['admin/config/content/settings'] = array(
'title' => 'Setings',
'description' => 'A form to change settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('settings_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
Then I added this:
function settings_form($form, &$form_state){
$form['locationUrl'] = array(
'#type' => 'textfield',
'#title' => t("URL kādu vēlaties?"),
'#description' => t("Example. about"),
'#required' => true
);
return system_settings_form($form);
}
And then i added to the _menu function:
$items[variable_get('admin/config/content/settings','locationUrl')] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => '', //page title
'page callback' => 'DGMap', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'access arguments' => array('access simple page'),
);
And i add this:
function page(){
return array('#markup' => '<h1>Hello?</h1>');
}
I save it all up. Clear cache. And go to settings and save the locationUrl as for example 'about'. And then try /drupal/about it gives me page not found exception.
Could anyone please help me with this? I hope that it is understandable what im trying to make.
Thanks for help.
The end thing i want to make is so that my module could create a page with custom JavaScript in it. If anyone could link me to a tutorial that would be great for this, i would really appreciate that. If there is a way for my module to place custom created JavaScript in the page that would be fine too.
Not sure if it's a good aproach for dynamic routing but you're getting the variable wrong for your second link in hook_menu(). In fact you make a route to YourDrupal/locationUrl instead of your example YourDrupal/about.
Use this for the second menu link (with an extra check on whether or not the setting is being saved):
if (!empty($setting = variable_get('locationUrl', ''))) {
$items[$setting] = array(
'title' => '', //page title
'page callback' => 'page', // use the same name as your pagecallback
// 'access arguments' => array('access simple page'),
'access arguments' => array('access content'), // temporarily use this
);
}
You also might be in need of a check on existing routes. Not sure what happens if the user has saved your form with a locationUrl like for example "admin" giving a link to YourDrupal/admin which already exists.

Drupal 7: Add Item to Admin Toolbar/Menu Programmatically

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.

How to display node fields in tabs?

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]]

Creating a menu item that links to its parent

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.

Creating drupal 6 module with tabs

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.

Resources