Score:0

Trying to sort a views result by a global views conditional field

id flag

I'm attempting to use hook_views_pre_render in a Drupal 8.9.20 site to sort the results of a view by a global views conditional field and I could really use some help. I've spent some hours searching forums with no luck.

Here's my code so far.

 /*
 * Implements hook_views_pre_render
 */
function mk_timing_views_pre_render($view) {
  if (($view->id() == 'mk_driver_results')) {
    $view->result['sort_by'] = 'views_conditional_field_9';
    $view->result['sort_order'] = 'ASC';
  }
}
ru flag
I fear you are out of luck here: Sorting is pre-defined in the query and done on the SQL server, the query result is already sorted. If I understand that module correctly, Views conditional field is not a real field or part of the query, it is just post-processing that result. So views module also can't use a conditional field as sort criteria.
davidp avatar
id flag
Thanks @Hudri, I was hoping to sort the view result with PHP after the SQL query had run but before it renders. The global conditional field is processed with PHP after the query is run so I'm hoping I can also use PHP to sort the result.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.