From 86def6b57a5456b0df93b7920b1c23a537cb1926 Mon Sep 17 00:00:00 2001
From: Justin Toupin <justintoupin@Justins-MacBook-Pro.local>
Date: Mon, 27 Feb 2023 11:53:45 -0700
Subject: [PATCH] Move language functionality for frontend builder out of
 controller and into form.

---
 src/Form/LayoutParagraphsBuilderForm.php | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/Form/LayoutParagraphsBuilderForm.php b/src/Form/LayoutParagraphsBuilderForm.php
index 6711354..1e634ef 100644
--- a/src/Form/LayoutParagraphsBuilderForm.php
+++ b/src/Form/LayoutParagraphsBuilderForm.php
@@ -105,7 +105,19 @@ class LayoutParagraphsBuilderForm extends FormBase {
       $render_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
       $renderer = $render_display->getRenderer($field_name);
       $layout_paragraphs_settings = $renderer->getSettings() + ['reference_field_view_mode' => $view_mode];
-      $this->layoutParagraphsLayout = new LayoutParagraphsLayout($entity->{$field_name}, $layout_paragraphs_settings);
+
+      // Load the correct translation for the layout paragraphs layout field.
+      $default_langcode_key = $entity->getEntityType()->getKey('default_langcode');
+      $langcode_key = $entity->getEntityType()->getKey('langcode');
+      $langcode = $entity->get($langcode_key)->value;
+      if ($entity->hasField($default_langcode_key)) {
+        $field = $entity->getTranslation($langcode)->$field_name;
+      }
+      else {
+        $field = $entity->$field_name;
+      }
+
+      $this->layoutParagraphsLayout = new LayoutParagraphsLayout($field, $layout_paragraphs_settings);
       $this->tempstore->set($this->layoutParagraphsLayout);
       $layout_paragraphs_storage_key = $this->tempstore->getStorageKey($this->layoutParagraphsLayout);
     }
-- 
GitLab