如何使用hook_views_query_alter()修改条件?


12

我正在尝试修改视图查询的where条件。到目前为止,我成功地更改了“ order by”,但是我不知道如何更改where条件。我想检查search_term,如果它是大写的,请将其转换为小写,以便查询可以找到它。在查询运行之前,我还需要用我的语言(波斯语)替换一些特殊字符。任何人都可以帮助我从哪里开始,或者使用什么钩子或views_handlers?

<?php
/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query
 */
function nashreneydev_views_query_alter(&$view, &$query) {
  //krumo($query);
  //krumo($view);
  if ($view->name == 'custom_search') {
    $search_term = $view->exposed_raw_input['combine'];

    **//$query->where[0]['conditions'][0]['field']= "?????";**
    $view->query->orderby[1]['field'] = "CASE node_type WHEN 'product_display' THEN 1 ELSE 2 END";
    $view->query->orderby[1]['direction'] = "ASC";
    $view->query->orderby[0]['field'] = "CASE node_title WHEN '".$search_term."' THEN 1 ELSE 2 END";
    $view->query->orderby[0]['direction'] = "ASC";
    //krumo($view->query->orderby);
  }
}
?>

现在条件如下的开发结果。:views_combine等于%s%。

CONCAT_WS('',node.title,'',field_data_body.body_value,'',field_data_field_author.field_author_target_id,'',field_data_field_data_field_translator.field_translator_target_id,'',field_data_field_data_field_book_tags.field_book_tags_tid)LIKE:views_com


不必(不鼓励)在注释“ @param ...”中编写钩子的参数。这些参数始终相同,并且每个钩子都有详细的文档说明。
wranvaud,2016年

完全正确,最好使用@inheritdoc
ajmedway

Answers:


9

太多了,那正是我在寻找的东西
nooshinha 2014年


2
function mymodule_views_query_alter(&$view, &$query) {
  // Term id's to unset from result set.
  $tids = array(346,355,359);
  if ($view->name == 'yourviewname') {

    $query->add_where(1,'taxonomy_term_data_node.tid', $tids, 'NOT IN');
    $query->orderby[0]['field'] = "CASE WHEN taxonomy_term_data_taxonomy_term_hierarchy.weight IS NULL THEN taxonomy_term_data_node.weight ELSE taxonomy_term_data_taxonomy_term_hierarchy.weight END";
    $query->orderby[0]['direction'] = "ASC";
    $query->orderby[1]['field'] = "taxonomy_term_data_node.weight";
    $query->orderby[1]['direction'] = "ASC";
  }
}

0

尝试这个,

 $location_value = strtolower($string);
 $query->where[1]['conditions'][0] = array('field'=>"node.title","value"=>'%'.$location_value.'%',"operator"=>"LIKE");

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.