Score:1

Modify sorting criteria using views_query_alter

fi flag
KTM

I have a view for the content type events with fields event_name, description, summary, event_start_date, etc. What my requirement is:

  1. Sort the events in ascending order of event_date equal or greater than the current date (upcoming events)
  2. Sort the events in descending order of event_date less than the current date (past events)

NOTE: the upcoming data should come first and then the past data.

eg data and expected output

example data

enter image description here

expected result

enter image description here

Through the normal view UI settings, I couldn't achieve this. so I tried the views_query_alter hook to modify the sorting criteria. but I couldn't achieve this. Any help?

What I have tried

/**
 * Implements hook_views_query_alter
 */
function news_t1_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if($view->id() == 'events'){
    $query->orderby[0]['field'] = "CASE node__field_st_field_st_value WHEN DATE(node__field_st_field_st_value) >= CURDATE()  END";
    $query->orderby[0]['direction'] = "ASC";
    $query->orderby[1]['field'] = "CASE node__field_st_field_st_value WHEN DATE(node__field_st_field_st_value) < CURDATE()  END";
    $query->orderby[1]['direction'] = "DESC";
    //$view->set_items_per_page(5);  
  }
}

node__field_st is the event_date field name.

Update after @Patrick Kenny's Answer

Your solution works great. thanks!. I added a Global: View area (Global: View area) in the footer of the main display. But there is a small glitch to be removed. I have contextual and exposed filters in the master view which is inherited (this part is okay). But the problem is heading is repeated in the attached display. any way to get rid of this? screenshot attached.

enter image description here

cn flag
There are several ways to get rid of the heading. Personally I would use a Views template, but I would advise opening up a new question on "How to remove the second heading when using a Views attachment?" and someone else may have a better idea.
Score:1
cn flag

Objective: Show upcoming events, then past events.

The simplest way to implement this entirely through the View UI is to add a second display to the view.

Basic steps

  1. Create the main view display. Configure the events to display as you like.
  2. Set to sort by upcoming events, and filter so that only upcoming events are displayed.
  3. Add an Attachment display. For this display, modify the filter (This page: override) to only show past events, and modify the sort in the same way.
  4. In the Footer of the main view display, add the attachment.
KTM avatar
fi flag
KTM
Sorry to add one more requirement to it that it should display in a single listing ( single display). No tabs
cn flag
@KTM My answer will produce a view with all the content in a big list. There will be two separate tabs in the Views UI admin screens, but visitors to the actual view will see everything on one page.
KTM avatar
fi flag
KTM
Wow then that will be great! I will try this :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.