diff --git a/web/modules/custom/dpl_event/src/EventWrapper.php b/web/modules/custom/dpl_event/src/EventWrapper.php index 7ecdba38d..80acec7d7 100644 --- a/web/modules/custom/dpl_event/src/EventWrapper.php +++ b/web/modules/custom/dpl_event/src/EventWrapper.php @@ -4,7 +4,6 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\drupal_typed\DrupalTyped; -use Drupal\node\NodeInterface; use Drupal\recurring_events\Entity\EventInstance; use Psr\Log\LoggerInterface; use Safe\DateTimeImmutable; @@ -86,33 +85,6 @@ private function getDate(string $value): \DateTimeInterface { return new DateTimeImmutable($event_date_values[$value], new \DateTimeZone('UTC')); } - /** - * Load an eventinstance address - either from the series/instance or branch. - */ - public function getAddressField(): ?FieldItemListInterface { - $instance_field = $this->getField('event_address'); - - if ($instance_field instanceof FieldItemListInterface) { - return $instance_field; - } - - // Could not find data - look up address from branch instead. - $branch_field = $this->getField('branch'); - - if (!$branch_field instanceof FieldItemListInterface) { - return NULL; - } - - $branch_address_field = 'field_address'; - $branch = $branch_field->referencedEntities()[0] ?? NULL; - - if (!($branch instanceof NodeInterface) || !$branch->hasField($branch_address_field)) { - return NULL; - } - - return $branch->get($branch_address_field); - } - /** * Get the EventState object of an eventinstance. */ diff --git a/web/modules/custom/dpl_event/src/Plugin/Field/FieldFormatter/BranchAddressFormatter.php b/web/modules/custom/dpl_event/src/Plugin/Field/FieldFormatter/BranchAddressFormatter.php index 2c555c810..9e07ce8e1 100644 --- a/web/modules/custom/dpl_event/src/Plugin/Field/FieldFormatter/BranchAddressFormatter.php +++ b/web/modules/custom/dpl_event/src/Plugin/Field/FieldFormatter/BranchAddressFormatter.php @@ -4,8 +4,9 @@ use Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\dpl_event\EventWrapper; +use Drupal\node\NodeInterface; use Drupal\recurring_events\Entity\EventInstance; +use Drupal\recurring_events\Entity\EventSeries; /** * A custom address field formatter: Get fallback address from branch. @@ -46,12 +47,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $entity = $items->getEntity(); - if (!($entity instanceof EventInstance)) { + if (!($entity instanceof EventInstance) && !($entity instanceof EventSeries)) { return $default_return; } - $wrapper = new EventWrapper($entity); - $field = $wrapper->getAddressField(); + $field = $this->getAddressField($entity); if (!$field instanceof FieldItemListInterface) { return $default_return; @@ -60,4 +60,52 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $field->view(); } + /** + * Loading the field if it exists. + */ + private function getField(EventSeries|EventInstance $event, string $field_name): ?FieldItemListInterface { + // First, let's look up the custom field - does it already have a value? + if ($event->hasField($field_name)) { + $field = $event->get($field_name); + + if (!$field->isEmpty()) { + return $field; + } + } + + return NULL; + } + + /** + * Load an event address - either from the series/instance or branch. + */ + private function getAddressField(EventSeries|EventInstance $event): ?FieldItemListInterface { + $address_field_name = ($event instanceof EventSeries) ? + 'field_event_address' : 'event_address'; + $branch_field_name = ($event instanceof EventSeries) ? + 'field_branch' : 'branch'; + + $field = $this->getField($event, $address_field_name); + + if ($field instanceof FieldItemListInterface) { + return $field; + } + + // Could not find data - look up address from branch instead. + $branch_field = $this->getField($event, $branch_field_name); + + if (!$branch_field instanceof FieldItemListInterface) { + return NULL; + } + + $branch_address_field = 'field_address'; + $branch = $branch_field->referencedEntities()[0] ?? NULL; + + if (!($branch instanceof NodeInterface) || !$branch->hasField($branch_address_field)) { + return NULL; + } + + return $branch->get($branch_address_field); + } + } diff --git a/web/modules/custom/dpl_event/src/Services/EventRestMapper.php b/web/modules/custom/dpl_event/src/Services/EventRestMapper.php index 8249af5f5..b24d9cc1e 100644 --- a/web/modules/custom/dpl_event/src/Services/EventRestMapper.php +++ b/web/modules/custom/dpl_event/src/Services/EventRestMapper.php @@ -226,27 +226,29 @@ private function getTicketCategories(): array { * * Notice that this may be the address of the related branch. * - * @see eventWrapper->getAddressField() + * @see BranchAddressFormatter */ - private function getAddress(): ?EventsGET200ResponseInnerAddress { - $field = $this->eventWrapper->getAddressField(); + private function getAddress(): EventsGET200ResponseInnerAddress { + // Loading the field, and rendering it, to let the BranchAddressFormatter + // do the work of looking up a possible branch. + $rendered = $this->event->get('event_address')->view('full'); - if (!($field instanceof FieldItemListInterface)) { - return NULL; - } + $zip = $rendered[0]['postal_code']['#value'] ?? NULL; + $address_1 = $rendered[0]['address_line1']['#value'] ?? NULL; + $address_2 = $rendered[0]['address_line2']['#value'] ?? NULL; - $value = $field->getValue(); + $street = "$address_1 $address_2"; - $zip = $value[0]['postal_code'] ?? NULL; - $address_1 = $value[0]['address_line1'] ?? NULL; - $address_2 = $value[0]['address_line2'] ?? NULL; + if (empty($address_1) && empty($address_2)) { + $street = NULL; + } $address = new EventsGET200ResponseInnerAddress(); $address->setLocation($this->getValue('event_place')); - $address->setStreet("$address_1 $address_2"); + $address->setStreet($street); $address->setZipCode(!empty($zip) ? intval($zip) : NULL); - $address->setCity($value[0]['locality'] ?? NULL); - $address->setCountry($value[0]['country_code'] ?? NULL); + $address->setCity($rendered[0]['locality']['#value'] ?? NULL); + $address->setCountry($rendered[0]['country_code']['#value'] ?? NULL); return $address; } diff --git a/web/themes/custom/novel/templates/layout/eventseries--full.html.twig b/web/themes/custom/novel/templates/layout/eventseries--full.html.twig index bd6be6225..32b84ff8a 100644 --- a/web/themes/custom/novel/templates/layout/eventseries--full.html.twig +++ b/web/themes/custom/novel/templates/layout/eventseries--full.html.twig @@ -16,10 +16,12 @@ "event_state": content.field_event_state, "event_ticket_categories": content.field_ticket_categories, "event_tags": content.field_tags, - "event_branch": content.field_branch, + "event_partners": content.field_event_partners, + "branch": content.field_branch, "event_teaser_text": content.field_teaser_text, } %} + {% include '@novel/layout/eventinstance--full.html.twig' with { content: content|merge(instancelike_content) } %}