I'm trying to build a Glossary View in Drupal 8. Let's say I have 3 content types: Person, Article, Event. I want sort by title ASC for Article and Event, but sort by Last Name for Person. So that listing would show something like.
Deep Study on Dolphins (article) John Doe (person) Jam in the Backyard (event)
And Not
Deep Study Jam in the Backyard John Doe
I think I can do this with hook_views_query_alter, but the only examples I've seen are for Drupal 7.
I've got something like this
function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'glossary_everything') {
$query->orderby[0]['field'] = "CASE WHEN node_field_data.last_name IS NULL THEN node_field_data.title ELSE node_field_data.last_name END";
}
}
But I'm getting a "column not found" sql error. I think it's treating the whole CASE statement as one big field. Is that because of the ['field'] designation in my array?