Skip to content
Snippets Groups Projects
Commit f29bb968 authored by Julian Pustkuchen's avatar Julian Pustkuchen Committed by Justin Toupin
Browse files

Issue #3263594: Incorrect handling of untranslatable (layout) paragraphs in...

Issue #3263594: Incorrect handling of untranslatable (layout) paragraphs in translatable environment
parent 5bce2e47
No related branches found
No related tags found
2 merge requests!103Issue #3295875: Add a new dedicated permission for Layout paragraphs configurations,!64Issue #3263594: Incorrect handling of untranslatable (layout) paragraphs in translatable environment
...@@ -617,52 +617,55 @@ class LayoutParagraphsBuilder extends RenderElement implements ContainerFactoryP ...@@ -617,52 +617,55 @@ class LayoutParagraphsBuilder extends RenderElement implements ContainerFactoryP
/** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem $item */ /** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem $item */
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
if (!empty($item->entity) && $item->entity instanceof ParagraphInterface) { if (!empty($item->entity) && $item->entity instanceof ParagraphInterface) {
// Now we're sure it's a paragraph:
$paragraph = $item->entity;
if (!$this->isTranslating()) { if (!$this->isTranslating()) {
// Set the langcode if we are not translating. // Set the langcode if we are not translating.
$langcode_key = $item->entity->getEntityType()->getKey('langcode'); $langcode_key = $paragraph->getEntityType()->getKey('langcode');
if ($item->entity->get($langcode_key)->value != $this->langcode) { if ($paragraph->get($langcode_key)->value != $this->langcode) {
// If a translation in the given language already exists, // If a translation in the given language already exists,
// switch to that. If there is none yet, update the language. // switch to that. If there is none yet, update the language.
if ($item->entity->hasTranslation($this->langcode)) { if ($paragraph->hasTranslation($this->langcode)) {
$item->entity = $item->entity->getTranslation($this->langcode); $paragraph = $paragraph->getTranslation($this->langcode);
} }
else { else {
$item->entity->set($langcode_key, $this->langcode); $paragraph->set($langcode_key, $this->langcode);
} }
} }
} }
else { else {
// Add translation if missing for the target language. // Add translation if missing for the target language,
if (!$item->entity->hasTranslation($this->langcode)) { // if the paragraph is translatable at all:
if ($paragraph->isTranslatable() && !$paragraph->hasTranslation($this->langcode)) {
// Get the selected translation of the paragraph entity. // Get the selected translation of the paragraph entity.
$entity_langcode = $item->entity->language()->getId(); $entity_langcode = $paragraph->language()->getId();
$source_langcode = $this->sourceLangcode ?? $entity_langcode; $source_langcode = $this->sourceLangcode ?? $entity_langcode;
// Make sure the source language version is used if available. // Make sure the source language version is used if available.
// Fetching the translation without this check could lead valid // Fetching the translation without this check could lead valid
// scenario to have no paragraphs items in the source version of // scenario to have no paragraphs items in the source version of
// to an exception. // to an exception.
if ($item->entity->hasTranslation($source_langcode)) { if ($paragraph->hasTranslation($source_langcode)) {
$entity = $item->entity->getTranslation($source_langcode); $paragraph = $paragraph->getTranslation($source_langcode);
} }
// The paragraphs entity has no content translation source field // The paragraphs entity has no content translation source field
// if no paragraph entity field is translatable, // if no paragraph entity field is translatable,
// even if the host is. // even if the host is.
if ($item->entity->hasField('content_translation_source')) { if ($paragraph->hasField('content_translation_source')) {
// Initialise the translation with source language values. // Initialise the translation with source language values.
$item->entity->addTranslation($this->langcode, $entity->toArray()); $paragraph->addTranslation($this->langcode, $paragraph->toArray());
$translation = $item->entity->getTranslation($this->langcode); $translation = $paragraph->getTranslation($this->langcode);
$manager = \Drupal::service('content_translation.manager'); $manager = \Drupal::service('content_translation.manager');
$manager->getTranslationMetadata($translation) $manager->getTranslationMetadata($translation)
->setSource($item->entity->language()->getId()); ->setSource($paragraph->language()->getId());
} }
} }
// If any paragraphs type is translatable do not switch. // If any paragraphs type is translatable do not switch.
if ($item->entity->hasField('content_translation_source')) { if ($paragraph->isTranslatable() && $paragraph->hasField('content_translation_source')) {
// Switch the paragraph to the translation. // Switch the paragraph to the translation.
$item->entity = $item->entity->getTranslation($this->langcode); $paragraph = $paragraph->getTranslation($this->langcode);
} }
} }
$items[$delta]->entity = $item->entity; $items[$delta]->entity = $paragraph;
} }
} }
$this->tempstore->set($this->layoutParagraphsLayout); $this->tempstore->set($this->layoutParagraphsLayout);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment