如何在视图渲染之前更改过滤器的值并更改输出?


15

当使用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

Answers:


8

很难找出要修改的视图对象的确切部分。我通常先导出视图,然后查看生成的代码以开始使用。

下面的代码段未修改,并且可以在我的一个生产站点上正常工作。希望这足以使您朝正确的方向前进(显然,这在名为的自定义模块中offer_select)。如果可以从导出的视图中提供代码,则可能有人可以看一下。

//Alter the End date filter on the offer views
function offer_select_views_pre_view(&$view) {
  if ($view->name == 'active_offers') {
    $view->display['default']->handler->options['filters']['field_end_value']['value']['value'] = time();
  }
}

--UPDATE--
对于您的特定视图,我安装了Location模块,并在NC中创建了一些节点,在Georgia中创建了一些节点,然后将视图导入进行测试。在我的自定义模块(名为dev)中,我从以下代码开始(已安装devel):

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    dpm($view->display['default']->handler->options['filters']);
  }
}

从那里,我继续将以krumo显示的显而易见的值添加到数组上,直到到达这里:

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    dpm($view->display['default']->handler->options['filters']['province']['value']);
  }
}

只需North Carolina在Krumo中打印即可。那时,我从打印值更改为如下设置:

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    $view->display['default']->handler->options['filters']['province']['value'] = 'Georgia';
  }
}

和瞧。做到了。


凉。如果有机会,我将与您一起探讨是否可以提出更具体的答案。
亚当·巴尔萨姆

@ blue928参见上面。我已针对您的问题和有关如何到达那里的一般说明更新了答案。
亚当·巴尔萨姆

效果很好。请注意正确答案:我的部分问题是我不需要调用views_get_view或设置显示。
blue928 2013年

2

如果有人在D8上搜索相同的解决方案时弹出此线程(就像我所做的那样):

新的API允许以更清晰的方式操作这些参数。请参阅本节以获取参考。

小例子

function dev_views_pre_view(ViewExecutable &$view, $display_id, array &$args) {
  if ($display_id === 'my_unique_display_name') {
      $args[0] = 'desired_value';
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.