14

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

2
  • 1
    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.
    – wranvaud
    Nov 11, 2016 at 17:46
  • 1
    Quite right, best to use @inheritdoc
    – ajmedway
    Feb 23, 2018 at 11:04

5 Answers 5

9

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)

1
  • tanx a lot, that's exactly what I was looking for
    – nooshinha
    May 25, 2014 at 13:17
14

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');
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

Try this,

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

You can use just like

$query->add_where(1,'node.nid',$value,'=');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.