ContentTranslationController::add() breaks if the latest translation affected revision id is NULL

Created on 12 May 2020, over 4 years ago
Updated 2 February 2023, almost 2 years ago

Dear Drupal Core maintainers,

I'm facing an issue when adding a new translation for a published content. This content has no revisions, they have been deleted by a contrib module node_revision_delete .

This issue is not related to node_revision_delete but it's due to a weakness when resolving the latest translation affected revision identifier in add method of ContentTranslationController:

  public function add(LanguageInterface $source, LanguageInterface $target, RouteMatchInterface $route_match, $entity_type_id = NULL) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $route_match->getParameter($entity_type_id);

    // In case of a pending revision, make sure we load the latest
    // translation-affecting revision for the source language, otherwise the
    // initial form values may not be up-to-date.
    if (!$entity->isDefaultRevision() && ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle())) {
      /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager()->getStorage($entity->getEntityTypeId());
      $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $source->getId());
      if ($revision_id != $entity->getRevisionId()) {
        $entity = $storage->loadRevision($revision_id);
      }
    }

    // @todo Exploit the upcoming hook_entity_prepare() when available.
    // See https://www.drupal.org/node/1810394.
    $this->prepareTranslation($entity, $source, $target);

The getLatestTranslationAffectedRevisionId method signature is :

  /**
   * Returns the latest revision affecting the specified translation.
   *
   * @param int|string $entity_id
   *   The entity identifier.
   * @param string $langcode
   *   The language code of the translation.
   *
   * @return int|string|null
   *   A revision ID or NULL if no revision affecting the specified translation
   *   could be found.
   */
  public function getLatestTranslationAffectedRevisionId($entity_id, $langcode);

So if getLatestTranslationAffectedRevisionId method returns NULL (which is the case when no revisions exist for a specific content), it ends up to a NULL $entity variable which breaks later the prepareTranslation method.

🐛 Bug report
Status

Needs work

Version

9.5

Component
Content translation 

Last updated 1 day ago

No maintainer
Created by

🇫🇷France jkuma

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Can we add steps to reproduce this to the issue summary please.

  • 🇮🇳India nikhil_110

    Re-rolled patch #12 for 9.5.x.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India nikhil_110

    Steps to test-
    1. Go to site.
    2. Go to admin/modules
    3. In the multilingual field , enable the modules.
    4. Go to /admin/config/regional/language
    5. Add a language - Chinese, Simplified
    6. Go to admin/config/regional/content-language
    7. Under Custom language settings select Content
    8. Enable the translation and Save configuration.
    9. Go to /admin/content
    10. Click on Add content
    11. Select an Article.
    12. Create an Article in English language.
    13. Now create a translation in Chinese, Simplified of the article content created in English.
    14. Verify the form.

  • 🇺🇸United States smustgrave

    #12 still applies so #23 was not needed so removing credit for that.

    Please include an interdiff with patches

    Also the steps to reproduce were copied from #6 which does not show how to reproduce the issue. Just to make translations

  • Status changed to Needs work almost 2 years ago
Production build 0.71.5 2024