2

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:

  1. I create a relationship to the queue
  2. I sort by position in the queue
  3. 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?

1 Answer 1

0

I suggest you to make use of the following hook:

function YOURCUSTOMMODULENAME_views_post_execute(&$view){

    switch ($view->name){

 case "YOURVIEWMACHINENAME";

    // Get the result of the view in a array      
    $data = $view->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->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->set_display("NAMEOFYOURDISPLAY");
  $reflang->set_arguments(ARGUMENTSFORTHEVIEW);
  $reflang->pre_execute();
  $reflang->execute();

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

Hopefully this helps you to solve the problem.

1
  • Thanks much for your help; I'll check this out and reply ASAP, other tasks have since been made more pressing, so it may be a bit. Commented Sep 8, 2017 at 20:22

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.