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 prabhakarsun on
I am trying to create a module to automatically translate nodes on save using Google translate API. Here is the code.
<?php
/**
* Implementation of hook_node_update()
*/
function autotranslate_node_update($node)
{
if ($node->language === 'en') {
autotranslate_add_translation($node);
}
}
/**
* Implementation of hook_node_insert()
*/
function autotranslate_node_insert($node)
{
if ($node->language === 'en') {
autotranslate_add_translation($node);
}
}
/**
* Callback for hook_node_insert & hook_node_update
*/
function autotranslate_add_translation($node)
{
$translations = array_keys(translation_node_get_translations($node->nid));
$enabled_languages = language_list('enabled');
foreach ($enabled_languages[1] as $language => $object) {
//Translate only if it is not the current language & there are no translations defined already for this language.
if ((!in_array($language, $translations)) && ($language != $node->language)) {
autotranslate_add_translated_node($node, substr($language, 0, 2));
//dsm(substr($language, 0, 2));
}
}
}
/**
* Create a translation for a node
*/
function autotranslate_add_translated_node($node, $language)
{
$new_node = clone $node;
//Translate
$new_node->title = autotranslate_translate($node->title, $node->language, $language);
$new_node->body = array(
'und' => array(
array(
'value' => autotranslate_translate($node->body['und'][0]['value'], $node->language, $language),
'format' => 'full_html'
)
)
);
$new_node->nid = NULL;
$new_node->vid = NULL;
if (isset($node->nid)) {
$new_node->tnid = $node->nid;
} else {
dsm($node);
}
//Change the language
$new_node->language = $language;
node_save($new_node);
unset($node);
unset($new_node);
}
/**
* Translate content using Google Translate API
*/
function autotranslate_translate($content, $from, $to)
{
$json = json_decode(file_get_contents(TRANSLATE_URI . 'key=' . TRANSLATE_API_KEY . '&q=' . urlencode($content) . '&source=' . $from . '&target=' . $to), true);
return $json['data']['translations'][0]['translatedText'];
}
?>
But I am getting following error:
<?php
DOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'entity_id' cannot be null: INSERT INTO {field_data_body} (entity_type, entity_id, revision_id, bundle, delta, language, body_value, body_summary, body_format) 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] => [:db_insert_placeholder_2] => [:db_insert_placeholder_3] => website [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => <p style=";text-align:right;direction:rtl"> اختبار الجسم ترجمة قيمة! </p> [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => full_html ) in field_sql_storage_field_storage_write() (line 448 of /Path-to-drupal/modules/field/modules/field_sql_storage/field_sql_storage.module).
?>
Comments
Instead of $new_node->nid
Instead of
you could try
and you should not do
unset($node);
since it is a reference and other code besides your own may still need it.http://drupal.stackexchange.c
http://drupal.stackexchange.com/questions/50360/how-to-programmatically-...