diff --git a/src/Form/LayoutParagraphsBuilderForm.php b/src/Form/LayoutParagraphsBuilderForm.php index 6711354..be757da 100644 --- a/src/Form/LayoutParagraphsBuilderForm.php +++ b/src/Form/LayoutParagraphsBuilderForm.php @@ -105,7 +105,12 @@ 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); + $field = $entity->$field_name; + // The entity is loaded by the routing system and is already in the + // correct language. + $langcode = $entity->language()->getId(); + + $this->layoutParagraphsLayout = new LayoutParagraphsLayout($field, $layout_paragraphs_settings, $langcode); $this->tempstore->set($this->layoutParagraphsLayout); $layout_paragraphs_storage_key = $this->tempstore->getStorageKey($this->layoutParagraphsLayout); } diff --git a/src/LayoutParagraphsLayout.php b/src/LayoutParagraphsLayout.php index 1b4faa4..32676ee 100644 --- a/src/LayoutParagraphsLayout.php +++ b/src/LayoutParagraphsLayout.php @@ -53,6 +53,13 @@ class LayoutParagraphsLayout implements ThirdPartySettingsInterface { */ protected $settings; + /** + * The language to use for the entities. + * + * @var string + */ + protected $langcode; + /** * The layout ID. * @@ -67,13 +74,21 @@ class LayoutParagraphsLayout implements ThirdPartySettingsInterface { * The paragraph reference field this layout is attached to. * @param array[] $settings * An array of settings. + * @param string $langcode + * (optional) The language of the parent entity. Because reference fields + * are untranslated, the field object is shared between translations of the + * parent entity, and so the entity obtained from the field is always the + * default language. This parameter is necessary to ensure that any entities + * are obtained in the correct language. */ public function __construct( EntityReferenceFieldItemListInterface $paragraphs_reference_field, - array $settings = [] + array $settings = [], + string $langcode = '', ) { $this->paragraphsReferenceField = $paragraphs_reference_field; $this->settings = $settings; + $this->langcode = $langcode; } /** @@ -97,6 +112,9 @@ class LayoutParagraphsLayout implements ThirdPartySettingsInterface { */ public function getEntity() { $entity = $this->paragraphsReferenceField->getEntity(); + if ($this->langcode) { + $entity = $entity->getTranslation($this->langcode); + } return $entity; } @@ -270,7 +288,13 @@ class LayoutParagraphsLayout implements ThirdPartySettingsInterface { $items = []; foreach ($this->paragraphsReferenceField as $field_item) { if ($field_item->entity) { - $items[] = $field_item->entity; + /** @var ContentEntityInterface $entity */ + $entity = $field_item->entity; + if ($this->langcode) { + $entity = $entity->hasTranslation($this->langcode) ? $entity->getTranslation($this->langcode) : $entity->addTranslation($this->langcode); + } + + $items[] = $entity; } } return $items;