translation_helpers.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Provides methods for other modules to use with translated content.
  5. */
  6. /**
  7. * Implement hook_node_delete().
  8. *
  9. * Manages translation information for nodes.
  10. */
  11. function translation_helpers_node_delete($node) {
  12. // Only act if we are dealing with a content type supporting translations.
  13. if (translation_supported_type($node->type)) {
  14. translation_helpers_invoke_translation_change($node);
  15. }
  16. }
  17. /**
  18. * Invoke hook_node_translation_change() to allow modules to respond to a change in
  19. * source translation.
  20. *
  21. * Follows logic in translation_remove_from_set().
  22. *
  23. * Sample implementation:
  24. * <code>
  25. * function example_node_translation_change($node) {
  26. * if (isset($node->translation_change)) {
  27. * // If there is only one node remaining, track by nid rather than tnid. Otherwise, use
  28. * // the new tnid.
  29. * $new = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
  30. * db_update('example')
  31. * ->fields(array('id' => $new))
  32. * ->condition('id', $node->translation_change['old_tnid'])
  33. * ->execute();
  34. * }
  35. * }
  36. * </code>
  37. */
  38. function translation_helpers_invoke_translation_change($node) {
  39. if (isset($node->tnid)) {
  40. if (db_query('SELECT COUNT(*) FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchField() == 1) {
  41. // There is only one node left in the set.
  42. $node->translation_change = array(
  43. 'old_tnid' => $node->tnid,
  44. 'new_tnid' => 0,
  45. // Determine the remaining former member of the translation set.
  46. // May be needed e.g. to reassign existing data from the tnid to this nid.
  47. 'remaining_nid' => db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchField(),
  48. );
  49. // Allow other modules to respond to the removal of this translation set.
  50. module_invoke_all('node_translation_change', $node);
  51. }
  52. else {
  53. // If the node being removed was the source of the translation set,
  54. // we pick a new source - preferably one that is up to date.
  55. if ($node->tnid == $node->nid) {
  56. $node->translation_change = array(
  57. 'old_tnid' => $node->tnid,
  58. 'new_tnid' => db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField(),
  59. );
  60. // Allow other modules to respond to the changed source for this translation set.
  61. module_invoke_all('node_translation_change', $node);
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Implement hook_module_implements_alter().
  68. *
  69. * Set translation_helpers to act before translation module.
  70. */
  71. function translation_helpers_module_implements_alter(&$implementations, $hook) {
  72. if ($hook == 'node_delete') {
  73. // Move translation_helpers_node_delete() to the beginning of the list.
  74. $group = $implementations['translation_helpers'];
  75. unset($implementations['translation_helpers']);
  76. $implementations = array_merge(array('translation_helpers' => $group), $implementations);
  77. }
  78. }
  79. /**
  80. * Return the source translation of a node.
  81. */
  82. function translation_helpers_get_source($node) {
  83. if (isset($node->tnid) && translation_supported_type($node->type)) {
  84. // A node can be its own source.
  85. if ($node->nid == $node->tnid) {
  86. return $node;
  87. }
  88. return node_load($node->tnid);
  89. }
  90. return FALSE;
  91. }
  92. /**
  93. * Return the translation of a node in a given language.
  94. */
  95. function translation_helpers_get_translation($node, $language) {
  96. if (isset($node->tnid) && translation_supported_type($node->type)) {
  97. $translations = translation_node_get_translations($node->tnid);
  98. if (isset($translations[$language])) {
  99. return node_load($translations[$language]->nid);
  100. }
  101. }
  102. return FALSE;
  103. }