Drupal 6: Make a menu_hook() return a specific view
I created a hook in order to add an item to the administrator's m开发者_运维百科enu. When the user clicks on the item, I want to return the content of a specific view I created. How should I return the view?
My current code looks like:
function my_view_menu(){
$items['view'] = array(
'title' => 'Report',
'page callback' => 'return_my_view',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function return_my_view(){
return t("Hello!");
}
EDIT:
As suggested by Berdir, this the correct way to call a view:
function return_my_view(){
$viewName = 'my_report'; // use the machine readable name of the view
return views_embed_view($viewName);
}
You could just add a menu item in the view itself...and restrict access (to the view) to the admin role of choice :)
- In your view choose "page" and click on the "Add Display" button (if there isn't already a page display).
- Under "Page Settings" add a Path and a Normal Menu-Entry in the Navigation Menu
- Next Under Basic Settings change the access to Role based and choose the role(s) that should have access
- Finally go to the navigation menu settings and drag the new menu item to the desired place in the Administer menu
You want views_embed_view(), see http://web.archive.org/web/20110213234806/http://thedrupalblog.com/embedding-view-drupal-6-using-views-embed-view
views_embed_view() is the correct call. If you are getting a blank page, try checking your apache error log to see if there are any php errors. I also notice that in your revised example you used $viewName = "my-report", but views_embed_view() expects the machine readable name of the view, which only allows for alphanumeric and underscore characters. Perhaps you are using the incorrect name?
Third technique: Once you have created a Page Display for a View, Views will provision that page with a menu entry. Once that exists, it is possible to duplicate that menu entry for your own purposes.
- Create a module with a weight of at least 11 (higher weight than Views)
Implement
hook_menu_alter()
and duplicate the View entry.function example_menu_alter(&$items) { $items['admin/new/path'] = $items['original/view/path']; }
This approach is somewhat convoluted, but is sometimes a useful alternative for Views or other "page" content you want to clone.
In addition to berdir's comment, you can also skip the intermediate callback function and just call views_embed_view directly from your menu router:
function hook_menu(){
$items['path/to/my/view'] = array(
'title' => 'Report',
'page callback' => 'views_embed_view',
'page arguments' => array('my-view-name'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
精彩评论