Problem/Motivation

The following error was first reported in the Drupal CMS issue queue where it was determined that it is most likely a Drupal core issue.

Drupal\Core\Entity\EntityMalformedException: The "node" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\EntityBase->toUrl() (line 177 of core/lib/Drupal/Core/Entity/EntityBase.php).

Steps to reproduce

  1. in a fresh Drupal Cms instance, enable core’s 4 Multilingual modules (config_translation, content_translation, locale, language)
  2. add a second language ( e.g. fr )
  3. enable content translation on ‘Utility page’
  4. create a new Utility page **Test** node and add a translation (e.g. node id 2)
  5. visit e.g. /fr/node/2, click the **Duplicate** local task…
  6. observe EntityMalformedException: _The "node" entity cannot have a URI as it does not have an ID._

Proposed resolution

It looks like this error is occurring in ContentTranslationHandler:370

// Replace the delete button with the delete translation one.
if (!$new_translation) {
  $weight = 100;
  foreach (['delete', 'submit'] as $key) {
    if (isset($form['actions'][$key]['weight'])) {
      $weight = $form['actions'][$key]['weight'];
      break;
    }
  }
  …

Preliminary diagnosis:

Why isNewTranslation() doesn’t catch this: TRANSLATION_CREATED status is set only by ContentTranslationHandler::prepareTranslation() on the dedicated translation-add route. An entity produced by createDuplicate() carries TRANSLATION_EXISTING status on every translation (because clone $this deep-copies the $translations array), so isNewTranslation() returns FALSE on the active langcode even though the entity has never been saved.

Preliminary / suggested fix:

if (!$new_translation && !$entity->isNew()) {…}

The new condition !$entity->isNew() appears to fix the issue triggered by ECA in a Drupal 11.3.10 project.

p.s. It was not tested in Drupal CMS. I can provide a patch or create a MR is required.

Issue fork drupal-3593561

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

bisonbleu created an issue. See original summary.

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

jurgenhaas’s picture

Status: Active » Needs review

I've made that simple change suggested by @bisonbleu and added a test for it. Disclosure: I've used AI to help me scaffold the test and later fix the various missing pieces, but I went through all of it in detail to make sure it makes sense.

bisonbleu’s picture

Status: Needs review » Active

Thanks @jurgenhaas!

  • Patch from MR's diff applies cleanly.
  • The error is gone when duplicating a node from the /fr UI.
  • The updated ContentTranslationHandlerTest runs successfully: OK (17 tests, 105 assertions)
bisonbleu’s picture

Status: Active » Needs review

I hate it when the status changes on its own… Setting back to needs review for more eyes…

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative, +Needs Review Queue Initiative

Can run the test-only run but did locally

1) Drupal\Tests\content_translation\Kernel\ContentTranslationHandlerTest::testEntityFormAlterDuplicateTranslation
Failed asserting that an array does not have the key 'delete_translation'.

/var/www/html/core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php:192

Change itself seems minimal checking if new(). Appears to have not broken anything so will go ahead and mark for ya.

godotislate’s picture

Not completely sure it makes sense to me that the translations for an entity that is new are not also new. I wonder if the change instead should be in ContentEntityBase::isNewTranslation(). Something like:

  public function isNewTranslation() {
    return $this->isNew() || $this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_CREATED;
  }

Tagging for subsystem maintainer review.

catch’s picture

Status: Reviewed & tested by the community » Needs review

Yes I would expect translations on a new entity to be counted as new, moving back to needs review for #8.

godotislate’s picture

Maybe a better idea than the suggested code in #8 is to reset the status for every item in the $translations array for the duplicate created by ContentEntityBase::createDuplicate(). Something like:

      $duplicate = clone $this;
      foreach ($duplicate->translations as &$translation) {
        $translation['status'] = static::TRANSLATION_CREATED;
      }

Removing Needs subsystem maintainer tag since @catch commented in 9, and while this is marked as content_translation, this could be considered an Entity API issue.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.14 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.