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.
By mishoboss on
Hi, is there a module that automatically translates to all active languages a new node on submit? I don't want actually to translate the content, just to create node duplicates to all languages. I mean to automatically create duplicate nodes, and to store in their "language" field the language they are and in "tnid" field the "nid" of the just created node.
This way the "Translate" button is useless and the content could change via the "Edit" button.
Comments
Anyone?
Anyone?
I assume this is not possible
I assume this is not possible since there is no answer... :(
Anyone any idea?
I wrote an extension to Rules
I wrote an extension to Rules module to handle automatic node translation on new node creation. I post the code modifications bellow:
Edit the modules\rules\rules\modules\node.rules.inc
Find and replace the "node_rules_event_info" function with this:
----------------------
function node_rules_event_info() {
$items = array ('node_insert' => array ('label' => t ( 'After saving new content' ), 'module' => 'Node', 'arguments' => rules_events_node_arguments ( t ( 'created content' ), t ( "content's author" ) ), 'redirect' => TRUE ), 'node_update' => array ('label' => t ( 'After updating existing content' ), 'module' => 'Node', 'arguments' => rules_events_node_arguments ( t ( 'updated content' ), t ( "content's author" ), TRUE ), 'redirect' => TRUE ), 'node_presave' => array ('label' => t ( 'Content is going to be saved' ), 'module' => 'Node', 'arguments' => rules_events_node_arguments ( t ( 'saved content' ), t ( "content's author" ), TRUE ), 'redirect' => TRUE ), 'node_view' => array ('label' => t ( 'Content is going to be viewed' ), 'module' => 'Node', 'help' => t ( "Note that if drupal's page cache is enabled, this event won't be generated for pages served from cache." ), 'arguments' => rules_events_node_arguments ( t ( 'viewed content' ), t ( 'content author' ) ) + array ('teaser' => array ('type' => 'boolean', 'label' => t ( 'Content is displayed as teaser' ) ), 'page' => array ('type' => 'boolean', 'label' => t ( 'Content is displayed as page' ) ) ) ), 'node_delete' => array ('label' => t ( 'After deleting content' ), 'module' => 'Node', 'arguments' => rules_events_node_arguments ( t ( 'deleted content' ), t ( 'content author' ) ), 'redirect' => TRUE ) );
// Specify that on presave the node is saved anyway.
$items ['node_presave'] ['arguments'] ['node'] ['saved'] = TRUE;
return $items;
}
---------------------
---------------------
Add the following 2 functions:
function rules_action_translate_node($node) {
if (! $node->tnid) {
$node->language = i18n_get_lang ();
$node->tnid = $node->nid;
$lang_arr = array_keys ( i18n_node_language_list ( $node, true ) );
for($i = 1; $i < sizeof ( $lang_arr ); $i ++) {
if ($lang_arr [$i] != $node->language) {
addNewNode ( $lang_arr [$i], $node );
}
}
return array ('node' => $node );
}
}
function addNewNode($lang, $node) {
$edit = array ( );
$edit ['type'] = $node->type;
$edit ['uid'] = $node->uid;
$edit ['name'] = $node->name;
$edit ['promote'] = $node->promote;
$edit ['comment'] = $node->comment;
$edit ['revision'] = $node->revision;
$edit ['status'] = 1;
$edit ['title'] = $node->title;
$edit ['body'] = $node->body;
$edit ['language'] = $lang;
$edit ['tnid'] = $node->nid;
$new_node = node_submit ( $edit );
node_save ( $new_node );
}
-------------
That's it. Now in the Actions you will find a new position "Translate node". It must be added on "After saving new content" event. I don't think this is the right way of extending the Rules module, but I don't find a better one. Please test it and post feedback :)
Action does not appear
Hi, I have added the extension to Rules, but action doesn't appear at list.
What I have make wrong?
Any solution?
Thanks and sorry for my english
if you replaced the
Ahh, sorry I posted wrong function in my hurry... sorry again! The right function is node_rules_action_info() and it must be replaced with this:
function node_rules_action_info() {
return array ('rules_action_node_set_author' => array ('label' => t ( 'Set the content author' ), 'arguments' => array ('node' => array ('type' => 'node', 'label' => t ( 'Content' ) ), 'author' => array ('type' => 'user', 'label' => t ( 'User, who is set as author' ) ) ), 'module' => 'Node' ), 'rules_action_set_node_title' => array ('label' => t ( 'Set content title' ), 'arguments' => array ('node' => array ('type' => 'node', 'label' => t ( 'Content' ) ), 'title' => array ('type' => 'string', 'label' => t ( 'Title' ) ) ), 'module' => 'Node' ), 'rules_action_translate_node' => array ('label' => t ( 'Translate node' ), 'arguments' => array ('node' => array ('type' => 'node', 'label' => t ( 'Content' ) ) ), 'module' => 'Node' ), 'rules_action_add_node' => array ('label' => t ( 'Add new content' ), 'arguments' => array ('author' => array ('type' => 'user', 'label' => t ( 'User, who is set as author' ) ), 'title' => array ('type' => 'string', 'label' => t ( 'Title' ), 'description' => t ( 'The title of the newly created content.' ) ) ), 'new variables' => array ('node_added' => array ('type' => 'node', 'label' => t ( 'New content' ), 'save' => TRUE, 'label callback' => 'rules_action_add_node_variable_label' ) ), 'module' => 'Node' ), 'rules_action_load_node' => array ('label' => t ( 'Load content by id' ), 'arguments' => array ('nid' => array ('type' => 'number', 'label' => t ( 'Content ID' ) ), 'vid' => array ('type' => 'number', 'label' => t ( 'Content Revision ID' ), 'description' => t ( "If you want to load a specific revision, specify it's revision id. Else leave it empty to load the current revision." ), 'required' => FALSE ) ), 'new variables' => array ('node_loaded' => array ('type' => 'node', 'label' => t ( 'Loaded content' ), 'label callback' => 'rules_action_load_node_variable_label' ) ), 'module' => 'Node' ) );
}
subscribing
subscribing
sub
sub
sub
sub
Using hook_rules_action_info
A better way to add your own action is to implement hook_rules_action_info(). Best practice is to add your hook implementation to a mymodule.rules.inc file in your custom modules folder. Here's my version of the hook
You can now create a new rule to be triggered by the After saving new content event. Your new action should be available for selection.
I made a quick stab at using i18n_synch to copy cck fields to the new node but it didn't work and I don't have time to look into it further. If anyone figures it out please share the solution. Be careful with recursive rules and remember to turn debugging on in Rules settings, so you can see what rules are being triggered.
Edit - just edited the source above to check if the new node is a source node. If it's not then don't create automatic translations of it.
Peacog Web Development
Thanks for the code snippet.
Thanks for the code snippet. It's just perfect!
Automatic url aliases
I've recently come across a problem with automatic url aliases and the above rule. If you use Pathauto to automatically create urls, and your Pathauto Update action setting is set to Do nothing. Leave the old alias intact, the url alias for the source node does not get set correctly. The aliases for the translated nodes are OK.
The solution is to install the Path Redirect module and change Update action to Create a new alias. Redirect from old alias. In any case, this is the recommended update action for SEO purposes.
Peacog Web Development
Module
I'm in the process of bundling this in to a proper module that includes a draft in the new language created using Google Translate. Will report back! Thanks for the head start... =)
--
https://www.codeenigma.com/
Done
Was pretty easy, actually:
http://drupal.org/project/i18n_auto_draft=)Edit: Updating link to module: http://drupal.org/project/i18n_auto
--
https://www.codeenigma.com/
Ported to D7
Thanks for this hidden gift. I've been working on this for a while and this simple rule really hit the spot.
Bobby Dean
Thanks a lot for this
Thanks a lot for this. It's been very usefull.
I was also trying to create menu items automatically, but i'm new to Drupal and found myself a bit stuck. I'm trying to use the "menu_save_link" function.
Would you have any idea of how i should proceed?
Thank you
be cautious with this code, i
be cautious with this code, i debugged around.
if (!$node->translation_source) {... this wa spossible in d6, in d7 that variable translation_source is not inside the node object
when running this rule with i18n sync enabled on the content type you get a lot of duplicate nodes like 5 times the same node in french
Thanks for this snippet. I
Thanks for this snippet. I added a "Content is translation source" condition and the capability of setting the translation set.
Drupal 7 version of this snippet
As the hooks of Rules module have changed in D7 (e.g., see http://drupalcontrib.org/api/drupal/contributions!rules!rules.api.php/fu...), I made some changes to the code snippets above to make it work.
How to use it (for newbies)
I just found this comment through Google... I would need this very same feature! Though I'm a kind of newbie in Drupal, am not much into the module creation... Where should I copy your code? Thanks for sharing!
subscribe
+1
doesnt work
i have no custom action to use
Instead of doing it with
Instead of doing it with Rules. You can use hook_entity_insert(). This will trigger you function when a node is inserted and you can limited by the content type.
- Darryl Norris
Be Connected: Website | Twitter | LinkendIn | GitHub
that's neat
unfortunately i'm not that good in scripting
could you write down an example for the community?
<3