From 519e3b98574cd4e73f49d4cbbaa70cdfa73d26da Mon Sep 17 00:00:00 2001 From: Benjamin Rasmussen Date: Mon, 7 Oct 2024 10:17:05 +0200 Subject: [PATCH 1/2] Display branch address on series level. DDFHER-38 We have a custom field formatter for the address field, that pulls the address of the branch if no alternative is set. However, the code only worked for eventinstances, and not eventseries. I've fixed it, so now, it happens for both. This also meant moving some code from the `EventWrapper`, as that also only works with EventInstances. ..And that meant that I had to update the Rest API, to use the field formatter, rather than pulling data directly --- .../custom/dpl_event/src/EventWrapper.php | 28 ---------- .../FieldFormatter/BranchAddressFormatter.php | 56 +++++++++++++++++-- .../src/Services/EventRestMapper.php | 28 +++++----- .../layout/eventseries--full.html.twig | 4 +- 4 files changed, 70 insertions(+), 46 deletions(-) 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) } %} From f5da66df5876c589f14e4f404d397a897d77e4f3 Mon Sep 17 00:00:00 2001 From: Benjamin Rasmussen Date: Fri, 1 Nov 2024 14:24:08 +0100 Subject: [PATCH 2/2] Expand documentation. --- .../src/Plugin/Field/FieldFormatter/BranchAddressFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9e07ce8e1..5c66b7328 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 @@ -61,7 +61,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } /** - * Loading the field if it exists. + * Loading the field if it exists and has a value. */ private function getField(EventSeries|EventInstance $event, string $field_name): ?FieldItemListInterface { // First, let's look up the custom field - does it already have a value?