Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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;
?>
share|improve this answer
    
hook_views_post_execute –  FLY Apr 23 '13 at 13:53
    
hook_views_post_execute does not help either ;( –  user20741 Sep 5 '13 at 20:59
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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