i18n_node.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization (i18n) module - Node type handling
  5. *
  6. * The i18n strings created by this module are:
  7. * - node:type:[type]:[name,title,body,help]
  8. */
  9. /**
  10. * Implements hook_field_extra_fields().
  11. */
  12. function i18n_node_field_extra_fields() {
  13. $return = array();
  14. $info = entity_get_info('node');
  15. foreach (array_keys($info['bundles']) as $bundle) {
  16. if (i18n_node_type_enabled($bundle)) {
  17. $return['node'][$bundle] = i18n_language_field_extra();
  18. }
  19. }
  20. return $return;
  21. }
  22. /**
  23. * Implements hook_menu().
  24. */
  25. function i18n_node_menu() {
  26. $items['admin/config/regional/i18n/node'] = array(
  27. 'title' => 'Node options',
  28. 'description' => 'Configure extended options for multilingual content and translations.',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('variable_group_form', 'i18n_node'),
  31. 'access arguments' => array('administer site configuration'),
  32. 'type' => MENU_LOCAL_TASK,
  33. 'weight' => 10,
  34. );
  35. $items['i18n/node/autocomplete'] = array(
  36. 'page callback' => 'i18n_node_autocomplete',
  37. 'file' => 'i18n_node.pages.inc',
  38. 'access arguments' => array('administer content translations'),
  39. 'type' => MENU_CALLBACK,
  40. );
  41. return $items;
  42. }
  43. /**
  44. * Implements hook_block_view_MODULE_DELTA_alter().
  45. *
  46. * Set translated help text for node/add pages, replace node_help() text.
  47. */
  48. function i18n_node_block_view_system_help_alter(&$block) {
  49. $arg = drupal_help_arg(arg(NULL));
  50. if ($arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) {
  51. if (($type = node_type_get_type(str_replace('-', '_', $arg[2]))) && !empty($type->help)) {
  52. $help = i18n_node_translate_type($type, 'help');
  53. if ($help !== $type->help) {
  54. $block['content'] = str_replace(filter_xss_admin($type->help), filter_xss_admin($help), $block['content']);
  55. }
  56. }
  57. }
  58. elseif ($arg[0] == 'node' && $arg[2] == 'edit') {
  59. $node = menu_get_object();
  60. if ($node && isset($node->type)) {
  61. $type = node_type_get_type($node->type);
  62. $help = i18n_node_translate_type($type, 'help');
  63. if ($help !== $type->help) {
  64. $block['content'] = str_replace(filter_xss_admin($type->help), filter_xss_admin($help), $block['content']);
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Implements hook_help().
  71. */
  72. function i18n_node_help($path, $arg) {
  73. switch ($path) {
  74. case 'admin/help#i18n_node':
  75. $output = '<p>' . t('Provides some extended multilingual options for nodes.') . '</p>';
  76. $output .= '<p>' . t('Additionally, if <em>String translation</em> enabled, this module will localize all content type configuration texts.') . '</p>';
  77. $output .= '<ul>';
  78. $output .= '<li>' . t('Content type names') . '</li>';
  79. $output .= '<li>' . t('Submission guidelines') . '</li>';
  80. $output .= '<li>' . t("Content type descriptions were previously localized so they won't be affected.") . '</li>';
  81. $output .= '</ul>';
  82. $output .= '<p>' . t('To search and translate strings, use the <a href="@translate-interface">translation interface</a> pages.', array('@translate-interface' => url('admin/config/regional/translate'))) . '</p>';
  83. return $output;
  84. case 'admin/config/regional/i18n':
  85. case 'admin/config/regional/i18n/node':
  86. $output = '<p>' . t('You can find some more per content type options on the <a href="@configure_content">Content types administration page</a>.', array('@configure_content' => url('admin/structure/types'))) . '</p>';
  87. return $output;
  88. }
  89. }
  90. /**
  91. * Implements hook_i18n_context_language().
  92. */
  93. function i18n_node_i18n_context_language() {
  94. // Node language when loading specific nodes or creating translations.
  95. if (arg(0) == 'node' ) {
  96. if (($node = menu_get_object('node')) && !empty($node->language) && i18n_node_type_enabled($node)) {
  97. return i18n_language_object($node->language);
  98. }
  99. elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['target']) && ($source = node_load($_GET['translation'])) && i18n_node_type_enabled($source)) {
  100. return i18n_language_object($_GET['target']);
  101. }
  102. }
  103. }
  104. /**
  105. * Implements hook_i18n_translate_path()
  106. */
  107. function i18n_node_i18n_translate_path($path) {
  108. if (preg_match("!^node/(\d+)(/.+|)!", $path, $matches) && ($node = node_load((int) $matches[1])) && i18n_object_langcode($node) && !empty($node->tnid)) {
  109. if ($translations = translation_node_get_translations($node->tnid)) {
  110. $result = array();
  111. foreach ($translations as $langcode => $node_translation) {
  112. $result[$langcode] = array(
  113. 'href' => 'node/' . $node_translation->nid . $matches[2],
  114. 'title' => $node_translation->title,
  115. 'object' => $node_translation,
  116. );
  117. }
  118. return $result;
  119. }
  120. }
  121. }
  122. /**
  123. * Implements hook_menu_alter().
  124. *
  125. * Take over the node translation page.
  126. */
  127. function i18n_node_menu_alter(&$items) {
  128. if (isset($items['node/%node/translate'])) {
  129. $items['node/%node/translate']['page callback'] = 'i18n_node_translation_overview';
  130. $items['node/%node/translate']['file'] = 'i18n_node.pages.inc';
  131. $items['node/%node/translate']['module'] = 'i18n_node';
  132. }
  133. // Take over node/add pages for string translation
  134. $items['node/add']['page callback'] = 'i18n_node_add_page';
  135. $items['node/add']['file'] = 'i18n_node.pages.inc';
  136. $items['node/add']['file path'] = drupal_get_path('module', 'i18n_node');
  137. // @TODO avoid iterating over every router path.
  138. foreach (node_type_get_types() as $type) {
  139. $path = 'node/add/' . str_replace('_', '-', $type->type);
  140. if (isset($items[$path])) {
  141. $items[$path]['title callback'] = 'i18n_node_type_name';
  142. $items[$path]['title arguments'] = array($type->type, $type->name);
  143. }
  144. }
  145. }
  146. /**
  147. * Get node language.
  148. */
  149. function i18n_node_get_lang($nid, $default = '') {
  150. $lang = db_query('SELECT language FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  151. return $lang ? $lang : $default ;
  152. }
  153. /**
  154. * Get allowed languages for node.
  155. *
  156. * This allows node types to define its own language list implementing hook 'language_list'.
  157. *
  158. * @param $node
  159. * Node to retrieve language list for.
  160. * @param $translate
  161. * Only languages available for translation.
  162. * @param $select
  163. * Only languages that can be selected for this node
  164. */
  165. function i18n_node_language_list($node, $translate = FALSE, $select = FALSE) {
  166. // Check if the node module manages its own language list.
  167. $languages = node_invoke($node, 'language_list', $translate);
  168. if (!$languages) {
  169. $languages = i18n_language_list('name', i18n_node_language_mode($node));
  170. if ($translate && isset($node->tnid) && $node->tnid && ($translations = translation_node_get_translations($node->tnid))) {
  171. unset($translations[$node->language]);
  172. foreach (array_keys($translations) as $langcode) {
  173. unset($languages[$langcode]);
  174. }
  175. }
  176. // Language may be locked for this node type, restrict options to current one
  177. if ($select && i18n_node_language_options($node, 'lock') && !empty($node->language) && !empty($languages[$node->language])) {
  178. $languages = array($node->language => $languages[$node->language]);
  179. }
  180. // Check language required for this type (no language neutral)
  181. elseif (!i18n_node_language_options($node, 'required')) {
  182. $languages = array(LANGUAGE_NONE => t('Language neutral')) + $languages;
  183. }
  184. }
  185. return $languages;
  186. }
  187. /**
  188. * Check options for node language
  189. */
  190. function i18n_node_language_options($node, $option) {
  191. $options = variable_get('i18n_node_options_' . $node->type, array());
  192. return in_array($option, $options, TRUE);
  193. }
  194. /**
  195. * Get language mode for node or node type
  196. */
  197. function i18n_node_language_mode($type) {
  198. $type = is_object($type) ? $type->type : $type;
  199. return variable_get('i18n_node_extended_' . $type, I18N_LANGUAGE_ENABLED);
  200. }
  201. /**
  202. * Implements hook_node_prepare().
  203. */
  204. function i18n_node_node_prepare($node) {
  205. $options = variable_get('i18n_node_options_' . $node->type, array());
  206. if (i18n_node_type_enabled($node) && empty($node->nid) && !i18n_object_langcode($node) && in_array('current', $options)) {
  207. // Set current language for new nodes if option enabled
  208. $node->language = i18n_language_content()->language;
  209. }
  210. }
  211. /**
  212. * Implements hook_permission().
  213. *
  214. * Permissions defined
  215. * - administer all languages
  216. * Disables language conditions for administration pages, so the user can view objects for all languages at the same time.
  217. * This applies for: menu items, taxonomy
  218. * - administer translations
  219. * Will allow to add/remove existing nodes to/from translation sets.
  220. */
  221. function i18n_node_permission() {
  222. return array(
  223. 'administer content translations' => array(
  224. 'title' => t('Administer content translations'),
  225. 'description' => t('Add or remove existing content to translation sets.'),
  226. ),
  227. );
  228. }
  229. /**
  230. * Implements hook_node_view()
  231. */
  232. function i18n_node_node_view($node) {
  233. if (i18n_node_type_enabled($node)) {
  234. $node->content['language'] = array(
  235. '#type' => 'item',
  236. '#title' => t('Language'),
  237. '#markup' => i18n_language_name($node->language),
  238. );
  239. }
  240. }
  241. /**
  242. * Implements hook_node_view_alter().
  243. *
  244. * Handles links for extended languages. Sets current interface language.
  245. */
  246. function i18n_node_node_view_alter(&$build) {
  247. $node = $build['#node'];
  248. // Hide node translation links.
  249. if (variable_get('i18n_hide_translation_links', 0)) {
  250. if (isset($build['links']['translation'])) {
  251. unset($build['links']['translation']);
  252. }
  253. }
  254. elseif (!empty($node->tnid) && !empty($build['links']['translation']) && i18n_node_language_mode($node) == I18N_LANGUAGE_EXTENDED) {
  255. // We only get links for translations for enabled languages
  256. // Set the right languages if language support is extended but not visible.
  257. $links = &$build['links']['translation']['#links'];
  258. $translations = translation_node_get_translations($node->tnid);
  259. foreach (i18n_node_language_list($node) as $langcode => $langname) {
  260. $index = 'translation_' . $langcode;
  261. if ($langcode != $node->language && isset($translations[$langcode]) && $translations[$langcode]->status && !isset($links[$index])) {
  262. // This a published translation to a disabled language. As the node is language extended, display the linkso
  263. // These links shouldn't switch the interface, though they have a language so the right language icon will be added
  264. $language = i18n_language_object($langcode);
  265. $links[$index] = array(
  266. 'href' => 'node/' . $translations[$langcode]->nid,
  267. 'title' => $language->native,
  268. 'language' => $language,
  269. 'attributes' => array(
  270. 'title' => $translations[$langcode]->title,
  271. 'class' => array('translation-link'),
  272. ),
  273. );
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * Implements hook_node_type_insert()
  280. */
  281. function i18n_node_node_type_insert($info) {
  282. i18n_node_node_type_update($info);
  283. }
  284. /**
  285. * Implements hook_node_type_update()
  286. */
  287. function i18n_node_node_type_update($info) {
  288. if (!empty($info->old_type) && $info->old_type != $info->type) {
  289. i18n_string_update_context("node:type:$info->old_type:*", "node:type:$info->type:*");
  290. }
  291. i18n_string_object_update('node_type', $info);
  292. }
  293. /**
  294. * Implements hook_node_type_delete()
  295. */
  296. function i18n_node_node_type_delete($info) {
  297. i18n_string_object_remove('node_type', $info);
  298. }
  299. /**
  300. * Implements hook_elements().
  301. *
  302. * Add a process callback for textfields.
  303. *
  304. * * @todo Update D7
  305. */
  306. /*
  307. function i18n_node_elements() {
  308. $type = array();
  309. $type['textfield'] = array('#process' => array('i18n_node_textfield_process'));
  310. return $type;
  311. }
  312. */
  313. /**
  314. * Process callback for textfield elements.
  315. *
  316. * When editing or translating a node, set Javascript to rewrite autocomplete
  317. * paths to use the node language prefix rather than the current content one.
  318. *
  319. * @todo Update D7
  320. */
  321. function i18n_node_textfield_process($element) {
  322. global $language;
  323. static $sent = FALSE;
  324. // Ensure we send the Javascript only once.
  325. if (!$sent && isset($element['#autocomplete_path']) && !empty($element['#autocomplete_path']) && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) {
  326. // Add a JS file for node forms.
  327. // Determine if we are either editing or translating an existing node.
  328. // We can't act on regular node creation because we don't have a specified
  329. // node language.
  330. $node_edit = $node = menu_get_object() && arg(2) == 'edit' && isset($node->language) && !empty($node->language);
  331. $node_translate = arg(0) == 'node' && arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language']);
  332. if ($node_edit || $node_translate) {
  333. $node_language = $node_edit ? $node->language : $_GET['language'];
  334. // Only needed if the node language is different from the interface one.
  335. if ($node_language != $language->language) {
  336. $languages = language_list();
  337. if (isset($languages[$node_language])) {
  338. drupal_add_js(drupal_get_path('module', 'i18n_node') . '/i18n_node.js');
  339. // Pass the interface and content language base paths.
  340. // Remove any trailing forward slash. Doing so prevents a mismatch
  341. // that occurs when a language has no prefix and hence gets a path
  342. // with a trailing forward slash.
  343. $interface = rtrim(url('', array('absolute' => TRUE)), '/');
  344. $content = rtrim(url('', array('absolute' => TRUE, 'language' => $languages[$node_language])), '/');
  345. $data = array('interface_path' => $interface, 'content_path' => $content);
  346. drupal_add_js(array('i18n' => $data), 'setting');
  347. }
  348. }
  349. }
  350. $sent = TRUE;
  351. }
  352. return $element;
  353. }
  354. /**
  355. * Implements hook_form_FORM_ID_alter().
  356. */
  357. function i18n_node_form_search_form_alter(&$form, &$form_state) {
  358. // Advanced search form. Add language and localize content type names
  359. if ($form['module']['#value'] == 'node' && !empty($form['advanced'])) {
  360. // @todo Handle language search conditions
  361. //$form['advanced']['language'] = _i18n_language_select();
  362. if (!empty($form['advanced']['type'])) {
  363. foreach ($form['advanced']['type']['#options'] as $type => $name) {
  364. $form['advanced']['type']['#options'][$type] = i18n_node_translate_type($type, 'name', $name);
  365. }
  366. }
  367. }
  368. }
  369. /**
  370. * Implements hook_form_FORM_ID_alter().
  371. */
  372. function i18n_node_form_node_type_form_alter(&$form, &$form_state) {
  373. if (isset($form['type'])) {
  374. $disabled = !i18n_node_type_enabled($form['#node_type']);
  375. $form['i18n'] = array(
  376. '#type' => 'fieldset',
  377. '#title' => t('Multilingual settings'),
  378. '#collapsible' => TRUE,
  379. '#collapsed' => TRUE,
  380. '#group' => 'additional_settings',
  381. '#attributes' => array(
  382. 'class' => array('i18n-node-type-settings-form'),
  383. ),
  384. '#description' => t('Extended multilingual options provided by Internationalization module.'),
  385. '#disabled' => $disabled,
  386. );
  387. // Some settings about node languages. Add variables for node type from variable definition
  388. if ($form['#node_type']->type) {
  389. variable_type_include('node_type');
  390. $form['i18n'] += node_variable_type_subform($form['#node_type']->type, array('i18n_node_options', 'i18n_node_extended'));
  391. }
  392. // Add disabled message
  393. if ($disabled) {
  394. $form['i18n']['#description'] .= ' <em>' . t('These will be available only when you enable Multilingual support in Publishing options above.') . '</em>';
  395. foreach (element_children($form['i18n']) as $key) {
  396. $form['i18n'][$key]['#disabled'] = TRUE;
  397. }
  398. }
  399. }
  400. }
  401. /**
  402. * Implements hook_form_BASE_FORM_ID_alter().
  403. */
  404. function i18n_node_form_node_form_alter(&$form, $form_state) {
  405. $node = $form['#node'];
  406. /**
  407. * i18n has to override locale.module
  408. * drupal_alter() fails to order modules correctly in some cases
  409. * for example specific hooks like hook_form_BASE_FORM_ID_alter
  410. *
  411. * its not possbile to reorder hook_form_BASE_FORM_ID_alter with
  412. * hook_module_implements_alter
  413. *
  414. * @see http://drupal.org/node/765860
  415. */
  416. // Replace core's node submit callback with our own,
  417. // in order to translate the node type name.
  418. $key = array_search('node_form_submit', $form['actions']['submit']['#submit']);
  419. if ($key !== FALSE) {
  420. $form['actions']['submit']['#submit'][$key] = 'i18n_node_form_submit';
  421. }
  422. // call a 'private' implemenation of i18n_node_form_node_form_alter()
  423. $form['#after_build'][] = '_i18n_node_form_node_form_alter';
  424. }
  425. /**
  426. * Replacement for core's node_form_submit(), taking care of
  427. * translating node type names.
  428. */
  429. function i18n_node_form_submit($form, &$form_state) {
  430. $node = node_form_submit_build_node($form, $form_state);
  431. $insert = empty($node->nid);
  432. node_save($node);
  433. $node_link = l(t('view'), 'node/' . $node->nid);
  434. $type = node_type_get_type($node->type);
  435. $type_name = i18n_node_translate_type($type);
  436. $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  437. $t_args = array('@type' => $type_name, '%title' => $node->title);
  438. if ($insert) {
  439. watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  440. drupal_set_message(t('@type %title has been created.', $t_args));
  441. }
  442. else {
  443. watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  444. drupal_set_message(t('@type %title has been updated.', $t_args));
  445. }
  446. if ($node->nid) {
  447. $form_state['values']['nid'] = $node->nid;
  448. $form_state['nid'] = $node->nid;
  449. $form_state['redirect'] = 'node/' . $node->nid;
  450. }
  451. else {
  452. // In the unlikely case something went wrong on save, the node will be
  453. // rebuilt and node form redisplayed the same way as in preview.
  454. drupal_set_message(t('The post could not be saved.'), 'error');
  455. $form_state['rebuild'] = TRUE;
  456. }
  457. // Clear the page and block caches.
  458. cache_clear_all();
  459. }
  460. /**
  461. * Implements hook_form_BASE_FORM_ID_alter().
  462. * Called by i18n_node_form_node_form_alter
  463. */
  464. function _i18n_node_form_node_form_alter($form, &$form_state) {
  465. $node = $form['#node'];
  466. if (i18n_node_type_enabled($node)) {
  467. if (!empty($form['language']['#options'])) {
  468. $form['language']['#options'] = i18n_node_language_list($node, TRUE, TRUE);
  469. }
  470. }
  471. elseif (variable_get('i18n_node_default_language_none', 0) && !isset($form['#node']->nid)) {
  472. // Override locale module setting default language to nodes. It is already in form_state.
  473. $form['language']['#value'] = $form_state['values']['language'] = LANGUAGE_NONE;
  474. }
  475. // Translate field names for title and body for the node edit form.
  476. if (!empty($form['title']['#title'])) {
  477. $form['title']['#title'] = i18n_node_translate_type($node->type, 'title_label', $form['title']['#title']);
  478. }
  479. if (!empty($form['body_field']['body']['#title'])) {
  480. $form['body_field']['body']['#title'] = i18n_node_translate_type($node->type, 'body', $form['body_field']['body']['#title']);
  481. }
  482. // Translate page title for node/add/% and node/%/edit pages.
  483. $types = node_type_get_types();
  484. if (empty($node->nid) && strpos($_GET['q'], 'node/add/' . str_replace('_', '-', $node->type)) === 0) {
  485. drupal_set_title(t('Create @name', array('@name' => i18n_node_translate_type($types[$node->type], 'name'))), PASS_THROUGH);
  486. }
  487. elseif (!empty($node->nid) && $_GET['q'] == 'node/' . $node->nid . '/edit') {
  488. drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => i18n_node_translate_type($types[$node->type], 'name'), '@title' => $node->title)), PASS_THROUGH);
  489. }
  490. return $form;
  491. }
  492. /**
  493. * Implements hook_theme().
  494. */
  495. function i18n_node_theme() {
  496. return array(
  497. 'i18n_node_select_translation' => array(
  498. 'render element' => 'element',
  499. 'file' => 'i18n_node.pages.inc',
  500. ),
  501. );
  502. }
  503. /**
  504. * Shorthand for translating node type strings
  505. *
  506. * @param $type
  507. * Node type name or full object
  508. */
  509. function i18n_node_translate_type($type, $property = 'name', $source = NULL, $options = array()) {
  510. if (is_object($type)) {
  511. $source = $type->$property;
  512. $type = $type->type;
  513. }
  514. return i18n_string_translate(array('node', 'type', $type, $property), $source, $options);
  515. }
  516. /**
  517. * Translate node type name (unfiltered)
  518. */
  519. function i18n_node_type_name($type, $name) {
  520. return i18n_string_translate(array('node', 'type', $type, 'name'), $name);
  521. }
  522. /**
  523. * Check whether this is a language enabled node type
  524. *
  525. * @param $type
  526. * Node, node type object, or node type name
  527. */
  528. function i18n_node_type_enabled($type) {
  529. $type = is_object($type) ? $type->type : $type;
  530. $mode = variable_get('language_content_type_' . $type, 0);
  531. return $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
  532. }