From 6eed26a756874aa7b1f4ae38d7d22caa5e445151 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 18 Apr 2024 20:44:00 +0100 Subject: [PATCH] Revert "Remove unused views sort plugin" This reverts commit c9d44621ff7ea9d95713c5816b20af0d08e7d749. --- .../talks/src/Plugin/views/sort/Event.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 web/modules/custom/talks/src/Plugin/views/sort/Event.php diff --git a/web/modules/custom/talks/src/Plugin/views/sort/Event.php b/web/modules/custom/talks/src/Plugin/views/sort/Event.php new file mode 100644 index 0000000..8899899 --- /dev/null +++ b/web/modules/custom/talks/src/Plugin/views/sort/Event.php @@ -0,0 +1,69 @@ +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" + ); + } + +}