Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/modules/translation/tests/translation.api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @file
* Hooks for translation module.
*/

/**
* Respond to changes in a node's translation set.
*
* The node object is extended with a 'translation_change' array containing
* both the old and new tnid.
*
* @param Node $node
* The node being acted upon.
*/
function hook_node_translation_change(Node $node) {
$update = db_update('my_table')
->fields(array(
'tnid' => $node->translation_change['new_tnid'],
))
->condition('tnid', $node->tnid, '=')
->execute();
}
24 changes: 22 additions & 2 deletions core/modules/translation/tests/translation.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,23 @@ class TranslationTestCase extends BackdropWebTestCase {
parent::setUp('node', 'language', 'locale', 'translation', 'translation_test');

// Setup users.
$this->admin_user = $this->backdropCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content'));
$this->translator = $this->backdropCreateUser(array('create page content', 'edit own page content', 'translate content'));
$this->admin_user = $this->backdropCreateUser(
array(
'bypass node access',
'administer nodes',
'administer languages',
'administer content types',
'administer blocks',
'access administration pages',
'translate content',
));
$this->translator = $this->backdropCreateUser(
array(
'create page content',
'edit own page content',
'delete own page content',
'translate content',
));

$this->backdropLogin($this->admin_user);

Expand Down Expand Up @@ -126,6 +141,11 @@ class TranslationTestCase extends BackdropWebTestCase {
$this->backdropPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node_translation_title)), 'Translated node updated.');

// Delete the source node.
$this->backdropPost('node/' . $node->nid . '/delete', array(), t('Delete'));
// Assert that hook_translation_change() was called.
$this->assertEqual(state_get('translation_change_called', ''), $node->nid, t('hook_node_translation_change() was called.'));

// Confirm that disabled languages are an option for translators when
// creating nodes.
$this->backdropGet('node/add/page');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@
function translation_test_node_insert(Node $node) {
backdrop_write_record('node', $node, 'nid');
}

/**
* Implementation of hook_node_translation_change().
*/
function translation_test_node_translation_change($node) {
state_set('translation_change_called', $node->nid);
}
17 changes: 17 additions & 0 deletions core/modules/translation/translation.module
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,22 @@ function translation_remove_from_set($node) {

// Determine which nodes to apply the update to.
$set_nids = db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchCol();

$node->translation_change = array(
'old_tnid' => $node->tnid,
'new_tnid' => 0,
// Determine the remaining former member of the translation set.
// May be needed e.g. to reassign existing data from the tnid to this nid.
'remaining_nid' => $set_nids,
);

if (count($set_nids) == 1) {
// There is only one node left in the set: remove the set altogether.
$query
->condition('tnid', $node->tnid)
->execute();
// Allow other modules to respond to the removal of this translation set.
node_invoke($node, 'translation_change');

$flush_set = TRUE;
}
Expand All @@ -502,10 +513,16 @@ function translation_remove_from_set($node) {
// we pick a new source - preferably one that is up to date.
if ($node->tnid == $node->nid) {
$new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField();
$node->translation_change = array(
'old_tnid' => $node->tnid,
'new_tnid' => $new_tnid,
);
db_update('node')
->fields(array('tnid' => $new_tnid))
->condition('tnid', $node->tnid)
->execute();
// Allow other modules to respond to the removal of this translation set.
node_invoke($node, 'translation_change');

$flush_set = TRUE;
}
Expand Down
Loading