How to sort view results by user role weight?

Is there a way to sort asc / desc of view results based on user role weight. We tried php code as a sorting criterion. We returned the weight of the user role as output, but the order does not change as expected.

+4
source share
1 answer

The next php function ..... in fact, I just made it a function, because it is more accurate, and I needed to use it in several views, but you could use this code directly in your view. Ideally, this should probably be in the module.

function get_role_weight($userId){

  $users = user_load($userId);
  $userRole = $users->roles;
  $arr = Array();

  foreach($userRole as $row){
    $roleID = user_role_load_by_name($row)->rid;
    $role = user_role_load($roleID);
    $arr[] = $role->weight;
  }

  sort($arr);

  return $arr[0];
}

How it's done

  • "Global: PHP"
  • - ,
  • " :": return get_role_weight($data->uid);

  • " :" <?php print $value; ?>

  • , , , "Global: PHP" " ", " :": if ($row1->php < $row2->php) { return -1;} else { return 1;}

+3

Source: https://habr.com/ru/post/1532827/


All Articles