Drupal 7 中的程序化视图 [英] Programmatic Views in Drupal 7
问题描述
我正在尝试创建两个视图.
I'm trying to create two views.
View-1 是节点列表.
View-1 is a list of nodes.
View-2 是与每个节点关联的图片库.
View-2 is an image gallery associated with each node.
我基本上想将节点标题从 View-1 传递给程序化的 View-2,以便 View-1 中的每一行都将加载 View-2(结果集由 View-1 的标题过滤!).
I basically want to pass the node title from View-1 to a programmatic View-2, so that each row in View-1 will load View-2(with a result set filtered by the title of View-1!).
我对这种方法感到困惑.这应该发生在自定义模块、预处理函数还是它们的某种组合中?
I'm confused about the approach. Should this happen in a custom module, preprocess functions, or some combination thereof?
我经常遇到这种情况 - 想要将参数从主视图传递到显示每个结果的辅助视图.
I run into this a lot - wanting to pass an argument from a primary view to a secondary view that displays with each result.
我意识到这个问题有点笼统,但我很好奇有更多经验的人会如何解决这个问题.
I realize that the question is a bit general, but I'm curious how folks with more experience would approach this problem.
推荐答案
就我个人而言,我会完全避免使用视图.
Personally I would avoid views here alltogether.
一个简单的模块,使用 hook_menu
来定义菜单项和两个处理所需参数的简单 menu-callback-functions.
A simple module using a hook_menu
to define the menu-items and two simple menu-callback-functions dealing with the required parameters.
另一种方法是让视图知道所有自定义参数和自定义查询过滤和表.
The alternative would be to make all the custom parameters and custom query-filtering and tables known to views.
我的(个人的)经验法则是:
My (pseronal) rule of thumb is:
- 如果您确定将在未来的项目中多次重复使用代码,那么值得投资一个视图插件.
- 如果您将使用视图界面进行一次性设置(一般用途),例如在编辑器/网站管理员工作流程中定义或更改视图,这是有道理的.
它的基础非常简单,而且很可能比编写视图扩展要少很多编码和开发.
The basics of this is really simple and most probably a lot less coding and development then writing the views extensions.
/** Implementation of hook_menu().
*/
function gallery_menu() {
$items = array();
$items['gallery'] = array(
'title' => 'Gallery',
'page callback' => '_gallery_list',
'access arguments' => array('access content'),
);
$items['gallery/%gallery'] = array(
'title' => 'For dynamic titles, see title_callback documentation',
'page callback' => '_gallery_view',
'access arguments' => array('access content'),
);
return $items;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_list() {
$html = "";
$galleries = pager_query("SELECT * FROM {galleries}", 10);
foreach($galleries as $gallery) {
$html .= check_plain($gallery->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_view($gallery) {
$html = "";
$images = pager_query("SELECT * FROM {images} WHERE gallery_id = %d", 10, $gallery->id);
foreach($images as $image) {
$html .= check_plain($image->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
显然,如评论中所述,您将另外创建一些主题函数来处理渲染,以 1) 避免在整个模块中使用硬编码的 spagetty-HTML 和 b) 允许前端在创建时保持他们的主题HTML.
Obviously, as stated in the comments, you would additionally create a few theme functions to handle the rendering, to 1) avoid hardcoded spagetty-HTML all over your module and b) allow the frontenders to stay in their theme when creating the HTML.
这篇关于Drupal 7 中的程序化视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!