Drupal - 视图。以编程方式设置过滤器
我希望这不是我一天中大部分时间都在寻找的愚蠢问题!
我有一个内容类型(文档),它只包含标题、文件和类别。类别值是必需的,并且由分类法“提供支持”。
我现在希望创建一个视图,该视图将显示按分类术语分组和标题的这些文档。
利用我有限的 Drupal 知识,我打算迭代相关术语 ID(使用 taxonomy_get_tree($vid)
),然后相应地渲染每个视图。
为此,我一直希望使用这个片段。
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
但这是行不通的;当我查询 $filter
($view->get_item($display_id, 'filter', 'field_dl_category'))
的值时,我得到 null 返回。
这可能是因为我的过滤器名称与 CCK 字段名称不同?
我正在使用 Drupal 7。
非常感谢任何帮助,我已经没有想法(和时间)了。
I hope this is not a stupid question I have been searching for most of the day!
I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.
I now wish to create a view which will display these documents grouped and titled by the taxonomy term.
Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)
) and then render each view accordingly.
To do this I have been hoping to use this snippet.
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
But this is not working; when I query the value of the $filter
($view->get_item($display_id, 'filter', 'field_dl_category'))
I get null returned.
Might this be that my filter name is not the same as the CCK field name?
I am using Drupal 7.
Any help much appreciated, I am running out of ideas (and time).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于成功地完成了这项工作,但我采取了略有不同的方法。
我更改了视图并添加了相关的上下文过滤器,然后使用此函数views_embed_view 来获取我所需的结果。
如果这有帮助的话!这是我的解决方案:
在我的情况下,trim($content) 返回“”,没有数据,因为视图模板已被编辑,但可能并非所有人都是这种情况。
我是一名非常新的 Drupal 开发人员,所以我确信有更好的方法可以做到这一点,如果是的话,请发帖。
I finally managed to get this working but I took a slightly different approach.
I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.
If this helps! this is my solution:
In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.
I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.
我将继续假设您想要使用视图显示一个按标记类别分组的文档节点列表。
您可以通过两种(也许更多)方法在视图 3 中执行此操作:
(a) 选择允许您选择分组字段的显示样式。 (您可以尝试默认情况下随视图附带的表格样式)。假设您已通过视图关系将
node
表与taxonomy_term_data
表正确关联,则可以选择taxonomy_term_data.name
作为分组字段。请注意,此分组是在视图刚刚渲染之前完成的。因此,您的查询只需选择(内容、标签)对的平面列表。
(b) 您还可以利用附件显示类型来实现类似的功能。首先在列表视图中显示已使用类别,单击该类别将显示一个页面(附件),其中包含该所选类别中标记的所有文档。
要了解如何执行 (a) 或 (b),请首先打开
advanced_help
模块(这不是 Views 必需的,但建议使用)。对于 (a),请阅读有关样式分组的部分,即
views/help/style-grouping.html
和对于 (b),请阅读有关附件显示的部分,即
views/help/display-attachment.html
关于您的方法的一些事项:
(a) 它将显示所有内容该词汇表中的术语,无论它们是否用于标记至少一个文档。
(b) 即使当前查看的用户无权访问该视图,
views_embed_view()
也将返回 NULL。所以,一定要抓住这个案子。I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.
There are two (of maybe more) ways by which you can do this in Views 3:
(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the
node
table to thetaxonomy_term_data
table through a Views relationship, you could choosetaxonomy_term_data.name
as the grouping field.Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.
(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.
To understand how to do (a) or (b), turn on the
advanced_help
module (which is not a Views requisite but is recommended) first.For (a), read the section on Grouping in styles i.e.
views/help/style-grouping.html
andFor (b), read the section on Attachment display i.e.
views/help/display-attachment.html
A couple of things about your approach:
(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.
(b)
views_embed_view()
will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.这是一个替代方案:
我知道您可能可以使用一些复杂的方法来设置它,显然这会更好。但如果你只是想要一个快速而肮脏的直接访问而不搞乱,这会让你到达那里。
Here's an alternative:
I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.