Skip to content

Commit

Permalink
Merge pull request #1773 from danskernesdigitalebibliotek/DDFHER-132_…
Browse files Browse the repository at this point in the history
…event-api-changed

Update event API, editing series should reflect in changed date. DDFHER-132
  • Loading branch information
rasben authored Nov 28, 2024
2 parents 7acdbf8 + 2d03d2d commit 3147e01
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions web/modules/custom/dpl_event/src/EventWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\drupal_typed\DrupalTyped;
use Drupal\recurring_events\Entity\EventInstance;
use Psr\Log\LoggerInterface;
use Safe\DateTime;
use Safe\DateTimeImmutable;

/**
Expand Down Expand Up @@ -147,6 +148,36 @@ public function getState(): ?EventState {
return NULL;
}

/**
* Getting relevant updated date - either the series or instance.
*
* As we use inheritance, we want an updated series to also reflect update.
* We could implement this, by programmatically saving all instances when
* the series is saved, but this may have unforseen consequences, as it is
* working against the Drupal system.
* Instead, we'll look up the instance and series changed dates, and take
* which ever is newer.
*/
public function getUpdatedDate(): ?DateTime {
$series = $this->event->getEventSeries();

$changed_instance = $this->event->getChangedTime();
$changed_series = $series->getChangedTime();

// Setting the timestamp to whichever is the larger.
$timestamp = ($changed_instance > $changed_series) ?
$changed_instance : $changed_series;

if (empty($timestamp)) {
return NULL;
}

$date = new DateTime();
$date->setTimestamp(intval($timestamp));

return $date;
}

/**
* Loading the field if it exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getResponse(EventInstance $event_instance): EventsGET200Response
'ticketCapacity' => $this->getValue('event_ticket_capacity'),
'ticketCategories' => $this->getTicketCategories(),
'createdAt' => $this->getDateField('created'),
'updatedAt' => $this->getDateField('changed'),
'updatedAt' => $this->eventWrapper->getUpdatedDate(),
'dateTime' => $this->getDate(),
'externalData' => $this->getExternalData(),
'series' => new EventsGET200ResponseInnerSeries([
Expand Down

0 comments on commit 3147e01

Please sign in to comment.