From cbd1417b24a608df8b451a3ab5c9f888de41e758 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 23 May 2024 23:58:42 +0100 Subject: [PATCH] Sort talks only by the event date Sort the talks on the listing page solely by `field_event_date`. Prior to this commit, the page was using a custom sort plugin that attempts to show future talks followed by past talks, but also affect the order to show upcoming talks in an ascending order, followed by past dalks in the opposite (descending) order. This was not working as expected. For now, solely using the event date field is the simpler solution. In the future, I'll either re-attempt this or remove the code and simplify my codebase. --- config/sync/views.view.talks.yml | 16 ++--- .../talks/src/Plugin/views/sort/Event.php | 69 ------------------- .../tests/src/Kernel/TalksPageSortTest.php | 41 ----------- 3 files changed, 8 insertions(+), 118 deletions(-) delete mode 100644 web/modules/custom/talks/src/Plugin/views/sort/Event.php delete mode 100644 web/modules/custom/talks/tests/src/Kernel/TalksPageSortTest.php diff --git a/config/sync/views.view.talks.yml b/config/sync/views.view.talks.yml index ffb3588..84abe8d 100644 --- a/config/sync/views.view.talks.yml +++ b/config/sync/views.view.talks.yml @@ -7,8 +7,8 @@ dependencies: - node.type.talk - system.menu.main module: + - datetime - node - - opdavies_talks - user id: talks label: 'Talks and workshops' @@ -96,20 +96,20 @@ display: options: { } empty: { } sorts: - event_sort: - id: event_sort - table: node__field_events - field: event_sort + field_event_date_value: + id: field_event_date_value + table: node__field_event_date + field: field_event_date_value relationship: none group_type: group admin_label: '' - plugin_id: event_sort - order: ASC + plugin_id: datetime + order: DESC expose: label: '' field_identifier: '' exposed: false - granularity: second + granularity: hour arguments: { } filters: status: diff --git a/web/modules/custom/talks/src/Plugin/views/sort/Event.php b/web/modules/custom/talks/src/Plugin/views/sort/Event.php deleted file mode 100644 index 8899899..0000000 --- a/web/modules/custom/talks/src/Plugin/views/sort/Event.php +++ /dev/null @@ -1,69 +0,0 @@ -time = $time; - } - - public static function create( - ContainerInterface $container, - array $configuration, - $pluginId, - $pluginDefinition - ) { - return new static( - $configuration, - $pluginId, - $pluginDefinition, - $container->get('datetime.time') - ); - } - - public function query(): void { - $this->ensureMyTable(); - - $currentDate = Carbon::parse('today')->getTimestamp(); - - $dateAlias = "$this->tableAlias.$this->realField"; - - // Is this event in the past? - $this->query->addOrderBy( - NULL, - sprintf("%d > %s", $currentDate, $dateAlias), - $this->options['order'], - "in_past" - ); - - // How far in the past/future is this event? - $this->query->addOrderBy( - NULL, - sprintf('ABS(%s - %d)', $dateAlias, $currentDate), - $this->options['order'], - "distance_from_now" - ); - } - -} diff --git a/web/modules/custom/talks/tests/src/Kernel/TalksPageSortTest.php b/web/modules/custom/talks/tests/src/Kernel/TalksPageSortTest.php deleted file mode 100644 index 43d3dd4..0000000 --- a/web/modules/custom/talks/tests/src/Kernel/TalksPageSortTest.php +++ /dev/null @@ -1,41 +0,0 @@ -createTalk([ - 'field_event_date' => Carbon::today()->addDays(4)->getTimestamp(), - ]); - $this->createTalk([ - 'field_event_date' => Carbon::today()->subDays(2)->getTimestamp(), - ]); - $this->createTalk([ - 'field_event_date' => Carbon::today()->addDay()->getTimestamp(), - ]); - $this->createTalk([ - 'field_event_date' => Carbon::today()->subDays(10)->getTimestamp(), - ]); - - $talkIds = (new Collection(views_get_view_result('talks'))) - ->map(fn(ResultRow $row) => (int) $row->_entity->id()); - - $this->assertSame([3, 1, 2, 4], $talkIds->toArray()); - } - -}