php 如何为 Drupal View 的模块创建自定义过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/241645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:02:50  来源:igfitidea点击:

How to create a custom filter for the Drupal View's Module?

phpdrupaldrupal-6drupal-views

提问by user6824

I have the Profile, CCK, and Views2 modules installed on a Drupal 6 site. I added a string field to the user profile. I can filter easily on preset values, thru the Views GUI builder, really nicely. However, I'd like the filter criteria to be dynamically set based on other environment variables (namely the $_SERVER['SERVER_NAME']).

我在 Drupal 6 站点上安装了 Profile、CCK 和 Views2 模块。我在用户配置文件中添加了一个字符串字段。我可以通过视图 GUI 构建器轻松过滤预设值,非常好。但是,我希望根据其他环境变量(即$_SERVER['SERVER_NAME'])动态设置过滤条件。

Is there a basic 'How-to-write-a-custom-drupal-views-filter' somewhere out there? I've been looking thru the documentation, but it's not obvious to my simple mind on how to do it.

是否有一个基本的“如何编写自定义drupal视图过滤器”?我一直在查看文档,但对于如何做到这一点,我的简单想法并不明显。

回答by AbhiG

You can create your own function like following to add your own filters.

您可以创建自己的函数,如下所示添加自己的过滤器。

<?php custom_views_embed_view($view_name, $display_id) {
$view = views_get_view($view_name);
$view->set_display($display_id);
$id = $view->add_item($display_id, 'filter', 'node', 'created',
                      array( 'value' => array('type' => 'date', 'value' => date('c')), 'operator' => '<='));
return $view->execute_display($display_id);
}
?>

回答by mjimcua

I have a similar problem and this article has been very helpful in resolving the problem.

我有一个类似的问题,这篇文章对解决问题非常有帮助。

http://www.metaltoad.com/blog/drupal-7-tutorial-creating-custom-filters-views

http://www.metaltoad.com/blog/drupal-7-tutorial-creating-custom-filters-views

And hook_views_data oficial documentation

和 hook_views_data 官方文档

http://api.drupal.org/api/views/docs%21docs.php/function/hook_views_data/6

http://api.drupal.org/api/views/docs%21docs.php/function/hook_views_data/6

回答by yrk

You can use viewsphpfiltermodule which allows filter views by node id.however there is a patch if you need to extend this for user views

您可以使用viewsphpfilter允许过滤视图的模块,node id.但是如果您需要扩展它,则有一个补丁user views

回答by alastairs

There is the possibility, having looked at the sort of filters installed for my own site, that filters have to be based on some database field, in which case what you're trying to achieve is not possible. It appears that the filters provide the WHERE clause to the generated SQL query.

在查看了为我自己的站点安装的过滤器类型后,有可能过滤器必须基于某个数据库字段,在这种情况下,您尝试实现的目标是不可能的。过滤器似乎为生成的 SQL 查询提供了 WHERE 子句。

Having said all that, if you want to pursue it further, your best bet is to start with a module that already provides filters for Views. There are filters provided with Views for the Node module; alternatively, you could look at the audio modulewhich also provides some filters. Additionally, posting to the Drupal forums or support list may turn up another module that will allow you to achieve what you're attempting.

说了这么多,如果你想进一步追求它,最好的办法是从一个已经为视图提供过滤器的模块开始。Node 模块的视图提供了过滤器;或者,您可以查看也提供一些过滤器的音频模块。此外,在 Drupal 论坛或支持列表上发帖可能会打开另一个模块,使您能够实现您正在尝试的目标。

回答by alastairs

yes you can do it. Try using the module "views filter block". Once you enable the block .. extract the html of the block from "view source" when viewing the page. Now disable the "views filter block" ... create your own custom block .. add the code to it with whatever css you like to make it look pretty . Within this code use php to dynamically specify what you want for the filter initial selection to be. Make sure you actually choose the field the filter is based on .. then within the custom php block use php code to write IF condition to check for the server_name value and accordingly assign the filter variable the right value."

是的,你可以做到。尝试使用模块“视图过滤器块”。启用块后.. 在查看页面时从“查看源代码”中提取块的 html。现在禁用“视图过滤器块”……创建您自己的自定义块……使用您喜欢的任何 css 添加代码,使其看起来很漂亮。在此代码中,使用 php 动态指定过滤器初始选择所需的内容。确保您实际选择了过滤器所基于的字段......然后在自定义 php 块中使用 php 代码编写 IF 条件以检查 server_name 值,并相应地为过滤器变量分配正确的值。”

There maybe other (possibly even better) ways to do it to actually write a module to use the filter . So this is but one suggestion. Also give "Views PHP Filter" a try. I have not used it yet but sounds like its worth a shot.

可能还有其他(可能更好)的方法来实际编写一个模块来使用 filter 。所以这只是一个建议。还可以尝试“查看 PHP 过滤器”。我还没有使用它,但听起来值得一试。

  • by drupal user (drupal username: drupdrips)
  • 通过 drupal 用户(drupal 用户名:drupdrips)