Summary

An English node cannot be translated to German if a field using IEF references Finnish content (and that reference has no English translation).

Reproducible with a fresh site on https://simplytest.me:

  • Drupal Core 8.3.7
  • Inline Entity Form 8.x-1.0-beta1

Preparation:

Site:

  1. Install standard site with English
  2. Install Inline Entity Form module at /admin/modules
  3. Install Content Translation module at /admin/modules (enables Language module)
  4. Add German and Finnish at /admin/config/regional/language/add
  5. Change error display: All messages, with backtrace information at /admin/config/development/logging

Content type:

  1. Enable translation for Article and "Show language selector on create and edit pages" at /admin/structure/types/manage/article
  2. Add a new field to Reference to Content, called "Card list" at /admin/structure/types/manage/article/fields/add-field and reference Articles
  3. Manage the form display at /admin/structure/types/manage/article/form-display and select "Inline Entity Form - Complex" for "Card list", then "Allow users to add existing nodes"

Content:

  1. Create "Finnish article to reference" at /node/add/article and save as Finnish
  2. Create "English article that references a Finnish one" at /node/add/article, add the existing Finnish node and save as English

To reproduce:

  1. Open "English article that references a Finnish one"
  2. Click the Translate tab
  3. Click "Add" next to German

Expected result:

Form opens to edit and save translation

Actual result:

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

InvalidArgumentException: Invalid translation language (en) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 783 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
Drupal\inline_entity_form\TranslationHelper::prepareEntity(Object, Object) (Line: 338)
Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormBase->prepareFormState(Object, Object, 1) (Line: 215)
Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex->formElement(Object, 0, Array, Array, Object) (Line: 322)
Drupal\Core\Field\WidgetBase->formSingleElement(Object, 0, Array, Array, Object) (Line: 85)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object, NULL) (Line: 532)
Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormBase->form(Object, Array, Object) (Line: 168)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 122)
Drupal\Core\Entity\ContentEntityForm->form(Array, Object) (Line: 98)
Drupal\node\NodeForm->form(Array, Object) (Line: 115)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 514)
Drupal\Core\Form\FormBuilder->retrieveForm('node_article_form', Object) (Line: 271)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'default', Array) (Line: 350)
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: 574)
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}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
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: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
02:28:25 drupal 8.3.7 ✘

The backtrace points the finger at inline_entity_form.

Additional notes:

  1. Change the Card list to use Autocomplete instead of IEF at /admin/structure/types/manage/article/form-display
  2. You can now translate to German.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hugovk created an issue. See original summary.

jsdix’s picture

I'm working on a project where we are also running into this when translating content that use entity_inline_form elements. It appears that if the referenced entity does not have a translation matching the source language of the parent, that is when the fatal error is thrown.

      $source = $form_state->get(['content_translation', 'source']);
      $source_langcode = $source ? $source->getId() : $entity_langcode;
      $source_translation = $entity->getTranslation($source_langcode);

should probably be

      $source = $form_state->get(['content_translation', 'source']);
      $source_langcode = $source ? $source->getId() : $entity_langcode;
      if (!$entity->hasTranslation($source_langcode)) {
        $entity->addTranslation($source_langcode, $entity->toArray());
      }
      $source_translation = $entity->getTranslation($source_langcode);

I've created a patch which solves the issue.

jsdix’s picture

jsdix’s picture

Version: 8.x-1.0-beta1 » 8.x-1.x-dev
vasyl.kletsko’s picture

Status: Active » Reviewed & tested by the community

Thanks jsdix for providing a patch, it fixes this bug.

  • bojanz committed 04dfa12 on 8.x-1.x authored by jsdix
    Issue #2908780 by jsdix: InvalidArgumentException: Cannot translate if...
bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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