Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I am creating/updating a node programmatically, i.e., with node_save($node);
What is the best practice for creating translations for an entity completely programmatically?
Comments
Comment #1
plachSee lines 367-375 of entity_translation.admin.inc:
The call to field_attach_update is not necessary if you call node_save after setting the translation.
Comment #2
plachI guess this is fixed.
Comment #4
pcambraHere's what I did for adding translations to taxonomy terms programatically, just in case someone finds it useful
Comment #5
Toxid CreditAttribution: Toxid commentedThought I'd share my code for translating nodes as well, since I couldn't find a way to do it while creating a node, before calling node_save.
If it's possible to add a translation while creating a node, before calling node_save, please let me know.
Comment #6
nikosnikos CreditAttribution: nikosnikos commentedThanks plach, pcambra and Toxid it saved me hours !
Here's how I did it :
Comment #7
smartango CreditAttribution: smartango commenteddoes not work anymore.
I want to add a translation to a node, it just does nothing
this
print all languages defined for all translation, but in the ui it is not present the translation (translate tab says 'add')
version: 7.x-1.0-beta1
Comment #8
plachIs the
$node->translations
key defined and not empty?Comment #9
smartango CreditAttribution: smartango commentedthis:
translations after . stdClass Object
(
[original] => en
[data] => Array
(
[en] => Array
(
[entity_type] => node
[entity_id] => 7
[language] => en
[source] =>
[uid] => 1
[status] => 1
[translate] => 0
[created] => 1348749201
[changed] => 1352392099
)
)
)
is the output of drupal_set_message('translations after . '.print_r($this->node->translations,TRUE)); (printed before and after setTranslation have the same content)
also I want to update original value, I am using:
$this->node->title_field[$language][0]['value'] = $title;
print_r($values):
values Array
(
[title_field] => Array
(
[it] => Array
(
[0] => Array
(
[value] => Prodotto con variantis
)
)
)
)
Comment #10
plachI am sorry, I don't know what troubles you are facing, but the way described in #6 should be the most correct.
Comment #12
mike.coenen CreditAttribution: mike.coenen commentedSorry to open this up again.
The suggestion of number 6 (nikosnikos) works great. The only thing that does not seem to work is the url alias not being created for the added translations. I have tried to debug the handler that gets returned but the only thing i can find is that it does set the pathauto = TRUE value in the node.
Anyone have an idea to get the aliases working? (It does work when creating a translation through the interface )
Comment #13
plachDid you try setting
pathauto = true
?Comment #14
mkalkbrennerJust an important hint for everyone who tries add a translation to an entity programmatically.
The way described in #6 works if you create a new node.
If you want to add a translation to an already existing node you will run into the issue described in #7. In that case you have to tell the handler to replace the cached entity with the updated one by setting the optional third parameter to TRUE:
Comment #15
Smos CreditAttribution: Smos commentedI do it a little different:
But I don't know how to change the title and body field of the translation without overwriting the original node. Does anybody know how?
Comment #16
torpy CreditAttribution: torpy commentedFor the title field at least, you need to have the title module installed: http://drupal.org/project/title
Comment #17
calculus CreditAttribution: calculus commentedI face a similar problem to comment #12.
I'm trying to create translations of body field when a user creates a node. I'm using hook_field_attach_presave.
Translation is being created (in edit form i have target-language's tab), url alias is being created for original content but not for translations. In fact if i edit translations i realize that path['pathauto'] is false.
Everything works right if i manually add a translation from UI. Generate automatic URL alias is ticked in edit translation form.
Do i miss something?
Comment #18
mvdve CreditAttribution: mvdve commentedSame issue here (as #12 and #17). Pathauto is disabled on the second language.
In my case I import taxonomy terms with the migrate module.
After enabling pathauto everything works perfect.
The following code only enables the main entity. Pathauto is still disabled on the other languages.
When the entity function "Hide shared elements on translation forms" is enabled, the pathauto field is show on the translation.
It looks like this field is not shared between the two versions.
Is there a way to enable pathauto or to fix the "share" issue?
Comment #19
mvdve CreditAttribution: mvdve commentedUpdated the code to be more simple and reliable. The pathauto update function activates the translated alias:
For the time being, I created an work around:
Hopefully this helps people with the same problem.
Comment #20
plachTentatively marking fixed again.
Comment #22
SebCorbin CreditAttribution: SebCorbin commentedThanks to you guys I made an MigrateDestination that set a translate_copy property.
Later in hook_node_insert(), it automatically creates translations for each language activated using default values from the original language.
Comment #23
dwb17 CreditAttribution: dwb17 commentedAfter googling for quite a while, and trying to find a working example of how to programmatically create a multilingual entity, I ended up at this post.
It seems that this is the best documentation around currently, so I though it would help others by adding an example code snippet that utilized Entity Metadata Wrapper(entity_metadata_wrapper) and Entity translation.
Comment #24
gambryTo properly create the pathauto alias, the only cross-entitytype solution I found so far is:
Where $langcode is your additional language code
$entity
is your entity and$entity_type
is taxonomy_term, node, user, etc.Comment #25
bendev CreditAttribution: bendev at WebstanZ commented#24 works fine thanks
it can be interesting to note that it should be run (for instance) @hook_node_insert because hook_entity_presave is too ealry and the alias would get deleted afterwards.