以编程方式向现有视图添加内容类型过滤器

时间:2011-01-18 17:48:23

标签: drupal drupal-6 drupal-views drupal-modules

我对Views很新,并希望以编程方式修改现有视图。这个artcile帮助programmatically-set-a-views-filter-in-drupal-6-and-views-2,但我不知道如何根据我的需要定制它。

我需要修改视图并添加内容类型过滤器。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

我刚刚这样做了,以编程方式为现有视图添加语言过滤器。这是我接触它的方式。

在views_ui界面(admin / build / views)中,选择您的视图并通过Web界面添加过滤器。正确设置后,导出视图。你将在PHP代码中获得一个大数组。在数组中找到您的过滤器并将其复制粘贴到临时文本文件中以供将来参考。

现在,在代码中,执行以下操作:

// Load the view by specifying its name (as displayed at admin/build/views
$view = views_get_view('name_of_view');
// Specify the display you are editing - you can see this at admin/build/views/edit/name_of_view.
// Usually it's something like 'page' or 'block' or 'default'
$display = 'default';
$view->view_set_display($display);
// Now load up an available filter. You must do this step - it's the equivalent of
// clicking 'add' in the interface.
// filter_name is the same as what is specified in your export from before. 
$filter_name = 'language';
$filter = $view->get_item($display, 'filter', $filter_name);
// Now set the arguments. This will need to match what is in your export from before...
$filter['value']['***CURRENT_LANGUAGE***'] = '***CURRENT_LANGUAGE***';
// ...and save the filter.
$view->set_item('default', 'filter', 'language', $filter);

这对我很有用。作为参考,这是我的观点导出的相关摘录:

'language' => array(
    'operator' => 'in',
'value' => array(
  '***CURRENT_LANGUAGE***' => '***CURRENT_LANGUAGE***',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
  'operator' => FALSE,
  'label' => '',
),
'id' => 'language',
'table' => 'node',
'field' => 'language',
'override' => array(
  'button' => 'Override',
),
 'relationship' => 'none',
),

答案 1 :(得分:1)

我怀疑您最好的选择是使用hook_views_query_alter添加WHERE来限制特定node.type。您可以检查$view以确定是否正在调用您的视图,然后使用$query对象添加约束。不幸的是,视图钩子很少被记录,你经常需要查看实际的模块源来看看实际发生了什么。

相关问题