i18n_node.module 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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', NULL, array('sanitize' => FALSE));
  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', NULL, array('sanitize' => FALSE));
  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. // Performance: for node view add access information right away.
  117. 'access' => !$matches[2] ? $node_translation->status : NULL,
  118. );
  119. }
  120. return $result;
  121. }
  122. }
  123. }
  124. /**
  125. * Implements hook_menu_alter().
  126. *
  127. * Take over the node translation page.
  128. */
  129. function i18n_node_menu_alter(&$items) {
  130. if (isset($items['node/%node/translate'])) {
  131. $items['node/%node/translate']['page callback'] = 'i18n_node_translation_overview';
  132. $items['node/%node/translate']['file'] = 'i18n_node.pages.inc';
  133. $items['node/%node/translate']['module'] = 'i18n_node';
  134. }
  135. // Take over node/add pages for string translation
  136. $items['node/add']['page callback'] = 'i18n_node_add_page';
  137. $items['node/add']['file'] = 'i18n_node.pages.inc';
  138. $items['node/add']['file path'] = drupal_get_path('module', 'i18n_node');
  139. // @TODO avoid iterating over every router path.
  140. foreach (node_type_get_types() as $type) {
  141. $path = 'node/add/' . str_replace('_', '-', $type->type);
  142. if (isset($items[$path])) {
  143. $items[$path]['title callback'] = 'i18n_node_type_name';
  144. $items[$path]['title arguments'] = array($type->type, $type->name);
  145. }
  146. }
  147. }
  148. /**
  149. * Get node language.
  150. */
  151. function i18n_node_get_lang($nid, $default = '') {
  152. $lang = db_query('SELECT language FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  153. return $lang ? $lang : $default ;
  154. }
  155. /**
  156. * Get allowed languages for node.
  157. *
  158. * This allows node types to define its own language list implementing hook 'language_list'.
  159. *
  160. * @param $node
  161. * Node to retrieve language list for.
  162. * @param $translate
  163. * Only languages available for translation.
  164. * @param $select
  165. * Only languages that can be selected for this node
  166. */
  167. function i18n_node_language_list($node, $translate = FALSE, $select = FALSE) {
  168. // Check if the node module manages its own language list.
  169. $languages = node_invoke($node, 'language_list', $translate);
  170. if (!$languages) {
  171. $languages = i18n_language_list('name', i18n_node_language_mode($node));
  172. if ($translate && isset($node->tnid) && $node->tnid && ($translations = translation_node_get_translations($node->tnid))) {
  173. unset($translations[$node->language]);
  174. foreach (array_keys($translations) as $langcode) {
  175. unset($languages[$langcode]);
  176. }
  177. }
  178. // Language may be locked for this node type, restrict options to current one
  179. if ($select && i18n_node_language_options($node, 'lock') && !empty($node->language) && !empty($languages[$node->language])) {
  180. $languages = array($node->language => $languages[$node->language]);
  181. }
  182. // Check language required for this type (no language neutral)
  183. elseif (!i18n_node_language_options($node, 'required')) {
  184. $languages = array(LANGUAGE_NONE => t('Language neutral')) + $languages;
  185. }
  186. }
  187. return $languages;
  188. }
  189. /**
  190. * Check options for node language
  191. */
  192. function i18n_node_language_options($node, $option) {
  193. $options = variable_get('i18n_node_options_' . $node->type, array());
  194. return in_array($option, $options, TRUE);
  195. }
  196. /**
  197. * Get language mode for node or node type
  198. */
  199. function i18n_node_language_mode($type) {
  200. $type = is_object($type) ? $type->type : $type;
  201. return variable_get('i18n_node_extended_' . $type, I18N_LANGUAGE_ENABLED);
  202. }
  203. /**
  204. * Implements hook_node_prepare().
  205. */
  206. function i18n_node_node_prepare($node) {
  207. $options = variable_get('i18n_node_options_' . $node->type, array());
  208. if (i18n_node_type_enabled($node) && empty($node->nid) && !i18n_object_langcode($node) && in_array('current', $options)) {
  209. // Set current language for new nodes if option enabled
  210. $node->language = i18n_language_content()->language;
  211. }
  212. }
  213. /**
  214. * Implements hook_permission().
  215. *
  216. * Permissions defined
  217. * - administer all languages
  218. * Disables language conditions for administration pages, so the user can view objects for all languages at the same time.
  219. * This applies for: menu items, taxonomy
  220. * - administer translations
  221. * Will allow to add/remove existing nodes to/from translation sets.
  222. */
  223. function i18n_node_permission() {
  224. return array(
  225. 'administer content translations' => array(
  226. 'title' => t('Administer content translations'),
  227. 'description' => t('Add or remove existing content to translation sets.'),
  228. ),
  229. );
  230. }
  231. /**
  232. * Implements hook_node_view()
  233. */
  234. function i18n_node_node_view($node) {
  235. if (i18n_node_type_enabled($node)) {
  236. $node->content['language'] = array(
  237. '#type' => 'item',
  238. '#title' => t('Language'),
  239. '#markup' => i18n_language_name($node->language),
  240. );
  241. }
  242. }
  243. /**
  244. * Implements hook_node_view_alter().
  245. *
  246. * Handles links for extended languages. Sets current interface language.
  247. */
  248. function i18n_node_node_view_alter(&$build) {
  249. $node = $build['#node'];
  250. // Hide node translation links.
  251. if (variable_get('i18n_hide_translation_links', 0)) {
  252. if (isset($build['links']['translation'])) {
  253. unset($build['links']['translation']);
  254. }
  255. }
  256. elseif (!empty($node->tnid) && !empty($build['links']['translation']) && i18n_node_language_mode($node) == I18N_LANGUAGE_EXTENDED) {
  257. // We only get links for translations for enabled languages
  258. // Set the right languages if language support is extended but not visible.
  259. $links = &$build['links']['translation']['#links'];
  260. $translations = translation_node_get_translations($node->tnid);
  261. foreach (i18n_node_language_list($node) as $langcode => $langname) {
  262. $index = 'translation_' . $langcode;
  263. if ($langcode != $node->language && isset($translations[$langcode]) && $translations[$langcode]->status && !isset($links[$index])) {
  264. // This a published translation to a disabled language. As the node is language extended, display the linkso
  265. // These links shouldn't switch the interface, though they have a language so the right language icon will be added
  266. $language = i18n_language_object($langcode);
  267. $links[$index] = array(
  268. 'href' => 'node/' . $translations[$langcode]->nid,
  269. 'title' => $language->native,
  270. 'language' => $language,
  271. 'attributes' => array(
  272. 'title' => $translations[$langcode]->title,
  273. 'class' => array('translation-link'),
  274. ),
  275. );
  276. }
  277. }
  278. }
  279. }
  280. /**
  281. * Implements hook_node_type_insert()
  282. */
  283. function i18n_node_node_type_insert($info) {
  284. i18n_node_node_type_update($info);
  285. }
  286. /**
  287. * Implements hook_node_type_update()
  288. */
  289. function i18n_node_node_type_update($info) {
  290. if (!empty($info->old_type) && $info->old_type != $info->type) {
  291. i18n_string_update_context("node:type:$info->old_type:*", "node:type:$info->type:*");
  292. }
  293. i18n_string_object_update('node_type', $info);
  294. }
  295. /**
  296. * Implements hook_node_type_delete()
  297. */
  298. function i18n_node_node_type_delete($info) {
  299. i18n_string_object_remove('node_type', $info);
  300. }
  301. /**
  302. * Implements hook_elements().
  303. *
  304. * Add a process callback for textfields.
  305. *
  306. * * @todo Update D7
  307. */
  308. /*
  309. function i18n_node_elements() {
  310. $type = array();
  311. $type['textfield'] = array('#process' => array('i18n_node_textfield_process'));
  312. return $type;
  313. }
  314. */
  315. /**
  316. * Process callback for textfield elements.
  317. *
  318. * When editing or translating a node, set Javascript to rewrite autocomplete
  319. * paths to use the node language prefix rather than the current content one.
  320. *
  321. * @todo Update D7
  322. */
  323. function i18n_node_textfield_process($element) {
  324. global $language;
  325. static $sent = FALSE;
  326. // Ensure we send the Javascript only once.
  327. if (!$sent && isset($element['#autocomplete_path']) && !empty($element['#autocomplete_path']) && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_DEFAULT) != LANGUAGE_NEGOTIATION_DEFAULT) {
  328. // Add a JS file for node forms.
  329. // Determine if we are either editing or translating an existing node.
  330. // We can't act on regular node creation because we don't have a specified
  331. // node language.
  332. $node_edit = $node = menu_get_object() && arg(2) == 'edit' && isset($node->language) && !empty($node->language);
  333. $node_translate = arg(0) == 'node' && arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language']);
  334. if ($node_edit || $node_translate) {
  335. $node_language = $node_edit ? $node->language : $_GET['language'];
  336. // Only needed if the node language is different from the interface one.
  337. if ($node_language != $language->language) {
  338. $languages = language_list();
  339. if (isset($languages[$node_language])) {
  340. drupal_add_js(drupal_get_path('module', 'i18n_node') . '/i18n_node.js');
  341. // Pass the interface and content language base paths.
  342. // Remove any trailing forward slash. Doing so prevents a mismatch
  343. // that occurs when a language has no prefix and hence gets a path
  344. // with a trailing forward slash.
  345. $interface = rtrim(url('', array('absolute' => TRUE)), '/');
  346. $content = rtrim(url('', array('absolute' => TRUE, 'language' => $languages[$node_language])), '/');
  347. $data = array('interface_path' => $interface, 'content_path' => $content);
  348. drupal_add_js(array('i18n' => $data), 'setting');
  349. }
  350. }
  351. }
  352. $sent = TRUE;
  353. }
  354. return $element;
  355. }
  356. /**
  357. * Implements hook_form_FORM_ID_alter().
  358. */
  359. function i18n_node_form_search_form_alter(&$form, &$form_state) {
  360. // Advanced search form. Add language and localize content type names
  361. if ($form['module']['#value'] == 'node' && !empty($form['advanced'])) {
  362. // @todo Handle language search conditions
  363. //$form['advanced']['language'] = _i18n_language_select();
  364. if (!empty($form['advanced']['type'])) {
  365. foreach ($form['advanced']['type']['#options'] as $type => $name) {
  366. $form['advanced']['type']['#options'][$type] = i18n_node_translate_type($type, 'name', $name);
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. * Implements hook_form_FORM_ID_alter().
  373. */
  374. function i18n_node_form_node_type_form_alter(&$form, &$form_state) {
  375. if (isset($form['type'])) {
  376. $disabled = !i18n_node_type_enabled($form['#node_type']);
  377. $form['i18n'] = array(
  378. '#type' => 'fieldset',
  379. '#title' => t('Multilingual settings'),
  380. '#collapsible' => TRUE,
  381. '#collapsed' => TRUE,
  382. '#group' => 'additional_settings',
  383. '#attributes' => array(
  384. 'class' => array('i18n-node-type-settings-form'),
  385. ),
  386. '#description' => t('Extended multilingual options provided by Internationalization module.'),
  387. '#disabled' => $disabled,
  388. );
  389. // Some settings about node languages. Add variables for node type from variable definition
  390. if ($form['#node_type']->type) {
  391. variable_type_include('node_type');
  392. $form['i18n'] += node_variable_type_subform($form['#node_type']->type, array('i18n_node_options', 'i18n_node_extended'));
  393. }
  394. // Add disabled message
  395. if ($disabled) {
  396. $form['i18n']['#description'] .= ' <em>' . t('These will be available only when you enable Multilingual support in Publishing options above.') . '</em>';
  397. foreach (element_children($form['i18n']) as $key) {
  398. $form['i18n'][$key]['#disabled'] = TRUE;
  399. }
  400. }
  401. }
  402. }
  403. /**
  404. * Implements hook_form_BASE_FORM_ID_alter().
  405. */
  406. function i18n_node_form_node_form_alter(&$form, $form_state) {
  407. $node = $form['#node'];
  408. /**
  409. * i18n has to override locale.module
  410. * drupal_alter() fails to order modules correctly in some cases
  411. * for example specific hooks like hook_form_BASE_FORM_ID_alter
  412. *
  413. * its not possbile to reorder hook_form_BASE_FORM_ID_alter with
  414. * hook_module_implements_alter
  415. *
  416. * @see http://drupal.org/node/765860
  417. */
  418. // Replace core's node submit callback with our own,
  419. // in order to translate the node type name.
  420. $key = array_search('node_form_submit', $form['actions']['submit']['#submit']);
  421. if ($key !== FALSE) {
  422. $form['actions']['submit']['#submit'][$key] = 'i18n_node_form_submit';
  423. }
  424. // call a 'private' implemenation of i18n_node_form_node_form_alter()
  425. $form['#after_build'][] = '_i18n_node_form_node_form_alter';
  426. }
  427. /**
  428. * Replacement for core's node_form_submit(), taking care of
  429. * translating node type names.
  430. */
  431. function i18n_node_form_submit($form, &$form_state) {
  432. $node = node_form_submit_build_node($form, $form_state);
  433. $insert = empty($node->nid);
  434. node_save($node);
  435. $node_link = l(t('view'), 'node/' . $node->nid);
  436. $type_name = i18n_node_type_name($node->type);
  437. $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  438. $t_args = array('@type' => $type_name, '%title' => $node->title);
  439. if ($insert) {
  440. watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  441. drupal_set_message(t('@type %title has been created.', $t_args));
  442. }
  443. else {
  444. watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  445. drupal_set_message(t('@type %title has been updated.', $t_args));
  446. }
  447. if ($node->nid) {
  448. $form_state['values']['nid'] = $node->nid;
  449. $form_state['nid'] = $node->nid;
  450. $form_state['redirect'] = node_access('view', $node) ? 'node/' . $node->nid : '<front>';
  451. }
  452. else {
  453. // In the unlikely case something went wrong on save, the node will be
  454. // rebuilt and node form redisplayed the same way as in preview.
  455. drupal_set_message(t('The post could not be saved.'), 'error');
  456. $form_state['rebuild'] = TRUE;
  457. }
  458. // Clear the page and block caches.
  459. cache_clear_all();
  460. }
  461. /**
  462. * Implements hook_form_BASE_FORM_ID_alter().
  463. * Called by i18n_node_form_node_form_alter
  464. */
  465. function _i18n_node_form_node_form_alter($form, &$form_state) {
  466. $node = $form['#node'];
  467. if (i18n_node_type_enabled($node)) {
  468. if (!empty($form['language']['#options'])) {
  469. $form['language']['#options'] = i18n_node_language_list($node, TRUE, TRUE);
  470. }
  471. }
  472. elseif (variable_get('i18n_node_default_language_none', 0) && !isset($form['#node']->nid)) {
  473. // Override locale module setting default language to nodes. It is already in form_state.
  474. $form['language']['#value'] = $form_state['values']['language'] = LANGUAGE_NONE;
  475. }
  476. // Translate field names for title and body for the node edit form.
  477. if (!empty($form['title']['#title'])) {
  478. $form['title']['#title'] = i18n_node_translate_type($node->type, 'title_label', $form['title']['#title']);
  479. }
  480. if (!empty($form['body_field']['body']['#title'])) {
  481. $form['body_field']['body']['#title'] = i18n_node_translate_type($node->type, 'body', $form['body_field']['body']['#title']);
  482. }
  483. // Translate page title for node/add/% and node/%/edit pages.
  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_type_name($node->type))), 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_type_name($node->type), '@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. * Get translated node type name (unfiltered)
  518. *
  519. * @param string $type
  520. * Node type.
  521. * @param string $name
  522. * Optional node type name.
  523. */
  524. function i18n_node_type_name($type, $name = NULL) {
  525. $name = isset($name) ? $name : node_type_get_name($type);
  526. return i18n_string_translate(array('node', 'type', $type, 'name'), $name, array('sanitize' => FALSE));
  527. }
  528. /**
  529. * Check whether this is a language enabled node type
  530. *
  531. * @param $type
  532. * Node, node type object, or node type name
  533. */
  534. function i18n_node_type_enabled($type) {
  535. $type = is_object($type) ? $type->type : $type;
  536. $mode = variable_get('language_content_type_' . $type, 0);
  537. return $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
  538. }