Fix the ordering for future talks
Rather than the custom event sorting plugin being based on the `created` value, this change adds a new `field_event_date` field to the talk node type and uses this for the sorting instead. This commit also adds a new `TalkDateUpdater` service that extracts either the next event date if there is a future date, or the last past event date if there is no future date, from `field_events` for each talk and saves it into the event date field. For consistency, and to ensure that the results are ordered correctly, the talk date updater converts the date from a date string (e.g. `2020-08-24`) into a UNIX timestamp, and the timestamp is saved in the event date field. This can be changed at a later date if needed. The talks view has been updated to use the updated sort plugin, and the existing tests have been updated to use the new field. References #204
This commit is contained in:
parent
bdf225b05d
commit
6d9ecd8df0
17 changed files with 340 additions and 21 deletions
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\opd_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\custom\Entity\Node\Talk;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\opd_talks\Service\TalkDateUpdater;
|
||||
use Drupal\Tests\custom\Kernel\TalksTestBase;
|
||||
|
||||
final class TalkEventDateTest extends TalksTestBase {
|
||||
|
||||
/** @test */
|
||||
public function talk_event_dates_are_set_to_the_next_future_date(): void {
|
||||
$dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subWeeks(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->addDays(4)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->addDays(10)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$expected = Carbon::today()->addDays(4)->getTimestamp();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNextEventDateIs($talk, $expected);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function talk_event_dates_are_set_to_the_last_past_date(): void {
|
||||
$dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(4)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$expected = Carbon::today()->subDays(2)->getTimestamp();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNextEventDateIs($talk, $expected);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function next_event_date_is_empty_if_there_are_no_events(): void {
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNoNextEventDate($talk);
|
||||
}
|
||||
|
||||
private function assertNextEventDateIs(Talk $talk, $expected): void {
|
||||
$this->assertSame($expected, $talk->getNextDate());
|
||||
}
|
||||
|
||||
private function assertNoNextEventDate(Talk $talk): void {
|
||||
$this->assertNull($talk->getNextDate());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue