I added two Sort By fields to my View: "Sort By Title" (by string) and "Sort By Rooms" (number) - they are custom PHP Sort with functions defined in module.

When I add them separately (just one of them) they both works but when I add them together only the last one works (the one on the bottom in section Sort criteria)... and if sorted by the first one I am getting "random" sorting results - I mean I do not see any pattern....

What is going on ... is this a bug ?


The query generated by the View:

Just list a of nodes with Type = 'diy_hotel'

    node.title AS node_title,
    node.nid AS nid,
    'node' AS field_data_field_diyh_hotel_status_node_entity_type
    {node} node
    (( (node.status = '1') AND (node.type IN  ('diy_hotel')) ))

Sorting Functions:

The function get_rooms_number_from_hotel_nid() returns an integer number.

function roomsgetter_admin_hotel_index_sort_by_hotel_name($row1, $row2) {
    if ($row1->title < $row2->title) {
        return -1;
    if ($row1->title > $row2->title) {
        return 1;
    return 0;

function roomsgetter_admin_hotel_index_sort_by_rooms($row1, $row2) {
    $a = get_rooms_number_from_hotel_nid($row1->nid);
    $b = get_rooms_number_from_hotel_nid($row2->nid);
    if ($a < $b) {
        return -1;
    if ($a > $b) {
       return 1;
    return 0;


I found code that executes the sorting functions. The problem there is that it executes ALL sorting functions defined in the view - but should execute only the one selected by the user via dropdown (f.ex. "Sort By Rooms") that's why I got all strange sorting results - the final sort was a combination of all defined sorting functions...

I was able to "fix" this by altering the "views_php_handler_sort.inc" file of drupal core. The function responsible for sorting now looks like:

function php_post_execute() {
    $sort_by = isset($_GET['sort_by']) ? $_GET['sort_by'] : $_POST['sort_by'];
    if (!empty($this->options['php_sort']) && $this->view->style_plugin->build_sort() && $this->options['id']==$sort_by) {
       $this->php_sort_function = create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_sort'] . ';');
       usort($this->view->result, array($this, 'php_sort'));

I added the last IF conditional statement here.

Although this fixed the sorting - I have a question "is this a bug?", should I file an issue to drupal.org ?

  • Under admin/structure/views/settings you can enable an option so you can see the generated database query. Add this to your question, also it would be nice to see the code for sort functions you're using. – Cyclonecode Jun 2 '14 at 17:39
  • Looking at the query above, the isn't any ORDER BY clause at all, is this really the entire sql statement? Can you also show how the custom sort functions is connected to the view? Which view hooks are you using? – Cyclonecode Jun 4 '14 at 15:35
  • I would like to see how the query looks when you configure the view to sort both by title and rooms. – Cyclonecode Jun 4 '14 at 15:44
  • The query looks just the same as above - because the sorting is done after the data has been pulled from DB - sorting is done in PHP not via SQL "ORDER BY" clause. – Michal Przybylowicz Jun 5 '14 at 4:55
  • State this in your question since it sounds like you have extended the views module with a new option called sort by rooms. Also, show how you load the view and how you then tries to sort the result. – Cyclonecode Jun 5 '14 at 11:10

Your Answer

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

Browse other questions tagged or ask your own question.