Skip to content

Commit

Permalink
Merge pull request #1633 from danskernesdigitalebibliotek/DDFHER-38_a…
Browse files Browse the repository at this point in the history
…ddress-on-series

Display branch address on series level. DDFHER-38
  • Loading branch information
rasben authored Nov 8, 2024
2 parents d036804 + 3c0c1c3 commit aac50d7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
25 changes: 1 addition & 24 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 @@ -89,7 +88,7 @@ private function getDate(string $value): \DateTimeInterface {
/**
* Getting an events branches.
*
* @return array<NodeInterface>|null
* @return array<\Drupal\node\NodeInterface>|null
* The matching branches.
*/
public function getBranches(): ?array {
Expand All @@ -102,28 +101,6 @@ public function getBranches(): ?array {
return $field->referencedEntities() ?? NULL;
}

/**
* 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 first branch instead.
$branch = $this->getBranches()[0] ?? NULL;

$branch_address_field = 'field_address';

if (!($branch instanceof NodeInterface) || !$branch->hasField($branch_address_field)) {
return NULL;
}

return $branch->get($branch_address_field);
}

/**
* Getting the description, from the first available text paragraph.
*/
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 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?
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 @@ -209,27 +209,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 aac50d7

Please sign in to comment.