1

I have an exposed filter that shows products which have multiple entity references. The user should be able to pick some number of entities and the view should show entities containing all those references. In my mind that should mean changing the in operator on the where clause to an all operator.

function hook_views_query_alter(&$view, &$query) {
  if ($view->vid == 7) {
    dpm ($query);
    if (isset($query->where[1]['conditions'][2]['operator'])) {
      $query->where[1]['conditions'][2]['operator'] = 'all';
    }
    dpm ($query);
  }
}

However this seems to have no effect. the operator is changed from the first dpm to the second and it does break if I enter some other value in the all does not change the results. Do I have the wrong operator? Or am I going about this all wrong?

[edit] According to Payou the function should look like

function falk_products_query_products_alter($query) {
  $conditions = &$query->conditions();
  $conditions2 = &$conditions[0]['field']->conditions();
  $conditions3 = &$conditions2[0]['field']->conditions();
  $my_query = &$conditions2[0]['field'];
  $my_conditions = &$my_query->conditions();
  dpm ($my_query->conditions());

  if (isset($my_conditions[2])) {
    foreach ($my_conditions[2]['value'] as $target) {
      $my_query->condition($my_conditions[2]['field'], array($target), 'in');
    }
  }
  dpm ($my_query->conditions());
}

Which at the dpm's give me arrays that look good to me but dont work if more than one value is input. enter image description here

3
  • If I'm right, you want to alter $my_conditions[2] to an "IN" condition? But in your code, what is $my_conditions[2] initial statement? Because, in your function you add an "IN" condition to all $my_conditions[2]['value'] and I don't think that is you want... Can you dump sql query and each condition and specially the one who have values to transform to an "IN" condition. – Payou Feb 6 '14 at 13:38
  • I would like to have something more like an 'all' or 'contains' condition, meaning if the database entry has [1,2,3,4] I would like [1,2] to be a match and [1,2,5] not to be. so if all the numbers are in. I cannot find a query to match this other than 'if 1 is in and 2 in'. – Joel Johnson Feb 6 '14 at 14:21
  • Two possibles ways, continue to tweak that query and achieve your 'all' or 'contains' operator in Mysql style OR hook just after query execution and do it in php... You need to implement "hook_views_post_execute" for the second way. First way are better than the second but Second are easier to implement i think.. – Payou Feb 6 '14 at 15:36
1

Altering a query like this is really not recommended.

First of all, you should use SelectQuery methods provided by "$query" variable. You can get an array of query conditions by using "SelectQuery::conditions()" method.

Be careful, if you want to alter this array get the array like the following snippet :

$conditions =& $query->conditions();
//Now you can use and alter your query conditions.

Some others tips related to your code :

1) You can tag your query in Views UI

Add a tag to your Views Query : enter image description here

With this trick you can define a special hook for altering all query who use this tag. This hook is "hook_query_TAG_alter"

Now you can use this code :

function YOUR_MODULE_query_my_tag_alter($query) {
  $conditions =& $query->conditions();
  //This condition is useless now
  //if ($view->vid == 7) {
  //Check conditions and do your updates

  //}
}

2) Careful to the module weight

When you alter something, other modules can do it too... This order is determined by the module weight saved in the "system" table.

By using your own tags on your query, other modules should not know about this tag and should not alter your query.

You can change your module weight but this is not the thread of this post

Links

SelectQuery Api

SelectQuery::conditions() method

Query Alteration recommandations by Drupal

hook_query_TAG_alter

Change Module Weight

2
  • Thank you for the info. I added the results of what you said to do above. It still isn't working for me – Joel Johnson Feb 4 '14 at 20:47
  • The query variable uses the DatabaseCondition api not the SelectQuery api. FYI :) – Joel Johnson Feb 4 '14 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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