From 61067b354bb30a026a35407cf769c73aa9c6d03a Mon Sep 17 00:00:00 2001
From: Owen Bush <owen.bush@lullabot.com>
Date: Fri, 26 May 2023 19:47:26 -0600
Subject: [PATCH] Resolve issue with creating translations and displaying the
 event instances

---
 src/EventCreationService.php                | 11 ++++++++++-
 src/EventUserTrait.php                      | 11 +++++++++++
 src/Plugin/ComputedField/EventInstances.php |  3 +--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/EventCreationService.php b/src/EventCreationService.php
index b504f04..edb51d9 100644
--- a/src/EventCreationService.php
+++ b/src/EventCreationService.php
@@ -533,7 +533,12 @@ class EventCreationService {
       $entity = $storage->create($data);
     }
     else {
+      // Grab the untranslated event series.
       $original = $event->getUntranslated();
+      // Find the corresponding default language event instance that matches
+      // the date and time of the version we wish to translate, so that we are
+      // mapping the translations from default language to translated language
+      // appropriately.
       $entity_ids = $storage->getQuery()
         ->condition('date__value', $data['date']['value'])
         ->condition('date__end_value', $data['date']['end_value'])
@@ -544,8 +549,12 @@ class EventCreationService {
         ->execute();
 
       if (!empty($entity_ids)) {
+        // Load the default language version of the event instance.
         $entity = $storage->load(reset($entity_ids));
-        $entity->addTranslation($event->language()->getId(), $data);
+        // Only add a translation if we do not already have one.
+        if (!$entity->hasTranslation($event->language()->getId())) {
+          $entity->addTranslation($event->language()->getId(), $data);
+        }
       }
     }
 
diff --git a/src/EventUserTrait.php b/src/EventUserTrait.php
index e80b112..7969453 100644
--- a/src/EventUserTrait.php
+++ b/src/EventUserTrait.php
@@ -80,4 +80,15 @@ trait EventUserTrait {
   public static function getDefaultEntityOwner() {
     return \Drupal::currentUser()->id();
   }
+
+  /**
+   * Backwards compatibility for getCurrentUserId().
+   *
+   * @return mixed
+   *   A default value for the uid field.
+   */
+  public static function getCurrentUserId() {
+    return static::getDefaultEntityOwner();
+  }
+
 }
diff --git a/src/Plugin/ComputedField/EventInstances.php b/src/Plugin/ComputedField/EventInstances.php
index 294679a..769412b 100644
--- a/src/Plugin/ComputedField/EventInstances.php
+++ b/src/Plugin/ComputedField/EventInstances.php
@@ -20,7 +20,6 @@ class EventInstances extends EntityReferenceFieldItemList {
     if (!empty($entity->id())) {
       $instances = \Drupal::entityTypeManager()->getStorage('eventinstance')->loadByProperties([
         'eventseries_id' => $entity->id(),
-        'langcode' => $this->getLangcode(),
       ]);
 
       // Sort by instance start date and reindex by field item delta, as
@@ -30,7 +29,7 @@ class EventInstances extends EntityReferenceFieldItemList {
       });
 
       foreach ($instances as $key => $instance) {
-        $this->list[$key] = $this->createItem($key, $instance);
+        $this->list[$key] = $this->createItem($key, $instance->getTranslation($this->getLangcode()));
       }
     }
   }
-- 
GitLab