1

I am designing and ecommerce store and I am trying to sort the products by availability using a hook_view. My function product_eta_seconds return the current eta of a product. Here is my module:

function availability_sort_views_pre_render(&$view) {
  if ('uc_products' == $view->name && 'page_1' == $view->current_display) {

    $view->result = array_reverse($view->result);
    $deliveryTime=array();
    foreach ($view->result as $viewRow){
        $deliveryTime[] = product_eta_seconds($viewRow->nid);       
    }
    asort($deliveryTime);   
    $view->result = sortArrayByArray($view->result,$deliveryTime);
  }
}

function sortArrayByArray($array,$orderArray) {
    $ordered = array();
    foreach($orderArray as $key=>$value) {
        if(array_key_exists($key,$array)) {

                $ordered[$key] = $array[$key];
                unset($array[$key]);
        }
    }
    return $ordered + $array;
}

The problem is the view->result returns only 9 items (my pager value) so when I look at page 2, there are results that should show up on the 1st page (with a lower eta). Is there a solution to get all items of the view without the pager and apply the pager, when the view is rendered ? Any other solution would suit though.

2 Answers 2

2

I couldn't find any hook to sort the views result after the results so I ended up using the module "Views PHP" and sort my view using the following:

<?php    
    $deliveryTime1Arr = delivery_time_lookup($row1->nid);
    $deliveryTimeRow1=$deliveryTime1Arr[3];
    $deliveryTime2Arr = delivery_time_lookup($row2->nid);
    $deliveryTimeRow2=$deliveryTime2Arr[3];
    if($deliveryTimeRow1>$deliveryTimeRow2) return 1;
    else if($deliveryTimeRow1 == $deliveryTimeRow2) return 0;
    else if($deliveryTimeRow1 < $deliveryTimeRow2) return -1;
?>
2
  • hook_views_post_execute
    – FLY
    Apr 23, 2013 at 13:53
  • hook_views_post_execute does not help either ;(
    – user20741
    Sep 5, 2013 at 20:59
0

Drupal view pager is generated from the result of view query. Even if you alter your display with pre_render the total view result will not change. So the pager will not be altered with your custom result. Here you can use hook_views_query_alter to alter your query and display according to your need.

Your Answer

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

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