Problem/Motivation

Enabled content_translation, menu_item_field and token modules.

When i used 'Provide a menu link' at original language, then goto translation, content_translation will broken.

menu_link_content: Drupal\Core\Entity\EntityStorageException: Invalid translation language (und) specified. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 847 of /mnt/www/html/mhicom01live/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Because content_translation will try to get source language when user want to translate an entity, at core/modules/content_translation/content_translation.module::content_translation_entity_presave()

but in token_node_menu_link_submit(), did not set the source language for menu translation, so content_translation get the source language was 'und', then at Drupal\content_translation\FieldTranslationSynchronizer::synchronizeFields()::177, getTranslation by und will throw error.

Steps to reproduce

- Enable content_translation, menu_item_field and token modules.
- Enable English and Japanese.
- Create a menu type, add field_image file field.
- Create an English node.
- Create a menu item by menu type above, and node reference by the node above, and setup the field_image.
- Goto node translation, add a Japanese node, save will show error.

Proposed resolution

None.

Remaining tasks

- Fix issue.
- Add test script.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork token-3168340

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

Kang Miao created an issue. See original summary.

Oscaner’s picture

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/token.module
    @@ -732,6 +733,7 @@ function token_node_menu_link_submit($entity_type, NodeInterface $node, &$form,
               if (!$entity->hasTranslation($node->language()->getId())) {
    +            _token_node_menu_link_translation_handler($entity, $node, $form_state);
                 $entity = $entity->addTranslation($node->language()->getId(), $entity->toArray());
    

    lets guard this with a module exists check for content_translation, not sure if you can get here without that, but who knows.

  2. +++ b/token.module
    @@ -772,6 +774,22 @@ function token_node_menu_link_submit($entity_type, NodeInterface $node, &$form,
     
    +/**
    + * Token node menu link translation handler
    + * @see \Drupal\content_translation\ContentTranslationHandler::entityFormEntityBuild()
    + */
    +function _token_node_menu_link_translation_handler(MenuLinkContent $entity, NodeInterface $node, FormStateInterface $form_state) {
    

    First line needs to end with a . and could maybe also improved a bit to describe what it does. Something like "Sets content translation metata"?

    Needs @param with type and description for the arguments.

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

dieterholvoet’s picture

Title: Node menu link translation will broken content_translation » Node menu link translation breaks content translation
Status: Needs work » Needs review

lets guard this with a module exists check for content_translation, not sure if you can get here without that, but who knows.

The moduleExists check already exists within the _token_node_menu_link_translation_handler function.

First line needs to end with a . and could maybe also improved a bit to describe what it does.

I addressed this feedback in the MR.