Problem/Motivation

Summary:
When adding a translation via the Content Translation UI (e.g. node/1329/translations/add/en/zh-hans), the page throws:
LogicException: The translation language cannot be changed (en). in Drupal\Core\Entity\ContentEntityBase->onChange() (line 851)
The stack trace points to InlineParagraphsWidget->formElement() and InlineParagraphsWidget (massageFormValues), where the widget calls $paragraphs_entity->set($langcode_key, $langcode) to align the paragraph’s langcode with the form. Core forbids changing the langcode of an existing content entity, so this triggers the exception when the referenced paragraph already exists in another language.

Steps to reproduce

  • Enable content translation for a content type that has a paragraph field.
  • Create a node (e.g. in English) with at least one paragraph.
  • Go to the “Translations” tab and add a translation (e.g. Chinese).
  • The add-translation form throws the LogicException when building the paragraph widget.

Proposed resolution

Only set the paragraph’s langcode when the paragraph is new ($paragraphs_entity->isNew()). When the paragraph already exists and has a different langcode, either switch to an existing translation with getTranslation($langcode) or do not call set($langcode_key, $langcode) so that core’s restriction is not violated. The same logic should be applied in both formElement() and the method that massages form values (e.g. massageFormValues()).

Comments

rajiv.singh created an issue. See original summary.

rajiv.singh’s picture

StatusFileSize
new1.76 KB

Adding a patch to fix the reported issue

rajiv.singh’s picture

StatusFileSize
new3.45 KB

I see the same fix is needed in the classic widget.
web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php file too.
ParagraphsWidget.php had the same pattern in two places and would trigger the same LogicException when adding a translation with the “Paragraphs (stable)” widget.