Trying to edit certain nodes I am getting:

The website encountered an unexpected error. Please try again later.

InvalidArgumentException: Invalid translation language (und) specified. in Drupal\Core\Entity\ContentEntityBase->addTranslation() (line 953 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
entity_reference_revisions_entity_revision_create(Object, Object, NULL)
call_user_func_array('entity_reference_revisions_entity_revision_create', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_revision_create', Array) (Line: 327)
Drupal\Core\Entity\ContentEntityStorageBase->createRevision(Object, ) (Line: 294)
Drupal\content_moderation\EntityTypeInfo->entityPrepareForm(Object, 'edit', Object) (Line: 136)
content_moderation_entity_prepare_form(Object, 'edit', Object)
call_user_func_array('content_moderation_entity_prepare_form', Array) (Line: 408)
Drupal\Core\Entity\EntityForm->prepareInvokeAll('entity_prepare_form', Object) (Line: 139)
Drupal\Core\Entity\EntityForm->init(Object) (Line: 291)
Drupal\Core\Entity\ContentEntityForm->init(Object) (Line: 107)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 518)
Drupal\Core\Form\FormBuilder->retrieveForm('node_services_edit_form', Object) (Line: 275)
Drupal\Core\Form\FormBuilder->buildForm('node_services_edit_form', Object) (Line: 93)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
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: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 41)
Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 669)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

It seems odd that entity_reference_revisions_entity_revision_create is being called by simply loading node/xx/edit

Seems related to comment 83 of #2961399: Support parallel translation forward revisions on untranslatable fields.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alberto56 created an issue. See original summary.

alberto56’s picture

Here is a patch which, at least, does not result in a fatal error. See screenshot.

alberto56’s picture

Status: Active » Needs review

In my case field_resources is a paragraph:

            [field_resources] => Array
                (
                    [x-default] => Array
                        (
                            [0] => Array
                                (
                                    [target_id] => 2236
                                    [target_revision_id] => 2816
                                )

                        )

                )

Calling dpm(\Drupal\paragraphs\Entity\Paragraph::load(2236)); works fine, so I'm not sure why this is happening. The revision ID seems to exist, too, so I'm not sure what's going on:

MySQL [drupal]> select * from paragraphs_item_revision where id=2236;
+------+-------------+----------+--------------+------------------+
| id   | revision_id | langcode | revision_uid | revision_default |
+------+-------------+----------+--------------+------------------+
| 2236 |        2816 | en       |            0 |                1 |
+------+-------------+----------+--------------+------------------+
1 row in set (0.00 sec)

In any event, perhaps the try/catch block is better because it prevents a fatal error when editing a page.

Saving the node, in my example, then coming back to the edit page, resulted in the error message disappearing.

It should be noted that this particular content was migrated from a Drupal 7 site so there might be something strange with the data.

lpeabody’s picture

I'm able to reproduce this error like so:

1) Edit a node that has a language assignment.
2) Set node language to Not specified.
3) Save the node.
4) Edit the node, get the error message in the main summary above.

lpeabody’s picture

Adding what could be a related issue. Though, the patch from that issue does not seem to solve our problem.

lpeabody’s picture

Some rapid feedback I can give from some more testing I've just done...

1) If I make sure that every paragraph and nested paragraphs are expanded in the node edit form, and I set the node language to Not specified (und), and save I can then edit the node again without error.

2) If I disable content moderation for that content type I can save/edit the node again in any combination of languages (including und).

So I think additional work needs to be done to make ERR and content moderation play nicely together.

I'll have to revisit this over the weekend or early next week to provide more information as I need to get this working for a client.

dieuwe’s picture

Status: Needs review » Needs work

I have the same issue here, and also with migrated data from Drupal 7. The patch "fixes" the fatal error, but doesn't do much beyond that.

When I view the node, I can see the rendered paragraph, but when I edit it (using the patch) I now see the following error and then there are no paragraph loaded in the entity reference field:

entity_reference_revisions_entity_revision_create had trouble updating delta 0 of field_co_owners; moving on.

It's a starting point for looking into exactly WHY some of these migrated entities are broken and what we can do to fix them. I'll let you know when I find out what it is.

dieuwe’s picture

Here's some info as to how I managed to work around the problem:

  1. I discovered in the addTranslation function that the reason the translation could not be added is because the UND translation is locked.
  2. I enabled the language module, and ran a config export of the UND language file.
  3. I changed "locked" to "false" and imported the updated configuration.

What was actually happening is that the node revision was send to 'und' while the paragraph revision was set to 'en' and so no matching translation could be generated.

A more permanent and safe solution might be to convert all content to either 'und' or 'en', but I am not sure which should be preferable or how this might interfere with the content moderation module.

You'll run unto this problem if you have migrated content with the langcode 'und' and then some paragraphs that have been assigned the 'en' langcode (in my case I think these were generated separately, not part of the original migration).

Perhaps someone could suggest some kind of update hook that can be used to normalise all content to the correct langcode?

keboca’s picture

I can confirm the patch https://www.drupal.org/project/entity_reference_revisions/issues/3024588... works charming in my case. Thanks folks!

keboca’s picture

@dieuwe I agree with your comments. Actually there's a blog which's pointing to same issue,
https://atendesigngroup.com/blog/fixing-invalid-translations-drupal-8-co...

I tried to create an hook_update_N in order to execute `$node->set('langcode', $langcode);` into all nodes with that bug. However the method invokes `addTranslation` which trigger the same issue, then no luck. Reason why I came with this query inside a hook_update_N:

use Drupal\Core\Language\Language;

Drupal::database()
    ->update('node')
    ->fields([
      'langcode' => Drupal::languageManager()->getDefaultLanguage()->getId(),
    ])
    ->condition('langcode', Language::LANGCODE_NOT_SPECIFIED)
    ->execute();

It update manually all nodes with UND language code defined. Even tho, the messages provided by the patch was still there:

entity_reference_revisions_entity_revision_create had trouble updating delta 0 of field_NAME; moving on.

As soon as I update any module with those messages Drupal does its things, then the message is vanish and it won't longer appear.
I would say it's good enough, nonetheless if you want to go further, then you may create another hook_update_N to load all nodes and save them again, surely it will solve everything at once.
Be aware to avoid any timeout while updating all your content.

alberto56’s picture

The patch in #2 no longer applies to 8.x-1.7 released 5 October 2019.

Here is the same patch which applies to the latest version of the code.

davo20019’s picture

I had the same problem, and it was happening because of a file field that one of my paragraphs had, was not checked as translatable at /admin/config/regional/content-language. Only the Alt and Title fields of the file field were checked as translatable.

fredonia_webteam’s picture

We have the same problem. This error is happening when content_moderation module is configured for node content types. It seems the error is only happening on pages that contain paragraphs. Language is not enabled. The paragraphs fields are being migrated from Drupal 7 come over with the language en.

flodevelop’s picture

Same here @davo2019.
Caused by a file field non translatable inside a paragraph.
With Content Moderation module enable on the parent node content type.

lisp’s picture

Same "problem" as mentioned at #12

Drupal version: 8.9.1

Error message: InvalidArgumentException: Invalid translation language (und) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 873 of /var/www/html/drupal/...web/core/lib/Drupal/Core/Entity/ContentEntityBase.php).

Checking image checkbox in /admin/config/regional/content-language in the paragraph section did solve the problem for me.

vistree’s picture

Hi, I have exactly the same problem!! Using content moderation with paragraphs in a multi language environment. We need to allow users to choose the language option "Language: Not specified". Problems start, when users try to change afterwards to a different language option (like EN).

On content type "basic page" I have an "entity reference revsion" field which links to a paragraph (field_section). Within this paragraph, I have an additonal "entity reference revsion" field, which again links a paragraph (field_section_content).

From what I have seen in all documentations and even the interface tells me NOT to translate the field in the sub-paragraph (field_section_content). Using patch from #11 I can see the error pointing to field_section_content.
As a test I enabled translation for this field - and the error has gone away (and existing sections are again displayed inside node edit form).

But, can I now keep the translation of field "field_section_content" on or will it break my site because of "https://www.drupal.org/node/2735121"?? Any idea on how to solve this? By the way: we need the

sic’s picture

we are also having this problem, out of the blue. adding an english translation (from german for instance) does run into the error. adding other translations, lets say from german to french, works just fine. 8.9.1

juampynr’s picture

In my case this was caused because I had a custom paragraphs migration that was setting the langcode to en, while the node migration that referenced such paragraph was using und.

Changing the custom migration to use und as the default langcode fixed it.

vistree’s picture

We don't use migrated content. Problem is also for fresh content + paragraphs add directly in the new nodes.

pratik_kamble’s picture

I have migrated the content from D7 to D8. And then turn on the content moderation module. Post to that I ran into this problem. Below code works for me.

diff --git a/entity_reference_revisions.module b/entity_reference_revisions.module
index fa24f77..5bb83eb 100644
--- a/entity_reference_revisions.module
+++ b/entity_reference_revisions.module
@@ -271,6 +271,9 @@ function entity_reference_revisions_entity_revision_create(ContentEntityInterfac
 
             /** @var \Drupal\Core\Entity\ContentEntityInterface $target_entity */
             $target_entity = $item->entity;
+            if ($active_langcode == 'und') {
+              $active_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
+            }
             if (!$target_entity->hasTranslation($active_langcode)) {
               $target_entity->addTranslation($active_langcode, $target_entity->toArray());
             }
dorficus’s picture

@pratik_kamble: did you make a patch for this? the current one is not applying and I'm running into this issue as well. I don't want to duplicate work if you've already put in the effort. Also, it couldn't hurt to contribute the patch back.

gsa’s picture

Here's the patch using the code from #20.
Works fine in my case.

jimkeller’s picture

We were still seeing issues with the patch in #20; attached is an updated patch that doesn't try to addTranslation on the default language, and also won't try to getTranslation unless it exists.

I'm not convinced this fix will actually suffice for multilingual sites - it seems like we're just juggling "und" and the default language code ("en" for me) to work around the exceptions. This is fine for my case since I only have one language but I feel like a more substantial fix is necessary here.

Lal_’s picture

Status: Needs work » Reviewed & tested by the community

#22 will come down with an error, when you want convert a entity in 'und' to 'en' (Drupal doesn't allow that) and thats why the check is necessary... IMO #23 is the best fix for this... Also tested on my local and it works perfectly... Thanks!!

nironan’s picture

We have #23 installed on a multilingual website and can confirm that we didn't get any side effect so far

dioni’s picture

I can confirm the same, multilingual website and working with #23 installed

tmwagner’s picture

Confirmed: Had exactly the same issue as described. Oddly, it was not happening until we updated core to 8.9.16.

Patch solved the problem

attisan’s picture

Status: Reviewed & tested by the community » Needs review
Related issues: +#2961399: Support parallel translation forward revisions on untranslatable fields
StatusFileSize
new2.22 KB

Added usage of \Drupal\Core\Language\LanguageInterface constants instead of using 'und' string and added a check against LanguageInterface::LANGCODE_NOT_APPLICABLE (see #2961399: Support parallel translation forward revisions on untranslatable fields).

vistree’s picture

We have the same problem with Drupal 9.3.8 and 9.3.9. Pathes from #23 and #28 both apply - but we still get the error. Content moderation is NOT in place, but the page is multilingual

mkindred’s picture

I ran into this error on Drupal 9.3.9 site (not multilingual) with content moderation and paragraphs migrated from D7. Patch #23 worked so far.

Liam Morland made their first commit to this issue’s fork.

liam morland’s picture

I just created a merge request with patches 22, 23, and 28, so the interdiffs can easily be seen.

steinmb’s picture

I assume it is coming from here. Sorry, have not tested the merge request.

Source ID(s)      Destination ID(s)   Level   Message
10508:10508:und   ::                  1       Invalid translation language (und) specified. 
(web/core/lib/Drupal/Core/Entity/ContentEntityBase.php:956)
steinmb’s picture

Tested MR on top of latest dev and D9.4.4 - I still got the error during a node migration from D7 field_collection to paragraph. Target site only have English lang. installed. though the language module is intalled.

Multilingual                      Language (language)                                                                      Enabled    9.4.4
adriancotter’s picture

I tested the MR on with Core 9.4.4 and am getting the same error. Although I was not using the latest dev.
For me, These errors are happening for node migrations. No language module installed. These errors were not happening last time I did the migration a couple of weeks ago. I'm not sure the exact change in between -- something on D7? or I think I did update D9 Core in there. It certainly wasn't the entity_reference_revision module though.

[error] Invalid translation language (und) specified. (/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php:956)

I assume that it should never be getting to the addTranslation method here in ContentEntityBase, since by definition the language of my content is correctly 'und'?

I'll try and play around with the code this coming week.

## UPDATE ### Turns out this error -- for me -- was a complete red herring. When I started testing more content types -- I got some duplicate ID errors instead of the translation language error... and then remembered that I had a process creating nodes of another sort.... possibly ones that didn't have revisions turned on? Either way -- I'm not entirely sure why it ended up with the translation error -- but the problem was a node existing in a place it shouldn't...

steinmb’s picture

Ref. #34 and #35 - My problem turned out to be similar to #36. In other words you can safely ignore my reports. Colliding id between D9 and 7 lead to migration tried to upgrade wrong node bundle during migration.

Anybody made their first commit to this issue’s fork.

DieterHolvoet made their first commit to this issue’s fork.

dieterholvoet’s picture

Rebase was a bit too complex so I merged 8.x-1.x in the issue fork.

damienmckenna’s picture

Rerolled.

anup.singh’s picture

#41 worked for me.

dieterholvoet’s picture

The latest state of the MR doesn't fix the issue for me. When saving a translation, I get 'Non-translatable field elements can only be changed when updating the current revision.'. This issue is not present when I apply the patch from #41.

In both cases I do have another issue: I still get the 'Invalid translation language (und) specified' error, unless I add the following snippet, in that case everything works as it should.

-                $target_entity->addTranslation($active_langcode, $target_entity->toArray());
+                $target_entity_translation = $target_entity->addTranslation($active_langcode, $target_entity->toArray());
+                if (\Drupal::hasService('content_translation.manager')) {
+                  $manager = \Drupal::service('content_translation.manager');
+                  $manager->getTranslationMetadata($target_entity_translation)->setSource($target_entity->language()->getId());
+                }

I have seen this fix/workaround be necessary for multiple other modules dealing with translations, so I'll add it here as well.

It's probably an issue with content_translation, but I haven't been able to figure that out.

berdir’s picture

Status: Needs review » Needs work

This seems to require a manual rebase.

jtwalters made their first commit to this issue’s fork.

firewaller’s picture

#41 works for me

rupertj made their first commit to this issue’s fork.

rupertj’s picture

#41 no longer applies. I've updated the issue fork with the code from 8.x-1.x. It's mergeable again and I'm successfully using the MR diff as a patch against 8.x-1.13.

There's a test failure in the pipeline, but it seems to be unrelated to the change in this MR?

drupov’s picture

Hey @rupertj - thanks for the update!

I'm successfully using the MR diff as a patch against 8.x-1.13.

Can you post the link to the diff here also?

rupertj’s picture

@drupov - Click the link under the issue summary that says "changes". Then there's a link under the "Code" button in the top right to the changes in the MR as a diff. You can do that for any MR in GitLab.

berdir’s picture

Status: Needs work » Fixed

Test coverage helps issues like this to get committed, but I also don't want to hold this up forever as this clearly affects quite a few people.

Did my best with the contribution credits, lots of people in this issue. Merging...

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • berdir committed 13e30652 on 8.x-1.x authored by liam morland
    fix: #3024588 InvalidArgumentException: Invalid translation language (...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

anybody’s picture

GREAT work, thank you berdir! Just ran into this and upgrading to the latest version fixed it. Hero of the day!! :)