entity_translation.node.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @file
  4. * The node specific translation functions and hook implementations.
  5. */
  6. /**
  7. * Identifies a content type which has translation support enabled.
  8. */
  9. define('ENTITY_TRANSLATION_ENABLED', 4);
  10. /**
  11. * Hides translation metadata.
  12. */
  13. define('ENTITY_TRANSLATION_METADATA_HIDE', 0);
  14. /**
  15. * Adds translation metadata to the original authoring information.
  16. */
  17. define('ENTITY_TRANSLATION_METADATA_SHOW', 1);
  18. /**
  19. * Replaces the original authoring information with translation metadata.
  20. */
  21. define('ENTITY_TRANSLATION_METADATA_REPLACE', 2);
  22. /**
  23. * Checks if the given entity has node translation enabled.
  24. */
  25. function entity_translation_node($entity_type, $node) {
  26. return $entity_type == 'node' && function_exists('translation_supported_type') && translation_supported_type($node->type);
  27. }
  28. /**
  29. * Node-specific menu alterations.
  30. */
  31. function entity_translation_node_menu_alter(&$items, $backup) {
  32. if (isset($backup['node'])) {
  33. $item = $backup['node'];
  34. // Preserve the menu router item defined by other modules.
  35. $callback['page callback'] = $item['page callback'];
  36. $callback['file'] = $item['file'];
  37. $callback['module'] = $item['module'];
  38. $access_arguments = array_merge(array(1, $item['access callback']), $item['access arguments']);
  39. }
  40. else {
  41. $callback = FALSE;
  42. $access_arguments = array(1);
  43. }
  44. $items['node/%node/translate']['page callback'] = 'entity_translation_overview';
  45. $items['node/%node/translate']['page arguments'] = array('node', 1, $callback);
  46. $items['node/%node/translate']['access arguments'] = $access_arguments;
  47. $items['node/%node/translate']['access callback'] = 'entity_translation_node_tab_access';
  48. $items['node/%node/translate']['file'] = 'entity_translation.admin.inc';
  49. $items['node/%node/translate']['module'] = 'entity_translation';
  50. }
  51. /**
  52. * Node specific access callback.
  53. */
  54. function entity_translation_node_tab_access() {
  55. $args = func_get_args();
  56. $node = array_shift($args);
  57. if (entity_translation_node('node', $node)) {
  58. $function = array_shift($args);
  59. return call_user_func_array($function, $args);
  60. }
  61. else {
  62. return entity_translation_tab_access('node', $node);
  63. }
  64. }
  65. /**
  66. * Returns whether the given node type has support for translations.
  67. *
  68. * @return
  69. * Boolean value.
  70. */
  71. function entity_translation_node_supported_type($type) {
  72. return variable_get('language_content_type_' . $type, 0) == ENTITY_TRANSLATION_ENABLED;
  73. }
  74. /**
  75. * Implements hook_node_view().
  76. *
  77. * Provides content language switcher links to navigate among node translations.
  78. */
  79. function entity_translation_node_view($node, $build_mode, $langcode) {
  80. if (!empty($node->translations) && drupal_multilingual() && entity_translation_node_supported_type($node->type) && !variable_get("entity_translation_hide_translation_links_{$node->type}", FALSE)) {
  81. $path = 'node/' . $node->nid;
  82. $links = EntityTranslationDefaultHandler::languageSwitchLinks($path);
  83. if (is_object($links) && !empty($links->links)) {
  84. $handler = entity_translation_get_handler('node', $node);
  85. $translations = $handler->getTranslations()->data;
  86. // Remove the link for the current language.
  87. unset($links->links[$langcode]);
  88. // Remove links to unavailable translations.
  89. foreach ($links->links as $langcode => $link) {
  90. if (!isset($translations[$langcode]) || !entity_translation_access('node', $translations[$langcode])) {
  91. unset($links->links[$langcode]);
  92. }
  93. }
  94. $node->content['links']['translation'] = array(
  95. '#theme' => 'links',
  96. '#links' => $links->links,
  97. '#attributes' => array('class' => 'links inline'),
  98. );
  99. }
  100. }
  101. }
  102. /**
  103. * Implements hook_form_FORM_ID_alter().
  104. *
  105. * Provides settings into the node content type form to choose for entity
  106. * translation metadata and comment filtering.
  107. */
  108. function entity_translation_form_node_type_form_alter(&$form, &$form_state) {
  109. if (entity_translation_enabled('node')) {
  110. $type = $form['#node_type']->type;
  111. $t_args = array('!url' => url('admin/config/regional/entity_translation'));
  112. $form['workflow']['language_content_type']['#options'][ENTITY_TRANSLATION_ENABLED] = t('Enabled, with field translation');
  113. $form['workflow']['language_content_type']['#description'] .= ' <p>' . t('If field translation is selected you can have per-field translation for each available language. You can find more options in the <a href="!url">entity translation settings</a>.', $t_args) . '</p>';
  114. // Hide settings when entity translation is disabled for this content type.
  115. $states = array(
  116. 'visible' => array(
  117. ':input[name="language_content_type"]' => array('value' => ENTITY_TRANSLATION_ENABLED),
  118. ),
  119. );
  120. $form['workflow']['entity_translation_hide_translation_links'] = array(
  121. '#type' => 'checkbox',
  122. '#default_value' => variable_get("entity_translation_hide_translation_links_$type", FALSE),
  123. '#title' => t('Hide content translation links'),
  124. '#description' => t('Hide the links to translations in content body and teasers. If you choose this option, switching language will only be available from the language switcher block.'),
  125. '#states' => $states,
  126. );
  127. $form['display']['entity_translation_node_metadata'] = array(
  128. '#type' => 'radios',
  129. '#title' => t('Translation post information'),
  130. '#description' => t('Whether the translation authoring information should be hidden, shown, or replace the node\'s authoring information.'),
  131. '#default_value' => variable_get("entity_translation_node_metadata_$type", ENTITY_TRANSLATION_METADATA_HIDE),
  132. '#options' => array(t('Hidden'), t('Shown'), t('Replacing post information')),
  133. '#states' => $states,
  134. );
  135. if (isset($form['comment'])) {
  136. $form['comment']['entity_translation_comment_filter'] = array(
  137. '#type' => 'checkbox',
  138. '#title' => t('Filter comments per language'),
  139. '#default_value' => variable_get("entity_translation_comment_filter_$type", FALSE),
  140. '#description' => t('Show only comments whose language matches content language.'),
  141. '#states' => $states,
  142. );
  143. }
  144. }
  145. }
  146. /**
  147. * Implements hook_preprocess_node().
  148. *
  149. * Alters node template variables to show/replace entity translation metadata.
  150. */
  151. function entity_translation_preprocess_node(&$variables) {
  152. $node = $variables['node'];
  153. $submitted = variable_get("node_submitted_{$node->type}", TRUE);
  154. $mode = variable_get("entity_translation_node_metadata_{$node->type}", ENTITY_TRANSLATION_METADATA_HIDE);
  155. if ($submitted && $mode != ENTITY_TRANSLATION_METADATA_HIDE) {
  156. global $language_content, $user;
  157. $handler = entity_translation_get_handler('node', $node);
  158. $translations = $handler->getTranslations();
  159. $langcode = $language_content->language;
  160. if (isset($translations->data[$langcode]) && $langcode != $translations->original) {
  161. $translation = $translations->data[$langcode];
  162. $date = format_date($translation['created']);
  163. $name = FALSE;
  164. if ($node->uid != $translation['uid']) {
  165. $account = $user->uid != $translation['uid'] ? user_load($translation['uid']) : $user;
  166. $name = theme('username', array('account' => $account));
  167. }
  168. switch ($mode) {
  169. case ENTITY_TRANSLATION_METADATA_SHOW:
  170. $variables['date'] .= ' (' . t('translated on <em>!date</em>', array('!date' => $date)) . ')';
  171. if ($name) {
  172. $variables['name'] .= ' (' . t('translated by !name', array('!name' => $name)) . ')';
  173. }
  174. break;
  175. case ENTITY_TRANSLATION_METADATA_REPLACE:
  176. $variables['date'] = $date;
  177. if ($name) {
  178. $variables['name'] = $name;
  179. }
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Returns whether the given comment type has support for translations.
  187. *
  188. * @return
  189. * Boolean value.
  190. */
  191. function entity_translation_comment_supported_type($comment_type) {
  192. $type = str_replace('comment_node_', '', $comment_type);
  193. return entity_translation_node_supported_type($type);
  194. }
  195. /**
  196. * Implements hook_query_TAG_alter().
  197. *
  198. * Filters out node comments by content language.
  199. *
  200. * @todo Find a way to track node comment statistics per language.
  201. */
  202. function entity_translation_query_comment_filter_alter(QueryAlterableInterface $query) {
  203. $node = $query->getMetaData('node');
  204. if (!empty($node->type) && variable_get("entity_translation_comment_filter_{$node->type}", FALSE)) {
  205. // Determine alias for "comment" table.
  206. $comment_alias = FALSE;
  207. foreach ($query->getTables() as $table) {
  208. if (is_string($table['table']) && $table['table'] == 'comment') {
  209. $comment_alias = $table['alias'];
  210. break;
  211. }
  212. }
  213. // Only show comments without language or matching the current content language.
  214. if ($comment_alias) {
  215. $query->condition(db_or()
  216. ->condition($comment_alias . '.language', $GLOBALS['language_content']->language)
  217. ->condition($comment_alias . '.language', LANGUAGE_NONE)
  218. );
  219. }
  220. }
  221. }
  222. /**
  223. * Implements hook_node_update_index().
  224. *
  225. * Add translated node content to search index.
  226. */
  227. function entity_translation_node_update_index($node) {
  228. $text = '';
  229. $langcodes = array_keys(language_list());
  230. $translations = entity_translation_get_handler('node', $node)->getTranslations();
  231. foreach ($langcodes as $langcode) {
  232. // Skip the default language (already indexed by search.module).
  233. if ($GLOBALS[LANGUAGE_TYPE_CONTENT]->language != $langcode && isset($translations->data[$langcode])) {
  234. // Render the node in each language.
  235. node_build_content($node, 'search_index', $langcode);
  236. $rendered = drupal_render($node->content);
  237. $text .= '<h1>' . check_plain($node->title) . '</h1>' . $rendered;
  238. }
  239. }
  240. return $text;
  241. }