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.
This commit is contained in:
parent
81f31a0615
commit
cbd1417b24
|
@ -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:
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_talks\Plugin\views\sort;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\views\Annotation\ViewsSort;
|
||||
use Drupal\views\Plugin\views\sort\Date;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @ViewsSort("event_sort")
|
||||
*/
|
||||
final class Event extends Date {
|
||||
|
||||
private TimeInterface $time;
|
||||
|
||||
public function __construct(
|
||||
array $configuration,
|
||||
string $pluginId,
|
||||
array $pluginDefinition,
|
||||
TimeInterface $time
|
||||
) {
|
||||
parent::__construct($configuration, $pluginId, $pluginDefinition);
|
||||
|
||||
$this->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"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
// phpcs:disable Drupal.Commenting.DocComment, Drupal.NamingConventions.ValidFunctionName
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\views\ResultRow;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class TalksPageSortTest extends TalksTestBase {
|
||||
|
||||
public static $modules = [
|
||||
'views',
|
||||
'opdavies_talks',
|
||||
];
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function upcoming_talks_are_shown_first_followed_by_past_talks_and_ordered_by_distance(): void {
|
||||
$this->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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue