I can't seam to get right a view query alter.
I'm passing an argument to the view but I don't need an "=" condition. I need a LIKE condition.
From the GUI I can't seem to be able to do it so I was thinking about altering the query from code.

What is the best way to alter the conditions in a code like this ?

function mymodule_query_views_alter(QueryAlterableInterface $query) {
    $where =& $query->conditions();
}

Comments

dawehner’s picture

Status: Active » Fixed

Maybe hook_views_query_alter is much easier to use for you.

That's a core hook, so you could ask in the forums for help.

paoloteo’s picture

hello, I can actually hook in with hook_views_query_alter but what should set to have that condition work with a "LIKE" ?

paoloteo’s picture

I've tried with something like:

function mymodule_views_query_alter(&$view, &$query) {
  if($item['field'] == 'node.title'){
    $query->where[0]['conditions'][$key]['operator'] = 'LIKE';
  }
}

but does not seam to work.
Any help is appreciated.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Aritra Banerjee’s picture

Issue summary: View changes

You should work with $query->add_where() condition under _views_query_alter hook function. You can try something like this below(It worked successfully in Drupal 7):

function hook_views_query_alter(&$view, &$query) {
  if($view->name == 'custom_view') {
      $user_interest = array(1,2,3);
      $query->add_where(1,'field_data_field_user_interest.field_user_interest_tid', $user_interest, 'IN');
  }
}

NOTE: 1. Here "field_data_field_user_interest" field must be added in Relationship of Views configuration.
2. Any desired SQL condition can be added in add_where() of $query.