Complex sorting Views Our Drupal 7 site has a feed of events which are parsed into nodes. Not every event is equally relevant, and they would like a way to selecrt the top five events, sorted chronologically, to appear on a block on the frontpage. They would also like the events to no longer display in the block once the event has passed. They are comfortable using Nodequeues and have requested one be created for this functionality. So far, this is pretty basic stuff and I have created a View which accomplishes this. The tricky part happens when there are less than five upcoming events in the queue. In this case, they would like unselected events to fill out any vacancies in the block of five.

Typically when I create a Nodequeue:


I create a relationship to the queue
I sort by position in the queue
I filter that the event is in the queue.


By removing the sort and filter, and instead sort first by if event is in queue and then by event date, I get the content I desire; when more than five events are selected, it works fine. When no events are selected, it works fine, however when there is a mix; naturally the in queue events are listed first. I would like all five events to be sorted chronologically.

An aside, I'm not certain Nodequeue is even the best way to do this; as the position in the queue implies a hierarchy. A flag could mark selected nodes, or a boolean field on the content type, or a taxonomy term might each be better solutions. I'm open to suggestions.

But first, how can I best properly sort this list?
I suggest you to make use of the following hook:

function YOURCUSTOMMODULENAME_views_post_execute(&amp;$view){

    switch ($view-&gt;name){

 case "YOURVIEWMACHINENAME";

    // Get the result of the view in a array      
    $data = $view-&gt;result;

    // your custom code to add / remove instances after the view was executed    


    // Set the result of the custom code back into the view
    $view-&gt;result = $data;
    break;

  }
}


Your custom code would be something like 
If there are less then 5 elements and more then 1 elements in the data array add new elements to the data array coming from another view.

To execute a view in code u can use something like this:

  $reflang = views_get_view("NAMOFYOURMACHINENAMEVIEW");
  $reflang-&gt;set_display("NAMEOFYOURDISPLAY");
  $reflang-&gt;set_arguments(ARGUMENTSFORTHEVIEW);
  $reflang-&gt;pre_execute();
  $reflang-&gt;execute();

  foreach ($reflang-&gt;result as $row =&gt; $rowinfo) {
    // add this to the data array
  }


Hopefully this helps you to solve the problem.