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" + ); + } + +}