Views query alter add orderby function FIELD() I can't figure out how to add following expression to the views query:

ORDER BY FIELD(field_dataid,3,2,1,4)


QueryPluginBase only gives the option to add:

$query-&gt;addOrderBy('node_field_data', 'nid', ASC);


But doesn't give the option to add an expression.

The Sql interface has the options to add an expression:

  $query-&gt;addExpression('FIELD(node_field_data.nid,3,2,1,4)', 'order_field');
  $query-&gt;orderBy('order_field', 'ASC');


But this doesn't work with the views query object.

So how can i add orderby expression to the views query object?
There is a similar answer here: https://drupal.stackexchange.com/a/200662/79998

You should either use $query-&gt;addField() to add the expression and then append or unshift in $query-&gt;orderby array,
or you can use directly $query-&gt;addOrderBy() to add the expression and order, if it's ok to be added last.

$query-&gt;addOrderBy(NULL, 'FIELD(node_field_data.nid,3,2,1,4)', 'ASC', 'order_field');