Drupal hook_menu from module for admin menu - drupal

I have a custom module "menu_mods" for adding menu items to the admin menu. It's not adding it. I want the link to show in the Navigation menu. I'm using the Garland theme for the admin pages.
Here is my module code:
function menu_mods_menu() {
$items = array();
$items['admin/editfrontpage']=array(
'title'=>'Edit Homepage',
'description'=>'Edit Homepage.',
'page callback' => 'edit_front_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
}
function edit_front_page(){
$frontPageUrl = drupal_get_normal_path(variable_get('site_frontpage', 'node')); // outputs "node/112"
$frontPageUrl = $frontPageUrl.'/edit';
drupal_goto($frontPageUrl);
}
Any idea why it's not displaying? After I make a change, I go to the modules page and then to the menu page.
thanks

WOW! Silly mistake.
I forgot to return the $items at the end of the function.
return $items;
By the way, this little function gives you an edit link to edit the front page of your site.

Have you cleared the menu cache yet? Not sure if going to the module page does that.

Related

Create drupal (sub-)menu

OK, so I have a weird thing to do, I'd appreciate any help. When you go to the Drupal admin panel and click Structure, you get a menu which contains Blocks, Content types, Menus and so on.
Is there a way I can programatically build one of those menus based on the path? For example if I have my module named test and all sub-actions of my module are located at www.drupalsite.com/admin/test/action_name, can I build my menu with all the /test/action_name there exist in the current module?
I know there's the option of hard-coding the menu, but I want to avoid it if possible.
It's hard to be that descriptive without some more information but you'd just need to implement hook_menu() and loop through your list of actions, creating a menu item for each. Every time the menu is rebuilt your menu hook will be called and the current list of actions will be built as menu links. Something like this:
function mymodule_menu() {
$actions = mymodule_get_actions_list();
foreach ($actions as $action) {
$items['admin/test/' . $action->name] = array(
'title' => $action->name,
'access arguments' => array('some permission'),
'page callback' => 'mymodule_callback',
'page_arguments' => array($action->name)
);
}
return $items;
}
function mymodule_callback($action_name) {
// Load the action and display the page
}
After you call your custom code to create one of these actions, be sure to call menu_rebuild() so your hook runs and the new action is added to menu.

Menu path with wildcard

You can't use wildcards in menu paths? A quick summary of my problem (which I've made sure makes sense, so you're not wasting your time): I have a menu which i'm showing on node pages of a certain content-type. My path to a node page would be like...
events/instal2010
...where instal2010 would be the name of an event (event is the content-type).
I'm using the Context and Menu Block modules to place a menu in the sidebar on that page...
Event (the default active item)
Programme
Visitor info
Book tickets
... where the path for Programme would be
events/instal2010/programme
So for this to work for many different events, those menu items need a wildcard in their path, e.g.
events/*/programme
Perhaps it's time to ditch menus and just use a block with php to determine what page we're on from the URL.
Any advice from experienced hands would be awsome, thanks.
You cannot create menu items with wildcards from the administrative interface of Drupal, but you can create menu items with wildcards in a module. I would recommend creating a custom module that uses hook_menu() to create the menu items. An example implementation would look something like:
function YOURMODULE_menu() {
$items = array();
$items['events/%/programme'] = array(
'title' => 'Programme',
'description' => 'Loads a program page',
'page callback' => 'YOUR CUSTOM FUNCTION NAME', // Custom function used to perform any actions, display the page, etc
'page arguments' => array(1), // Passes wildcard (%) to your page callback function
'access callback' => TRUE, // Change if you want to control access
'type' => MENU_NORMAL_ITEM, // Creates a link in the menu
'menu_name' => 'primary-links' // Adds the link to your primary links menu, change if needed
);
return $items;
}
In $items['events/%/programme'] = array(, the % is the wildcard and it will be passed to your page callback function. It may be helpful to read more about hook_menu() and the Anatomy of hook_menu may also help as well.

Drupal 6 - force page view to display a different view?

Kind-of a crazy question here...
I have a view display that's set up as a page. It looks great in theme A (desktop), but terrible in theme B (mobile). So I made a different version of the view for theme B. Since the desktop/mobile 'sites' are the same just with different themes, the url for this page will be the same regardless of hte theme selected.
So I would like to be able to point the user to:
mysite/this_crazy_view
and have the returned page select the proper view depending on which theme it's in. I know that if I were using blocks I would just assign the appropriate blocks to the page in question on a theme-by-theme basis, but since the displays are using page display I don't know what the right approach would be.
I would rather not rebuild the views as blocks if I can help it (if it can't be helped, so be it...) so I was hoping there was some way to conditionally load the view via the tpl.php file or something like that...
The code I'm using in my module (per #Clive 's recommendation below) is:
<?php
function bandes_custom_hook_menu() {
$items['charley_test'] = array(
'title' => 'Title',
'access arguments' => array('access content'),
'page callback' => 'bandes_custom_set_page_view',
'type' => MENU_NORMAL_ITEM );
return $items;
}
function bandes_custom_set_page_view() {
global $theme_key;
$view_name = $theme_key == 'mobile_jquery' ? 'course_views_mobile' : 'course_views';
$display_id = 'page_5';
return views_embed_view($view_name, $display_id);
}
?>
I've cleared the cache a number of times and tried a variety of different paths in the $items array. The course_views and course_views_mobile both definitely work on their own.
I was also wondering if I could just create a views-view--course-views--page-5.tpl.php which contains almost nothing aside from the views_embed_view(course_views_mobile, page_5) part? (Only on one of the two themes...)
Actually I think the answer was simpler than all of the above. The redirect thing was giving me fits, so I removed the module, reset the paths to what I had been using, and tried the template/theme approach instead.
This is: views-view--course-views--page-5.tpl.php, only used on the mobile theme, but referring to the non-mobile view (kinda gives me a headache, but it works)
<?php
//get the view
print "IM IN YR VUE, MESSING THNGZ UP!"; //yeah, I'm going to remove this part...
$view_name="course_views_mobile";
$display_id="page_5";
print views_embed_view($view_name, $display_id);
?>
Any reason that shouldn't work? (Or why it is a really bad idea?)
Me again :)
I just thought of an easy-ish way around this actually; if you can change the URL of your views to something other than the path you want to access them at (any path would do) you could implement a hook_menu() function in a custom module for that path, to make the choice depending on your theme:
function MYMODULE_hook_menu() {
$items['this_crazy_view'] = array(
'title' => 'Title',
'access arguments' => array('access content'),
'page callback' => 'MYMODULE_crazy_view_page',
'type' => MENU_CALLBACK // or MENU_NORMAL_ITEM if you want it to appear in menus as usual
);
return $items; // Forgot to add this orginally
}
function MYMODULE_crazy_view_page() {
global $theme_key;
$view_name = $theme_key == 'foo' ? 'theView' : 'theOtherView';
$display_id = 'page_1'; // Or whatever the page display is called
return views_embed_view($view_name, $display_id);
}
That should do the trick

Drupal: Dynamic View using Arguments

For a current project i need to setup a specific view to display a gallery detailpage. It should work like this:
1. User clicked a node (costum-post-type: gallery)
2. User received an overview page with linked images
3. User clicked an image
4. User received the gallery page (gallerific view)
Step 1-3 are done. But how can I get Drupal to build a detail page using the data of the overview page?
For Example something like this: http://example.com/gallery-1/detail or http://example.com/gallery-2/detail.
/gallery-n is the overview page with linked images and detail is the detailpage of /gallery-n.
Hope you'll understand what i mean?!
EDIT
On the overview page i have a bunch of thumbails which each are linked to the detail gallery (jquery galleriffic) page.
If I'm correct understand your problem you should do this things.
1. Create view1 for page with linked images. It should be page display with http://example.com/images/%nid
where %nid is nid argument of gallery.
2. Create view2 for gallery detailed page. it should be page display with http://example.com/%nid/detail
3. Theme that views as you want.
4. For view1 for image field use override output in field settings to make it links to %nid/detail
P.S. Use relationships where needed. If description is not clear, fill free to ask.
You can try something like this, in a custom module you make (or maybe already have):
where you set the path to the page you want in the menu and set it as a callback that calls a function and then you can render whatever you want, or call whatever you want.
function MODULENAME_menu() {
$items = array();
$items['gallery/%/detail'] = array(
'title' => 'Gallery Detail',
'page callback' => 'MODULENAME_gallery_detail_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function MODULENAME_gallery_detail_page($gallery_id) {
// Here you can render the view as a page, using the gallery
// id which you passed as a parameter to this function.
// So Change MYCUSTOMVIEW to the view you want to render
$view = views_get_view('MYCUSTOMVIEW');
print views_build_view('page', $view, array(), false, false);
}
Just change MODULENAME with the name of your module. You might need to do some work when calling the views_build_view, but it should be a start, you can ask some more questions if you like and I'll help out.

What is the difference between MENU_NORMAL_ITEM and MENU_CALLBACK?

What is the difference between MENU_NORMAL_ITEM and MENU_CALLBACK?
The more precise answer is that hook_menu() creates router items, and also menu links are generated. MENU_NORMAL_ITEM generates a menu link which will appear in the navigation menu, while MENU_CALLBACK does not add a menu link, so it won't appear in the menu.
MENU_NORMAL_ITEM creates a menu item while MENU_CALLBACK doesn't. That is the only difference.
Addition to the above comment, MENU_CALLBACK can be used in some scenarios such as AJAX.
Example: example.com/ajax/country_list is a MENU_CALLBACK which returns a list of countries in HTML,JSON or XML format... This menu doesn't appear in the browser.
You can visit http://api.drupal.org/api/group/menu/6 for more information.
Drupal maps urls to functions.
Means you need a function for every URL.The function is mostly present in a module.
ex mysite/add will have a mapping to a function in a module.
Many cases we don't want the URL as as a menu item but intend to use it for other purposes. The best example being a Ajax callback.
Ex: you have an auto-suggest form which calls a function suggest in the server.The front end Ajax will need a url to fire the request.Let the url be www.mysite/suggest
This is the case when you need a MENU_CALLBACK
function example_menu() {
$items['suggest'] = array(
'page callback' => 'example_suggest',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function example_suggest() {
//you can return the autosuggested items to the page
}

Resources