Drupal 6:使 menu_hook() 返回特定视图
我创建了一个挂钩,以便将一个项目添加到管理员菜单中。当用户单击该项目时,我想返回我创建的特定视图的内容。我应该如何返回视图?
我当前的代码如下所示:
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!");
}
编辑:
正如 Berdir 所建议的,这是调用视图的正确方法:
function return_my_view(){
$viewName = 'my_report'; // use the machine readable name of the view
return views_embed_view($viewName);
}
I created a hook in order to add an item to the administrator's menu. 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只需在视图本身中添加一个菜单项...并将访问(对视图)限制为所选的管理员角色:)
单击“添加显示”按钮(如果还没有页面显示)。
“页面设置”添加路径和
导航中的正常菜单条目
菜单
将访问权限更改为基于角色和
选择应该具有的角色
访问
导航菜单设置和拖动
将新菜单项添加到所需的位置
位于管理菜单中
You could just add a menu item in the view itself...and restrict access (to the view) to the admin role of choice :)
on the "Add Display" button (if there isn't already a page display).
"Page Settings" add a Path and a
Normal Menu-Entry in the Navigation
Menu
change the access to Role based and
choose the role(s) that should have
access
navigation menu settings and drag
the new menu item to the desired
place in the Administer menu
您想要views_embed_view(),请参阅http://web.archive.org/web/20110213234806/http://thedrupalblog.com/embedding-view-drupal-6-using-views-embed-view
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() 是正确的调用。如果您收到空白页面,请尝试检查 apache 错误日志以查看是否存在任何 php 错误。我还注意到,在您修改后的示例中,您使用了 $viewName = "my-report",但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?
第三种技术:一旦您为视图创建了页面显示,视图将为该页面提供菜单条目。一旦存在,就可以出于您自己的目的复制该菜单项。
实现
hook_menu_alter()
并复制视图条目。这种方法有些复杂,但有时对于您想要克隆的视图或其他“页面”内容来说是一个有用的替代方法。
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.
Implement
hook_menu_alter()
and duplicate the View entry.This approach is somewhat convoluted, but is sometimes a useful alternative for Views or other "page" content you want to clone.
除了berdir的注释之外,您还可以跳过中间回调函数,直接从菜单路由器调用views_embed_view:
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: