0

In a the hook hook_view_query_alter, i need to arrange the query to put some result at the very first, with some complexes conditions.

To that, in mysql, we can use the "if" function in the select part of the query and then add a "order by" on this field, for exemple like this :

select id, if(col_A > x and (colB < y);1;0) as first
from my_table
order_by first

I need to apply that to the query of my view.

On a regular dynamic Drupal query, we can use the addExpression function where we can put anything.

I can't found a equivalent for the views\query\Sql class. I tried :

$query->addField(
      'pm',
      "pm.field_tags_make_target_id is not null or(activity_field_data.user_id = ".$user->id()." and activity_field_data.created > ".$timeStamp.") ,1,0",
      "is_first",['function'=>"if"]);

but it seem to be ignored when it build the query.

So how to had customised expression in the "select" part of view query?

  • I don't know if the views sql query api handles all the things that the sql query api handles, however you can simply add a tag to the sql query in the views ui under advanced->other->query_settings then alter the query as normal with hook_query_TAG_alter(). – Charles Bamford Oct 31 at 16:38

Your Answer

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

Browse other questions tagged or ask your own question.