When want to create copy of node, and this node uses entity translation, I get this error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-64-de' for key 'PRIMARY': INSERT INTO {entity_translation} (entity_type, entity_id, language, source, uid, status, translate, created, changed) VALUES ( ..... )in EntityTranslationDefaultHandler->saveTranslations() (Zeile 274 von /srv/inostudio/kunden/5316.002_Selecta/online/sites/all/modules/_i18n/entity_translation/includes/translation.handler.inc).

in saveTranslations() in translation.handler.inc the entity_id gets not replaced with the new (correct) entity id; I assume because the += array concatenation, does not replace already set array-fields.

manally setting the entity id to the new id, makes the whole thing work:

$translation['entity_id'] = $this->entityId;

I guess this still needs elaboration, to actually become a patch...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plach’s picture

If you can provide a patch I'll be glad to review it :)

tarmstrong’s picture

Assigned: Unassigned » tarmstrong
tarmstrong’s picture

Just confirming this bug, with steps to reproduce.

Components:

1. Drupal core 7.x git branch
2. Entity translation 7.x-1.0-alpha1 (current HEAD)
3. Node clone git branch 7.x-1.x

Steps:

1. Add translatable field to Article content type
2. Make an article
3. Translate the field
4. Attempt to clone the node.

The node clone settings are the "save and then open for editing" option.

Result:
Same error as valderama.

tarmstrong’s picture

Ok, found the problem. In EntityTranslationDefaultHandler::saveTranslations(), I found the following code. The goal is to make sure the translation has default values. But it also fails to update the entity_id with the new entity ID (in the case of cloning a node).

In simpler terms, node_save() causes the entity translations to be cloned, but fails to set the correct ID on them.

       foreach ($translations->data as $langcode => $translation) {
        $translation += array(
           'entity_type' => $this->entityType,
           'entity_id' => $this->entityId,
           'source' => '',
           'uid' => $user->uid,
           'translate' => 0,
           'status' => 0,
           'created' => REQUEST_TIME,
           'changed' => REQUEST_TIME,
         );

I've fixed the problem by making some values override the existing value with array_merge() instead of the "+=" array operator.

tarmstrong’s picture

Status: Active » Needs review
tarmstrong’s picture

Issue tags: +montreal

Just adding the montreal tag to celebrate the DrupalCamp Montreal code sprint!

plach’s picture

Issue tags: -montreal

Nice catch! Just some minor remarks:

+++ b/includes/translation.handler.inc
@@ -250,16 +250,23 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
+        // if these aren't set in the translation already, set them

Comments should start with a capital letter and end with a dot.

+++ b/includes/translation.handler.inc
@@ -250,16 +250,23 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
+        // but these values should be overridden

See above.

+++ b/includes/translation.handler.inc
@@ -250,16 +250,23 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
+          'uid' => $user->uid,

This looks like the correct solution but the user id should ony default to the current user so I'd keep only entity type and entity in the override array.

plach’s picture

Status: Needs review » Needs work
tarmstrong’s picture

Thanks, updated the patch with your suggestions.

tarmstrong’s picture

Status: Needs work » Needs review
plach’s picture

FileSize
375.34 KB

Sorry for being a PITA, but while looking at your code I thought parts of it might be moved outside the for iteration as a microptimization. To avoid the need to reroll the patch here is one. Would you confirm it still fixes the issue?

plach’s picture

FileSize
1.78 KB

wrong one :)

tarmstrong’s picture

Haha, I was wondering why your micro-optimization had anything to do with entity_get_info()...

Looks good. I think this code is much clearer for the reader anyways, so it's not just a micro-optimization. Tested and still works.

plach’s picture

Status: Needs review » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

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

petednz’s picture

Component: Code » Base system

hi - happy to be told to open a new ticket but am getting the error described above i think
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-5926-en' for key 'PRIMARY': INSERT INTO {entity_translation} (entity_type, entity_id, language, source, uid, status, translate, created, changed) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 5926 [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 9 [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 0 [:db_insert_placeholder_7] => 1334323249 [:db_insert_placeholder_8] => 1334330752 ) inEntityTranslationDefaultHandler->saveTranslations() (line 271 of/var/www/www/sites/all/modules/entity_translation/includes/translation.handler.inc).
but then I am using alpha1
do i take it the committed patch is only on the dev version?
any plans for an updated release?

Pls’s picture

Would love to see this patch on new release, as I came up with this issue when using node_export module for D7. Thanks for the patchs guys!

plach’s picture

This is part of alpha 2.

Pls’s picture

plach, thanks. I totally missed that, anyway thanks for awesome patch!

  • Commit 6a53eb3 on master, et-permissions-1829630, factory, et-fc, revisions by plach:
    Issue #1230858 by tarmstrong, plach | valderama: Fixed Compatibility...

  • Commit 6a53eb3 on master, et-permissions-1829630, factory, et-fc, revisions, workbench by plach:
    Issue #1230858 by tarmstrong, plach | valderama: Fixed Compatibility...