Source language is not set in nested Paragraphs when transitioning from a locked language (und, zxx) to another language. InvalidArgumentException: The entity cannot be translated since it is language neutral (und).

Created on 30 July 2023, about 1 year ago

Problem/Motivation

When attempting to change the language of a node from "Not specified" (und) to another language (e.g., en), an error occurs under specific conditions. This problem arises when the node (the content type) contains a field that references a Paragraph of Type A, which, in turn, includes another nested Paragraph of Type B. The issue is observed when using the "Paragraphs (stable)" widget for the field configuration.

The configuration of the "Paragraphs (stable)" widget for the referenced Paragraph Type A (referenced from the node) is set to "Closed" in "Edit mode," as depicted below:

So, if you created your node with "Not specified" language (und), then you edit it to change it to "English" (en) and keep the Paragraph widget closed, the nested Paragraph (Type B) fails to inherit the language of the host entity, leading to language inconsistency within the structure. The resulting behavior is as follows:

  • The node acquires the new language (en)
  • Paragraph Type A adopts the new language (en)
  • Paragraph Type B retains the old language (und)

Consequently, this issue becomes evident when attempting to create a new translation for the node, such as in Japanese (ja), resulting in an exception being thrown: InvalidArgumentException: The entity cannot be translated since it is language neutral (und). This exception originates from the addTranslation() function in core/lib/Drupal/Core/Entity/ContentEntityBase.php, triggered by the locked and non-translatable language of Paragraph B (und).

Interestingly, if you click the "Edit" button on the Paragraph widget to display the inline form for editing the Paragraph (while editing the node to change its language) and save the node, then all the Paragraphs, including the nested Paragraph type B, will correctly inherit the source language and the subsequent translations to the node will not fail.

This issue is distinct from the problems reported in the following links:

https://www.drupal.org/project/entity_reference_revisions/issues/3093853 🐛 InvalidArgumentException: The entity cannot be translated since it is language neutral (und). in Drupal\Core\Entity\ContentEntityBase->addTranslation() Closed: duplicate
https://www.drupal.org/project/paragraphs/issues/3082703 🐛 Paragraph translation source language is not set when parent is under Content Moderation Needs work

Unlike those issues, we have confirmed that this problem does not involve the content_moderation module.

The full exception trace is as follows:

InvalidArgumentException: The entity cannot be translated since it is language neutral (und). in Drupal\Core\Entity\ContentEntityBase->addTranslation() (line 959 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formElement(Object, 0, Array, Array, Object) (Line: 353)
Drupal\Core\Field\WidgetBase->formSingleElement(Object, 0, Array, Array, Object) (Line: 1116)
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formMultipleElements(Object, Array, Object) (Line: 111)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object, NULL) (Line: 1238)
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->form(Object, Array, Object) (Line: 183)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 781)
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formElement(Object, 0, Array, Array, Object) (Line: 353)
Drupal\Core\Field\WidgetBase->formSingleElement(Object, 0, Array, Array, Object) (Line: 1116)
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formMultipleElements(Object, Array, Object) (Line: 111)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object, NULL) (Line: 1238)
Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->form(Object, Array, Object) (Line: 183)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 121)
Drupal\Core\Entity\ContentEntityForm->form(Array, Object) (Line: 127)
Drupal\node\NodeForm->form(Array, Object) (Line: 106)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 534)
Drupal\Core\Form\FormBuilder->retrieveForm('node_event_form', Object) (Line: 281)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'default', Array) (Line: 394)
Drupal\content_translation\Controller\ContentTranslationController->add(Object, Object, Object, 'node')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 46)
Drupal\redirect_after_login\RedirectMiddleware->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 713)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Even if the Paragraph widget is closed when saving a host entity, update the referenced Paragraph entity (and the nested ones), so that they can inherit the language set in the host entity (in case those Paragraph types are translatable).

Remaining tasks

Detect where in the form submission process the appropriate language update should occur for the referenced translatable paragraphs.

🐛 Bug report
Status

Active

Version

1.15

Component

Code

Created by

🇨🇴Colombia camilo.escobar

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024