在Drupal纲领性意见7 [英] Programmatic Views in Drupal 7
问题描述
我试图创建两个观点。
I'm trying to create two views.
查看-1是节点的列表
查看-2是与每个节点相关联的图像库。
View-2 is an image gallery associated with each node.
我基本上想从View-1的结题传递给一个纲领性查看-2,使每一行查看-1将装载查看-2(通过查看-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!).
我感到困惑的办法。如果发生这种情况的自定义模块中,preprocess功能,或其组合?
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
来定义菜单项和两个简单的菜单回调函数的处理所需的参数。
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.
拇指我(pseronal)规则是:
My (pseronal) rule of thumb is:
- 如果您确定将重新使用code多次在未来的项目视图,插件是值得投资的。
- 如果您将使用视图界面,用于更多的则一次性设置(一般使用),例如定义或更改视图在编辑/站长的工作流程这是有道理的。
这样做的基础是非常简单的,最可能的编码和发展少了很多,然后写意见的扩展。
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)避免硬codeD spagetty-HTML在你的模块和b)允许frontenders留在自己的创建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屋!