当使用Drupal 7和Views 3时,更改Views过滤器值以使其影响将要呈现的内容的正确方法是什么?
我尝试了这里提到的所有D6。
尽管有一些人可以使用它,但Merlin指出,修改display_options是实现此目的的不正确方法,但是他对要做什么的回答含糊不清(http://drupal.org/node/789710#comment- 2927556)。
我试过了:
function pages_views_pre_view($view){
$view = views_get_view('north_carolina');
$view->set_display('default');
$view->display_handler->options['filters']['province']['value'] = 'Georgia';
dsm($view->display_handler->options['filters']['province']);
}
笔记:
1)我创建了一个视图north_carolina
,该视图具有两种类型的显示-页面和块(page_1和block_1)。
2)我的显示器上的过滤器是相同的,因此我尝试更改“默认”显示,以使其影响所有显示器。
3)我的过滤器的默认值为“北卡罗莱纳州”(在“视图UI”中设置),但请注意,我正在尝试将其更改为“乔治亚州”
4)如果之后立即运行DSM,我会看到过滤器已更改,但是,我的结果仍然显示“北卡罗莱纳州”结果。缓存已关闭,在全部运行drush cc之后,我仍然得到相同的结果。
我还尝试通过hook_views_pre_build
和运行相同的代码hook_views_pre_execute
。
有什么建议么?
编辑:
如建议的那样,可能会有更多信息。这是视图的转储:
$view = new view();
$view->name = 'north_carolina';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'North Carolina';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'test nc block';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Location: Province */
$handler->display->display_options['filters']['province']['id'] = 'province';
$handler->display->display_options['filters']['province']['table'] = 'location';
$handler->display->display_options['filters']['province']['field'] = 'province';
$handler->display->display_options['filters']['province']['value'] = 'North Carolina';
$handler->display->display_options['filters']['province']['exposed'] = TRUE;
$handler->display->display_options['filters']['province']['expose']['operator_id'] = 'province_op';
$handler->display->display_options['filters']['province']['expose']['label'] = 'State';
$handler->display->display_options['filters']['province']['expose']['operator'] = 'province_op';
$handler->display->display_options['filters']['province']['expose']['identifier'] = 'province';
$handler->display->display_options['filters']['province']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
4 => 0,
);
/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->display->display_options['block_description'] = 'test nc block';
请注意,这些处理程序由locations模块提供。专门针对“位置:省”过滤器,我会得到一个自动填写的表格。当我输入北卡罗莱纳州时,它会弹出,我必须选择它。但是,请注意,在SQL语句中,视图生成使用状态的两个字符缩写:
SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
FROM
{node} node
LEFT JOIN {location_instance} location_instance ON node.vid = location_instance.vid
LEFT JOIN {location} location ON location_instance.lid = location.lid
WHERE (( (node.status = '1') AND (location.province = 'NC') ))
ORDER BY node_created DESC
LIMIT 10 OFFSET 0