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()).
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | paragraphs-3573596-3.patch | 3.45 KB | rajiv.singh |
| #2 | paragraphs-3573596-1.patch | 1.76 KB | rajiv.singh |
Comments
Comment #2
rajiv.singh commentedAdding a patch to fix the reported issue
Comment #3
rajiv.singh commentedI 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.