0

This question is related to my previous question about how to change the sort order in a view. Since this is a new question I thought I better post a new one.

My new problem is that I use hook_views_pre_render() as suggested, but I am having trouble getting the views sort order right. I use an anonymous function like this to change the views order:

/** 
* Implementation of hook_views_pre_render. Sorts the nodes under "Givare"
 */ 
function mysite_views_pre_render(&$view) { 
    if ($view->name === 'related_items' && $view->current_display === 'probes') {   
        $node = node_load(arg(1));
        $sortorder = array();
        $groups = $node->field_lagg_till_givargrupp['und'];
        foreach($groups as $group){
            $group_nid = $group['nid'];
            $group_node = node_load($group_nid);
            $probe_nids = $group_node->field_koppla_givare['und'];
            foreach($probe_nids as $probe_nid){
                $sortorder[] = $probe_nid['nid'];
            }
        }

        usort($view->result, function ($a, $b) use ($sortorder) {
            $cmpa = array_search($a->nid, $sortorder);
            $cmpb = array_search($b->nid, $sortorder);  
            dsm($a->nid." ".$b->nid." ".$cmpa." ".$cmpb);   
            if($cmpa === $cmpb) {
                return 0;
            } 
            return ($cmpa < $cmpb) ? -1 : 1;
        });
    }
}

The variable $sortorder contains an array with nids in the wanted order. But the resulting $views_result does not follow that order (but it is altered, just in the wrong way). Can anyone see what I am doing wrong?

EDITED: added context – the view in question is a block display with a grid setting (there are several of these block displays in the same view and these block displays are used by Quick Tabs to render each block under a tab as related items.) In this particular block display I have tested to omit exposed filters, to deselect grouping by title etcetera to see if that would impact the sorting, but with no luck. I was surprised that this block needed sorting at all because I give the order explicitely with a contextual filter. See this question for the contextual filter: Sort criteria is ignored in Views

Also, as I added the row dsm($a->nid." ".$b->nid." ".$cmpa." ".$cmpb); I can see that there are more than one hundred value pairs, but there are only 22 items to be rendered(!). The first few rows of that dsm() looks like this, where for example the nid 20726 is repeated several times:

20726 20693 7 12

21863 20726 5 7

20726 20695 7 4

20726 20697 7 0

20726 20699 7 15

20861 20726 21 7

20860 20726 20 7

20859 20726 19 7

20858 20726 23 7

20857 20726 22 7

20768 20726 2 7

  • I added your code to a hook_views_pre_render() hook and it works correct. I suggest you look for the error elsewhere. This code is likely in a theme's template.php file. Is it added to the right theme's file (admin or frontend)? Are you looking at the result of the sort by inspecting the value of the $view->result within the hook or are you looking at the rendered output? PS i was tempted to vote to close this as it is PHP code, but as the code works the error is in some other place making it a Drupal question again and I suggest you give more context – J. Reynolds Apr 21 '16 at 12:18
  • Yes, it's in the template.php file and there is only one theme, so no mistake there. Also I check both the the rendered output and within the hook itself. I can't see what is wrong, but thanks for confirming that the snippet works. – TBJ Apr 21 '16 at 12:26
  • usort uses Quickort so all as expected. Check your hook signature. It should be hook_views_pre_render(&$view) – J. Reynolds Apr 21 '16 at 17:03
  • Yes, the signature is correct. And the function does alter the sorting in the correct view display, but not as expected. Above I have added the whole function instead of just the sorting snippet. – TBJ Apr 22 '16 at 7:33
  • My only conclusion is that $sortorder is suspect and either contains duplicates or is not inclusive of the values of $view->result nids. – J. Reynolds Apr 22 '16 at 7:46
0

PROBLEM SOLVED. As J. Reynolds pointed out there were missing nid values in $view->result before the sorting was made.

There was also an issue with the nids because I had forgotten to set the field mapping (migrate d2d) to "do not create automatic URLs" for pathauto during a an earlier migration from D6 to D7 of the group nodes that contained the items in question. So the item nids were fetched from the wrong group entity. My mistake :-(.

But everything is sorted out now. Thanks for your input!

| improve this answer | |

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.