diff --git a/src/site/filters/liquid.js b/src/site/filters/liquid.js index 0e8dfad5f2..60f397912b 100644 --- a/src/site/filters/liquid.js +++ b/src/site/filters/liquid.js @@ -1109,6 +1109,32 @@ module.exports = function registerFilters() { }); }; + //* Filters and Sorts event dates (fieldDatetimeRangeTimezone) starting with the most upcoming event. + //* Also sorts press releases (fieldReleaseDate) from newest to oldest. + liquid.filters.filterAndSortEvents = data => { + if (!data) return null; + const currentTimestamp = moment().unix(); + + const filteredEvents = data.filter(event => { + const occurrenceArray = event.fieldDatetimeRangeTimezone.map( + occurrence => { + return occurrence.value; + }, + ); + const futureOccurrences = occurrenceArray.filter( + occurrence => occurrence >= currentTimestamp, + ); + + return futureOccurrences.length > 0; + }); + + return liquid.filters.sortByDateKey( + filteredEvents, + 'fieldDatetimeRangeTimezone', + false, + ); + }; + //* paginatePages has limitations, it is not yet fully operational. liquid.filters.paginatePages = (page, items, aria) => { const perPage = 10; diff --git a/src/site/layouts/health_care_region_page.drupal.liquid b/src/site/layouts/health_care_region_page.drupal.liquid index 25b6bab310..b59a5a947f 100644 --- a/src/site/layouts/health_care_region_page.drupal.liquid +++ b/src/site/layouts/health_care_region_page.drupal.liquid @@ -186,7 +186,10 @@ {% elsif eventTeasersAll.entities.0.reverseFieldListingNode.entities.length %} {% assign upcomingEvents = eventTeasersAll.entities.0.reverseFieldListingNode.entities %} - {% assign sortedEvents = upcomingEvents | sortByDateKey: 'fieldDatetimeRangeTimezone', false %} + {% comment %} {% assign filteredEvents = upcomingEvents | filterPastEvents %} + {% assign sortedEvents = filteredEvents | sortByDateKey: 'fieldDatetimeRangeTimezone', false %} {% endcomment %} + + {% assign sortedEvents = upcomingEvents | filterAndSortEvents %}