6

Is there a way to make a conditional sort criteria with views in D7?

I want to do the following stuff:

if node property = x
  use this sort criteria
else
  use this one
3

You could achieve this using Views PHP.

Excerpt from the page:

This module allows the admin account (user 1) to add fields, filters and sorts to views which use PHP code. It also supports additional accounts when the core PHP Filter module is enabled.

  • 3
    You could use this module but I think putting PHP into the database is poor form. – Stewart Robinson Sep 3 '12 at 17:53
2
/**
 * Implements hook_views_query_alter().
 */
function HOOK_views_query_alter(&$view, &$query) {
  if ($view->name == 'view_name' && $view->current_display == 'page') {
    $query->orderby = array(
      array(
        'field' => 'CASE WHEN node.property = x THEN node.y ELSE node.z END',
        'direction' => 'ASC',
      )
    );
  }
}

https://www.drupal.org/node/2133879

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.