Tell me more ×
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

1 Answer

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 at 13:53

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.