Skip to content

Commit

Permalink
Display branch address on series level. DDFHER-38
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rasben committed Oct 7, 2024
1 parent e759c73 commit 519e3b9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 46 deletions.
28 changes: 0 additions & 28 deletions web/modules/custom/dpl_event/src/EventWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

}
28 changes: 15 additions & 13 deletions web/modules/custom/dpl_event/src/Services/EventRestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
} %}

0 comments on commit 519e3b9

Please sign in to comment.