Add langcode constructor parameter to LayoutParagraphsLayout so that entity translations are shown correctly.
Closes #3305070
Merge request reports
Activity
270 288 $items = []; 271 289 foreach ($this->paragraphsReferenceField as $field_item) { 272 290 if ($field_item->entity) { 273 $items[] = $field_item->entity; 291 $items[] = $this->langcode ? $field_item->entity->getTranslation($this->langcode) : $field_item->entity; Check if the translation exists, and if not, create it. Use the original content for the translation.
(See https://www.drupal.org/project/layout_paragraphs/issues/3305070#comment-14999870)
Maybe something like:
291 $items[] = $this->langcode ? $field_item->entity->getTranslation($this->langcode) : $field_item->entity; 291 /** @var ContentEntityInterface $entity */ 292 $entity = $field_item->entity; 293 if ($this->langcode) { 294 $entity = $entity->hasTranslation($this->langcode) ? $entity->getTranslation($this->langcode) : $entity->addTranslation($this->langcode); 295 } 296 297 $items[] = $entity; changed this line in version 3 of the diff
270 288 $items = []; 271 289 foreach ($this->paragraphsReferenceField as $field_item) { 272 290 if ($field_item->entity) { 273 $items[] = $field_item->entity; 291 $items[] = $this->langcode ? $field_item->entity->getTranslation($this->langcode) : $field_item->entity; I agree that we need to test whether a translation exists or not. This is the logic that worked for me (instead of
$items[] = $this->langcode ? $field_item->entity->getTranslation($this->langcode) : $field_item->entity;
):if ($field_item->entity->hasTranslation($this->langcode)) { $items[] = $field_item->entity->getTranslation($this->langcode); } else { $items[] = $field_item->entity; }
When I tested the suggested change by @jleehr and edited a translated version of the node, a new paragraph created in the original language did not appear in the layout editor. With the above logic the new paragraph does appear and saves properly.
Edited by Jordan GrahamHaving tested my approach above and discussed it with colleagues I retract my suggestion.
I have tested the MR without the suggestion from @jleehr and found that if I add a paragraph in the original language and edit a translation of that node, the layout editor fails to open and Drupal logs a php error saying
InvalidArgumentException: Invalid translation language (es) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 874 of /app/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
With the suggestion from @jleehr the layout editor functions properly and no php error is logged.
changed this line in version 3 of the diff
added 1 commit
- 9a42a058 - Tested with this suggestion and it resolved an error that I experienced using...
270 288 $items = []; 271 289 foreach ($this->paragraphsReferenceField as $field_item) { 272 290 if ($field_item->entity) { 273 $items[] = $field_item->entity; 291 /** @var ContentEntityInterface $entity */ added 1 commit
- 301b105d - Makes sure original content is shown when creating a new translation
added 2 commits
added 32 commits
-
21ecf3b4...fe1637f1 - 31 commits from branch
project:2.0.x
- c53c50dc - Merge branch '2.0.x' into 3305070-set-language-on-layout
-
21ecf3b4...fe1637f1 - 31 commits from branch
283 300 $items = []; 284 301 foreach ($this->paragraphsReferenceField as $field_item) { 285 302 if ($field_item->entity) { 286 $items[] = $field_item->entity; 303 /** @var ContentEntityInterface $entity */ 304 $entity = $field_item->entity; 305 if ($this->langcode) { 306 $entity = $entity->hasTranslation($this->langcode) ? $entity->getTranslation($this->langcode) : $entity->addTranslation($this->langcode, $entity->toArray());