|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\hoeringsportal_public_meeting\EventSubscriber; |
| 4 | + |
| 5 | +use Drupal\node\NodeInterface; |
| 6 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 7 | +use Drupal\Core\Routing\TrustedRedirectResponse; |
| 8 | +use Drupal\Core\Url; |
| 9 | +use Drupal\hoeringsportal_public_meeting\Helper\PublicMeetingHelper; |
| 10 | +use Drupal\itk_pretix\Plugin\Field\FieldType\PretixDate; |
| 11 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 12 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 13 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 14 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 15 | + |
| 16 | +/** |
| 17 | + * An event subscriber. |
| 18 | + */ |
| 19 | +class RedirectEventSubscriber implements EventSubscriberInterface { |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private readonly RouteMatchInterface $routeMatch, |
| 23 | + #[Autowire(service: 'hoeringsportal_public_meeting.public_meeting_helper')] |
| 24 | + private readonly PublicMeetingHelper $publicMeetingHelper, |
| 25 | + ) {} |
| 26 | + |
| 27 | + /** |
| 28 | + * Redirect to first upcoming date (or first date) |
| 29 | + */ |
| 30 | + public function redirectToDate(RequestEvent $event) { |
| 31 | + // Redirect to a date if viewing a pretix meeting with no specific pretix |
| 32 | + // date requested. |
| 33 | + if ('entity.node.canonical' === $this->routeMatch->getRouteName() |
| 34 | + || ('hoeringsportal_public_meeting.public_meeting_date' === $this->routeMatch->getRouteName() |
| 35 | + && (int) $this->routeMatch->getParameter('dates_delta') < 0)) { |
| 36 | + /** @var \Drupal\node\NodeInterface $node */ |
| 37 | + $node = $this->routeMatch->getParameter('node'); |
| 38 | + if ($this->publicMeetingHelper->isPublicMeeting($node) && $this->publicMeetingHelper->hasPretixSignUp($node)) { |
| 39 | + $datesDelta = $this->getNextDatesDelta($node); |
| 40 | + if (NULL !== $datesDelta) { |
| 41 | + $url = Url::fromRoute( |
| 42 | + 'hoeringsportal_public_meeting.public_meeting_date', |
| 43 | + [ |
| 44 | + 'node' => $node->id(), |
| 45 | + 'dates_delta' => $datesDelta, |
| 46 | + ] |
| 47 | + ); |
| 48 | + $event->setResponse(new TrustedRedirectResponse($url->toString(TRUE) |
| 49 | + ->getGeneratedUrl())); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get next dates delta if any. |
| 57 | + * |
| 58 | + * The "next dates delta" is the delta of an upcoming date if any or of the |
| 59 | + * first date. |
| 60 | + */ |
| 61 | + private function getNextDatesDelta(NodeInterface $node): ?int { |
| 62 | + $getDelta = static function (PretixDate $date): ?int { |
| 63 | + $delta = (int) $date->getName(); |
| 64 | + |
| 65 | + return $delta > -1 ? $delta : NULL; |
| 66 | + }; |
| 67 | + |
| 68 | + // Check if we have a context with an upcoming date. |
| 69 | + $context = $this->publicMeetingHelper->getPublicMeetingContext($node); |
| 70 | + if (isset($context['upcoming'])) { |
| 71 | + if ($data = reset($context['upcoming'])) { |
| 72 | + $delta = $getDelta($data); |
| 73 | + if (NULL !== $delta) { |
| 74 | + return $delta; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $dates = $this->publicMeetingHelper->getPretixDates($node); |
| 80 | + if ($date = $dates->first()) { |
| 81 | + $delta = $getDelta($date); |
| 82 | + if (NULL !== $delta) { |
| 83 | + return $delta; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritdoc} |
| 92 | + */ |
| 93 | + public static function getSubscribedEvents() { |
| 94 | + return [ |
| 95 | + KernelEvents::REQUEST => [['redirectToDate']], |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments