欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

how to use hook_views_query_alter() to modify where condition?


I'm trying to modify the where condition of a views query. till now I was successful with altering "order by", but I have no idea how to alter the where condition. I want to check the search_term and if it was in uppercase, transform it to lowercase so the query can find it. also there are some special characters in my language (persian) that I need to replace them before the query runs. anyone can help me where to start or what hooks or views_handlers to use?

<?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);
  }
}
?>

the devel result for where condition is as follow right now. :views_combine is equal to %s%.

CONCAT_WS(' ', node.title, ' ', field_data_body.body_value, ' ', field_data_field_author.field_author_target_id, ' ', field_data_field_translator.field_translator_target_id, ' ', field_data_field_book_tags.field_book_tags_tid) LIKE :views_combine

shareimprove this question
 
   
It is not necessary (discouraged to) write the parameters of a hook in the comment "@param...". These parameters are always the same and well documented for every hook. – Drilix Nov 11 at 17:46

You can access the content of a where condition modifying its value:

$query->where[0]['conditions'][0]['value'] = 'something...';

Pretty similar what you did with orderby. Also you can add custom where conditions (https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where/7 and https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where_expression/7)

shareimprove this answer
 
   
tanx a lot, that's exactly what I was looking for – weber85 May 25 '14 at 13:17

To add a new where clause you can use, add_where

For example :

$query->add_where(1,'taxonomy_term_data_node.tid', $value, 'NOT IN');
shareimprove this answer
 

Try this,

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

You can use just like

$query->add_where(1,'node.nid',$value,'=');  
shareimprove this answer
 
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";
  }
}

I'm trying to modify the where condition of a views query. till now I was successful with altering "order by", but I have no idea how to alter the where condition. I want to check the search_term and if it was in uppercase, transform it to lowercase so the query can find it. also there are some special characters in my language (persian) that I need to replace them before the query runs. anyone can help me where to start or what hooks or views_handlers to use?

<?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);
  }
}
?>

the devel result for where condition is as follow right now. :views_combine is equal to %s%.

CONCAT_WS(' ', node.title, ' ', field_data_body.body_value, ' ', field_data_field_author.field_author_target_id, ' ', field_data_field_translator.field_translator_target_id, ' ', field_data_field_book_tags.field_book_tags_tid) LIKE :views_combine

shareimprove this question
 
   
It is not necessary (discouraged to) write the parameters of a hook in the comment "@param...". These parameters are always the same and well documented for every hook. – Drilix Nov 11 at 17:46

You can access the content of a where condition modifying its value:

$query->where[0]['conditions'][0]['value'] = 'something...';

Pretty similar what you did with orderby. Also you can add custom where conditions (https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where/7 and https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where_expression/7)

shareimprove this answer
 
   
tanx a lot, that's exactly what I was looking for – weber85 May 25 '14 at 13:17

To add a new where clause you can use, add_where

For example :

$query->add_where(1,'taxonomy_term_data_node.tid', $value, 'NOT IN');
shareimprove this answer
 

Try this,

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

You can use just like

$query->add_where(1,'node.nid',$value,'=');  
shareimprove this answer
 
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";
  }
}



来自  http://drupal.stackexchange.com/questions/76651/how-to-use-hook-views-query-alter-to-modify-where-condition

普通分类: