We've had at least 2 occasions where we needed to intervene within this function (once for ERS, and also for UUID). Would it make sense to add a hook here? That clone operation has also caused problems, because not all attributes should be automatically inherited from the source node to the translated one.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Yes, adding a hook sounds fine, patch? :) Remember to also add a .api.php file and documentation for that hook inside it, see the current entity source overview patch for an example.

ethanethan’s picture

Ok, i'll give it a shot!

ethanethan’s picture

Similar to the TMGMT checkout hooks, should i add one at the beginning of saveTranslation and then right before update_node_translation? Not sure how to do this, since i need to specify a different $tnode, but also need to get in the if (empty($node->tnid)) block.

$data = array('job_item'=>$job_item);
drupal_alter('tmgmt_before_update_node_translation', $tnode, $node, $data);
Berdir’s picture

One hook sounds good enough for me for the moment. $tnode is by reference, so you could completely replace the object if you want to. If there is only one thing in $data, you can just as well pass $job_item directly as the third argument.

ethanethan’s picture

(i had two actually, just removed $translation since the results are cached and i can run it again!)

Yup i'm replacing $tnode for sure. The issue is with the node_save on the source node (in the empty($node->tnid) block). I need to set something for ERS at that point, and after tnid is set, i can't seem to filter for it anymore.

ethanethan’s picture

Assigned: Unassigned » ethanethan
FileSize
1.96 KB

Ok, here's the hook and the documentation. Let me know what you think.

ethanethan’s picture

Status: Active » Needs review
Berdir’s picture

Status: Needs review » Needs work

The hook looks ok, some coding style issues to be fixed:

+++ b/sources/node/tmgmt_node.plugin.incundefined
@@ -48,6 +48,9 @@ class TMGMTNodeSourcePluginController extends TMGMTDefaultSourcePluginController
+      // Allow modules and translator plugins to alter, for example in the case of creating
+      // revisions for translated nodes, or altering properties of the tnode before saving.

Make sure that comments aren't longer than 80 characters, most editors provide a setting to show where the 80 character line is. In this case, exactly both last words are too long.

+++ b/sites/all/modules/contrib/tmgmt/sources/node/tmgmt_node.api.phpundefined
@@ -0,0 +1,25 @@
+/**
+ * @param object $tnode translated node
+ * @param object $node source node
+ * @param TMGMTJobItem $job_item

First line should be a short description of what the hook allow to do

"Alter the created node translation."

The description of a @param should be on the next line, intended by 2 spaces more than @param. Also, the comment should start with an uppercase character and end with a ".".

+++ b/sites/all/modules/contrib/tmgmt/sources/node/tmgmt_node.api.phpundefined
@@ -0,0 +1,25 @@
+  // Exmample implementation of hook to create revisions of translations
+  $translations = translation_node_get_translations($node->tnid);
+  $job = $job_item->getJob();
+  $revisions_enabled = true;
+  if (isset($translations[$job->target_language]) && $revisions_enabled){
+    $existing_tnode = node_load($translations[$job->target_language]->nid);
+    $tnode = clone $existing_tnode;
+    // This signals to node_save to create a new revision.
+    $tnode->revision = 1;
+    $tnode->translation_source = $node;

Typo ("Exmample").

I don't understand what your example is doing, it completely replaces our generated node object without adding the new translations? That makes no sense.

ethanethan’s picture

For the example, let me try to explain! The original implementation in TMGMT just loads an existing translation:
$tnode = node_load($translations[$job->target_language]->nid);

and then resaves the data within this one. We needed a way to revision translations, so i cloned the existing one instead (similar to how TMGMT does it for a new translation) and set its revision property. The hook comes before the actual saving, so yes i replace the target tnode. Is there a better way to do this? Or maybe i should just change the example?

Berdir’s picture

Yes, I understand what you are trying to do. But at least the example code is missing the most important part... assigning the new translations. All that the code is doing is saving the original, previous translation as a new revision, completely ignoring the new translation. Then there's that useless clone, you could just overwrite the original variable initially.

Let's go for a very simple example instead, I suggest just something like this:

// Always store new translations as a new revision.
$tnode->revision = 1;
ethanethan’s picture

Oh. Well thats a much simpler way to do the same thing, isnt it.

seanB’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
1.41 KB

I updated the patch in #6 to work with the latest version of the module and processed the comments. Could you review this?

Berdir’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Status: Needs review » Patch (to be ported)

Works for me. Couldn't apply the patch with git, it didn't understand that tmgmt_node.api.php is a new file, but patch -p1 worked.

Thanks!

8.x-1.x doesn't have tmgmt_node, but the same hook in the content translation module would make sense as well.

  • Berdir committed a8923b5 on 7.x-1.x authored by seanB
    Issue #1673904 by ethanethan, seanB: alter hook request in source...
gaurav_manerkar’s picture

Hi,

Is there any plan to rollout the patch for 8x version ?