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
- in a fresh Drupal Cms instance, enable core’s 4 Multilingual modules (config_translation, content_translation, locale, language)
- add a second language ( e.g. fr )
- enable content translation on ‘Utility page’
- create a new Utility page **Test** node and add a translation (e.g. node id 2)
- visit e.g. /fr/node/2, click the **Duplicate** local task…
- 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.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3593561
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
Comment #4
jurgenhaasI'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.
Comment #5
bisonbleu commentedThanks @jurgenhaas!
OK (17 tests, 105 assertions)Comment #6
bisonbleu commentedI hate it when the status changes on its own… Setting back to needs review for more eyes…
Comment #7
smustgrave commentedCan run the test-only run but did locally
Change itself seems minimal checking if new(). Appears to have not broken anything so will go ahead and mark for ya.
Comment #8
godotislateNot 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:Tagging for subsystem maintainer review.
Comment #9
catchYes I would expect translations on a new entity to be counted as new, moving back to needs review for #8.
Comment #10
godotislateMaybe a better idea than the suggested code in #8 is to reset the status for every item in the
$translationsarray for the duplicate created byContentEntityBase::createDuplicate(). Something like: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.
Comment #11
needs-review-queue-bot commentedThe 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.