Skip to content

Commit

Permalink
Add event prices to RSS feed for Redia app
Browse files Browse the repository at this point in the history
Prices in the original feed are (mostly) displayed as raw integers in
the format "[price]" or "[low price] - [high price]". If the event is
free then there is no price element for it in the feed.

Add support for similar output in the our new implementation of the
feed by using the raw price range formatting.
  • Loading branch information
kasperg committed Nov 6, 2024
1 parent 9a72aeb commit 22623b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\dpl_event\PriceFormatter;
use Drupal\dpl_redia_legacy\RediaEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -24,6 +25,7 @@ class EventsController extends ControllerBase {
public function __construct(
protected FileUrlGeneratorInterface $fileUrlGenerator,
protected DateFormatterInterface $dateFormatter,
protected PriceFormatter $priceFormatter,
) {}

/**
Expand All @@ -33,6 +35,7 @@ public static function create(ContainerInterface $container): static {
return new static(
$container->get('file_url_generator'),
$container->get('date.formatter'),
$container->get('dpl_event.price_formatter'),
);
}

Expand Down Expand Up @@ -80,7 +83,7 @@ private function getItems(): array {
$items = [];

foreach ($events as $event) {
$items[] = new RediaEvent($event);
$items[] = new RediaEvent($event, $this->priceFormatter);
}

return $items;
Expand Down Expand Up @@ -184,6 +187,11 @@ private function buildRss(array $items, Request $request): string {
$xml->writeElement('content-rss:booking-url', $item->bookingUrl);
}

// Events without a price element are interpreted as free.
if ($item->prices) {
$xml->writeElement('content-rss:arrangement-price', $item->prices);
}

$xml->writeElement('content-rss:promoted', $item->promoted);
$xml->endElement();
}
Expand Down
12 changes: 11 additions & 1 deletion web/modules/custom/dpl_redia_legacy/src/RediaEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\dpl_event\EventWrapper;
use Drupal\dpl_event\PriceFormatter;
use Drupal\node\NodeInterface;
use Drupal\recurring_events\Entity\EventInstance;

Expand All @@ -28,6 +29,7 @@ class RediaEvent extends ControllerBase {
public ?RediaEventMedia $media;
public ?RediaEventMedia $mediaThumbnail;
public ?string $bookingUrl;
public ?string $prices;
// phpcs:enable

/**
Expand All @@ -38,7 +40,7 @@ class RediaEvent extends ControllerBase {
*/
public string $promoted;

public function __construct(EventInstance $event_instance) {
public function __construct(EventInstance $event_instance, PriceFormatter $price_formatter) {
$event_wrapper = new EventWrapper($event_instance);

$branch = $event_wrapper->getBranches()[0] ?? NULL;
Expand Down Expand Up @@ -73,6 +75,14 @@ public function __construct(EventInstance $event_instance) {
$this->branch = $branch;
$this->bookingUrl = $event_wrapper->getLink();

if (!$event_wrapper->isFreeToAttend()) {
$prices = $event_wrapper->getTicketPrices();
$this->prices = $price_formatter->formatRawPriceRange($prices);
}
else {
$this->prices = NULL;
}

// In the old system, there was a way for editors to mark content a
// promoted. However, this does not exist in the new CMS, so we wil
// just hardcode it.
Expand Down

0 comments on commit 22623b4

Please sign in to comment.