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';
  • 1
    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.
    – Hudri
    Commented Mar 24, 2023 at 7:59
  • 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.
    – davidp
    Commented Mar 24, 2023 at 22:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.