i18n_taxonomy.module 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. <?php
  2. /**
  3. * @file
  4. * i18n taxonomy module
  5. *
  6. * Internationalization (i18n) package.
  7. *
  8. * This module groups together all existing i18n taxonomy functionality
  9. * providing several options for taxonomy translation.
  10. *
  11. * Translates taxonomy term for selected vocabularies running them through the localization system.
  12. * It also translates terms for views filters and views results.
  13. *
  14. * @author Jose A. Reyero, 2004
  15. */
  16. /**
  17. * Implements hook_help().
  18. */
  19. function i18n_taxonomy_help($path, $arg) {
  20. switch ($path) {
  21. case 'admin/help#i18n_taxonomy' :
  22. $output = '<p>' . t('This module adds support for multilingual taxonomy. You can set up multilingual options for each vocabulary:') . '</p>';
  23. $output .= '<ul>';
  24. $output .= '<li>' . t('A language can be assigned globaly for a vocabulary.') . '</li>';
  25. $output .= '<li>' . t('Different terms for each language with translation relationships.') . '</li>';
  26. $output .= '<li>' . t('Terms can be common to all languages, but may be localized.') . '</li>';
  27. $output .= '</ul>';
  28. $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>';
  29. $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) . '</p>';
  30. return $output;
  31. case 'admin/config/regional/i18n':
  32. $output = '<p>' . t('To set up multilingual options for vocabularies go to <a href="@configure_taxonomy">Taxonomy configuration page</a>.', array('@configure_taxonomy' => url('admin/structure/taxonomy'))) . '</p>';
  33. return $output;
  34. case 'admin/structure/taxonomy/%':
  35. $vocabulary = taxonomy_vocabulary_machine_name_load($arg[3]);
  36. switch (i18n_taxonomy_vocabulary_mode($vocabulary)) {
  37. case I18N_MODE_LOCALIZE:
  38. return '<p>' . t('%capital_name is a localizable vocabulary. You will be able to translate term names and descriptions using the <a href="@translate-interface">translate interface</a> pages.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name, '@translate-interface' => url('admin/config/regional/translate'))) . '</p>';
  39. case I18N_MODE_LANGUAGE:
  40. return '<p>' . t('%capital_name is a vocabulary with a fixed language. All the terms in this vocabulary will have %language language.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name, '%language' => i18n_language_property($vocabulary->language, 'name'))) . '</p>';
  41. case I18N_MODE_TRANSLATE:
  42. return '<p>' . t('%capital_name is a full multilingual vocabulary. You will be able to set a language for each term and create translation relationships.', array('%capital_name' => drupal_ucfirst($vocabulary->name))) . '</p>';
  43. }
  44. break;
  45. }
  46. }
  47. /**
  48. * Implements hook_menu().
  49. */
  50. function i18n_taxonomy_menu() {
  51. $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/list'] = array(
  52. 'title' => 'Terms',
  53. 'type' => MENU_DEFAULT_LOCAL_TASK,
  54. 'weight' => -20,
  55. );
  56. $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/sets'] = array(
  57. 'title' => 'Translation sets',
  58. 'page callback' => 'i18n_taxonomy_translation_sets_overview',
  59. 'page arguments' => array(3),
  60. 'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_sets_access',
  61. 'access arguments' => array(3),
  62. 'type' => MENU_LOCAL_TASK,
  63. 'file' => 'i18n_taxonomy.admin.inc',
  64. );
  65. $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/sets/add'] = array(
  66. 'title' => 'Create new translation',
  67. 'page callback' => 'drupal_get_form',
  68. 'page arguments' => array('i18n_taxonomy_translation_term_form', 3),
  69. 'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_sets_access',
  70. 'access arguments' => array(3),
  71. 'type' => MENU_LOCAL_ACTION,
  72. //'parent' => 'admin/content/taxonomy/%taxonomy_vocabulary',
  73. 'file' => 'i18n_taxonomy.admin.inc',
  74. );
  75. $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/sets/edit/%i18n_taxonomy_translation_set'] = array(
  76. 'title' => 'Edit translation',
  77. 'page callback' => 'drupal_get_form',
  78. 'page arguments' => array('i18n_taxonomy_translation_term_form', 3, 7),
  79. 'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_sets_access',
  80. 'access arguments' => array(3),
  81. 'type' => MENU_CALLBACK,
  82. //'parent' => 'admin/content/taxonomy/%taxonomy_vocabulary',
  83. 'file' => 'i18n_taxonomy.admin.inc',
  84. );
  85. $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/sets/delete/%i18n_taxonomy_translation_set'] = array(
  86. 'title' => 'Delete translation',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('i18n_translation_set_delete_confirm', 7),
  89. 'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_sets_access',
  90. 'access arguments' => array(3),
  91. 'type' => MENU_CALLBACK,
  92. );
  93. $items['i18n/taxonomy/autocomplete/vocabulary/%taxonomy_vocabulary_machine_name/%'] = array(
  94. 'title' => 'Autocomplete taxonomy',
  95. 'page callback' => 'i18n_taxonomy_autocomplete_language',
  96. 'page arguments' => array(5, 4),
  97. 'access arguments' => array('access content'),
  98. 'type' => MENU_CALLBACK,
  99. 'file' => 'i18n_taxonomy.pages.inc',
  100. );
  101. $items['i18n/taxonomy/autocomplete/language/%'] = array(
  102. 'title' => 'Autocomplete taxonomy',
  103. 'page callback' => 'i18n_taxonomy_autocomplete_language',
  104. 'page arguments' => array(4, NULL),
  105. 'access arguments' => array('access content'),
  106. 'type' => MENU_CALLBACK,
  107. 'file' => 'i18n_taxonomy.pages.inc',
  108. );
  109. return $items;
  110. }
  111. /**
  112. * Implements hook_admin_paths().
  113. */
  114. function i18n_taxonomy_admin_paths() {
  115. $paths = array(
  116. 'taxonomy/*/translate' => TRUE,
  117. 'taxonomy/*/translate/*' => TRUE,
  118. );
  119. return $paths;
  120. }
  121. /**
  122. * Implements hook_menu_alter().
  123. *
  124. * Take over the taxonomy pages
  125. */
  126. function i18n_taxonomy_menu_alter(&$items) {
  127. // If ctool's page manager is active for the path skip this modules override.
  128. // Also views module takes over this page so this won't work if views enabled.
  129. // Skip when taxonomy_display enabled, see http://drupal.org/node/1280194
  130. if (variable_get('page_manager_term_view_disabled', TRUE) && !module_exists('taxonomy_display')) {
  131. // Taxonomy term page. Localize terms.
  132. $items['taxonomy/term/%taxonomy_term']['page callback'] = 'i18n_taxonomy_term_page';
  133. $items['taxonomy/term/%taxonomy_term']['title callback'] = 'i18n_taxonomy_term_name';
  134. $items['taxonomy/term/%taxonomy_term']['file'] = 'i18n_taxonomy.pages.inc';
  135. $items['taxonomy/term/%taxonomy_term']['file path'] = drupal_get_path('module', 'i18n_taxonomy');
  136. }
  137. // Localize autocomplete
  138. $items['taxonomy/autocomplete']['page callback'] = 'i18n_taxonomy_autocomplete_field';
  139. $items['taxonomy/autocomplete']['file'] = 'i18n_taxonomy.pages.inc';
  140. $items['taxonomy/autocomplete']['file path'] = drupal_get_path('module', 'i18n_taxonomy');
  141. }
  142. /**
  143. * Menu access callback for vocabulary translation tab. Show tab only for full multilingual vocabularies.
  144. */
  145. function i18n_taxonomy_vocabulary_translation_tab_sets_access($vocabulary) {
  146. return user_access('administer taxonomy') && i18n_taxonomy_vocabulary_mode($vocabulary->vid, I18N_MODE_TRANSLATE);
  147. }
  148. /**
  149. * Menu access callback for term translation tab. Show tab only for translatable terms
  150. *
  151. * @todo This should work also for localizable terms when we've got that part implemented
  152. */
  153. function i18n_taxonomy_term_translation_tab_access($term) {
  154. return taxonomy_term_edit_access($term) && i18n_taxonomy_vocabulary_mode($term->vid) & I18N_MODE_MULTIPLE && user_access('translate interface');
  155. }
  156. /**
  157. * Implements hook_field_formatter_info().
  158. */
  159. function i18n_taxonomy_field_formatter_info() {
  160. return array(
  161. 'i18n_taxonomy_term_reference_link' => array(
  162. 'label' => t('Link (localized)'),
  163. 'field types' => array('taxonomy_term_reference'),
  164. ),
  165. 'i18n_taxonomy_term_reference_plain' => array(
  166. 'label' => t('Plain text (localized)'),
  167. 'field types' => array('taxonomy_term_reference'),
  168. ),
  169. );
  170. }
  171. /**
  172. * Implements hook_field_formatter_prepare_view().
  173. *
  174. * This preloads all taxonomy terms for multiple loaded objects at once and
  175. * unsets values for invalid terms that do not exist.
  176. */
  177. function i18n_taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  178. return taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
  179. }
  180. /**
  181. * Implements hook_field_formatter_view().
  182. */
  183. function i18n_taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  184. $element = array();
  185. $language = i18n_language_interface();
  186. // Terms whose tid is 'autocreate' do not exist
  187. // yet and $item['taxonomy_term'] is not set. Theme such terms as
  188. // just their name.
  189. switch ($display['type']) {
  190. case 'i18n_taxonomy_term_reference_link':
  191. foreach ($items as $delta => $item) {
  192. if ($item['tid'] == 'autocreate') {
  193. $element[$delta] = array(
  194. '#markup' => check_plain($item['name']),
  195. );
  196. }
  197. else {
  198. if (isset($item['taxonomy_term'])) {
  199. $term = $item['taxonomy_term'];
  200. }
  201. else {
  202. $term = taxonomy_term_load($item['tid']);
  203. }
  204. $uri = entity_uri('taxonomy_term', $term);
  205. $element[$delta] = array(
  206. '#type' => 'link',
  207. '#title' => i18n_taxonomy_term_name($term, $language->language),
  208. '#href' => $uri['path'],
  209. '#options' => $uri['options'],
  210. );
  211. }
  212. }
  213. break;
  214. case 'i18n_taxonomy_term_reference_plain':
  215. foreach ($items as $delta => $item) {
  216. $name = ($item['tid'] != 'autocreate' ? i18n_taxonomy_term_name($item['taxonomy_term'], $language->language): $item['name']);
  217. $element[$delta] = array(
  218. '#markup' => check_plain($name),
  219. );
  220. }
  221. break;
  222. }
  223. return $element;
  224. }
  225. /**
  226. * Implements hook_field_extra_fields().
  227. */
  228. function i18n_taxonomy_field_extra_fields() {
  229. $return = array();
  230. $info = entity_get_info('taxonomy_term');
  231. foreach (array_keys($info['bundles']) as $bundle) {
  232. $vocabulary = taxonomy_vocabulary_machine_name_load($bundle);
  233. if ($vocabulary && i18n_taxonomy_vocabulary_mode($vocabulary, I18N_MODE_TRANSLATE)) {
  234. $return['taxonomy_term'][$bundle] = i18n_language_field_extra();
  235. }
  236. }
  237. return $return;
  238. }
  239. /**
  240. * Implements hook_field_attach_view_alter().
  241. */
  242. function i18n_taxonomy_field_attach_view_alter(&$output, $context) {
  243. // Copied from rdf_field_attach_view_alter of modules/rdf/rdf.module since we have another #formatter
  244. // Append term mappings on displayed taxonomy links.
  245. foreach (element_children($output) as $field_name) {
  246. $element = &$output[$field_name];
  247. if (isset($element['#field_type']) && $element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'i18n_taxonomy_term_reference_link') {
  248. foreach ($element['#items'] as $delta => $item) {
  249. // This function is invoked during entity preview when taxonomy term
  250. // reference items might contain free-tagging terms that do not exist
  251. // yet and thus have no $item['taxonomy_term'].
  252. if (isset($item['taxonomy_term'])) {
  253. $term = $item['taxonomy_term'];
  254. if (!empty($term->rdf_mapping['rdftype'])) {
  255. $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
  256. }
  257. if (!empty($term->rdf_mapping['name']['predicates'])) {
  258. $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
  259. }
  260. }
  261. }
  262. }
  263. }
  264. // Add language field for display
  265. if ($context['entity_type'] == 'taxonomy_term' && i18n_taxonomy_vocabulary_mode($context['entity']->vid, I18N_MODE_TRANSLATE)) {
  266. $output['language'] = array(
  267. '#type' => 'item',
  268. '#title' => t('Language'),
  269. '#markup' => i18n_language_name($context['entity']->language),
  270. );
  271. }
  272. }
  273. /**
  274. * Implements hook_field_info_alter()
  275. */
  276. function i18n_taxonomy_field_info_alter(&$info) {
  277. // Change default formatter for term reference fields
  278. $info['taxonomy_term_reference']['default_formatter'] = 'i18n_taxonomy_term_reference_link';
  279. // Translate field values.
  280. $info['taxonomy_term_reference']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
  281. // Sync callback for field translations
  282. $info['taxonomy_term_reference']['i18n_sync_callback'] = 'i18n_taxonomy_field_prepare_translation';
  283. }
  284. /**
  285. * Implements hook_field_storage_details_alter().
  286. *
  287. * We don't alter the storage details but the stored details of the field itself...
  288. *
  289. * @param array $field
  290. * The field record just read from the database.
  291. */
  292. function i18n_taxonomy_field_storage_details_alter(&$details, &$field) {
  293. if ($field['type'] === 'taxonomy_term_reference') {
  294. $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
  295. }
  296. }
  297. /**
  298. * Implements hook_field_attach_prepare_translation_alter().
  299. *
  300. * Prepare and synchronize translation for term reference fields.
  301. */
  302. function i18n_taxonomy_field_attach_prepare_translation_alter(&$entity, $context) {
  303. $entity_type = $context['entity_type'];
  304. $source_entity = $context['source_entity'];
  305. $options = array(
  306. 'default' => FALSE,
  307. 'deleted' => FALSE,
  308. 'language' => NULL,
  309. );
  310. // Determine the list of instances to iterate on.
  311. list(, , $bundle) = entity_extract_ids($entity_type, $source_entity);
  312. $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
  313. if (!empty($instances)) {
  314. foreach ($instances as $field_info) {
  315. $field = field_info_field($field_info['field_name']);
  316. if ($field['type'] == 'taxonomy_term_reference' && isset($entity->{$field_info['field_name']})) {
  317. // iterate over languages.
  318. foreach ($entity->{$field_info['field_name']} as $language => &$items) {
  319. i18n_taxonomy_field_prepare_translation($entity_type, $entity, $field, $field_info, $entity->language, $items, $source_entity, $source_entity->language);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. /**
  326. * Prepare and synchronize translation for term reference fields
  327. */
  328. function i18n_taxonomy_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
  329. foreach ($items as $index => $item) {
  330. $term = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
  331. if ($translation = i18n_taxonomy_term_get_translation($term, $langcode)) {
  332. $items[$index] = array(
  333. 'taxonomy_term' => $translation,
  334. 'tid' => $translation->tid
  335. );
  336. }
  337. $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
  338. }
  339. }
  340. /**
  341. * Returns the set of valid terms for a taxonomy field.
  342. *
  343. * @param $field
  344. * The field definition.
  345. * @return
  346. * The array of valid terms for this field, keyed by term id.
  347. */
  348. function i18n_taxonomy_allowed_values($field) {
  349. global $language;
  350. $options = array();
  351. foreach ($field['settings']['allowed_values'] as $tree) {
  352. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  353. if (i18n_taxonomy_vocabulary_mode($vocabulary->vid) == I18N_MODE_TRANSLATE) {
  354. $parent = i18n_taxonomy_translation_term_tid($tree['parent'], NULL, $tree['parent']);
  355. $language = i18n_language_context();
  356. $terms = i18n_taxonomy_get_tree($vocabulary->vid, $language->language, $parent);
  357. }
  358. else {
  359. $terms = taxonomy_get_tree($vocabulary->vid, $tree['parent']);
  360. }
  361. if ($terms) {
  362. foreach ($terms as $term) {
  363. $options[$term->tid] = str_repeat('-', $term->depth) . i18n_taxonomy_term_name($term);
  364. }
  365. }
  366. }
  367. }
  368. return $options;
  369. }
  370. /**
  371. * Implements hook_i18n_translate_path()
  372. */
  373. function i18n_taxonomy_i18n_translate_path($path) {
  374. if (strpos($path, 'taxonomy/term/') === 0) {
  375. return i18n_taxonomy_translate_path($path);
  376. }
  377. }
  378. /**
  379. * Implements hook_i18n_context_language().
  380. */
  381. function i18n_taxonomy_i18n_context_language() {
  382. if (arg(0) == 'taxonomy') {
  383. // Taxonomy term pages
  384. if (arg(1) == 'term' && is_numeric(arg(2)) && ($term = menu_get_object('taxonomy_term', 2)) && ($langcode = i18n_object_langcode($term))) {
  385. return i18n_language_object($langcode);
  386. }
  387. }
  388. }
  389. /**
  390. * Find translations for taxonomy paths.
  391. *
  392. * @param $path
  393. * Path to translate.
  394. * @param $path_prefix
  395. * Path prefix, including trailing slash, defaults to 'taxonomy/term/'.
  396. * It will be different for taxonomy term pages and for forum pages.
  397. *
  398. * @return
  399. * Array of links (each an array with href, title), indexed by language code.
  400. */
  401. function i18n_taxonomy_translate_path($path, $path_prefix = 'taxonomy/term/') {
  402. $prefix_match = strtr($path_prefix, array('/' => '\/'));
  403. if (preg_match("/^($prefix_match)([^\/]*)(.*)$/", $path, $matches)) {
  404. $links = array();
  405. $term = FALSE;
  406. // If single term, treat it differently
  407. if (is_numeric($matches[2]) && !$matches[3]) {
  408. $term = taxonomy_term_load($matches[2]);
  409. if (!empty($term->i18n_tsid)) {
  410. $set = i18n_translation_set_load($term->i18n_tsid);
  411. }
  412. }
  413. foreach (language_list() as $langcode => $language) {
  414. if ($term) {
  415. if (!empty($set) && ($translation = $set->get_item($langcode))) {
  416. $links[$langcode] = array(
  417. 'href' => $path_prefix . $translation->tid,
  418. 'title' => $translation->name,
  419. );
  420. }
  421. else {
  422. $links[$langcode] = array(
  423. 'href' => $path,
  424. 'title' => i18n_taxonomy_term_name($term, $langcode),
  425. );
  426. }
  427. }
  428. elseif ($str_tids = i18n_taxonomy_translation_tids($matches[2], $langcode)) {
  429. $links[$langcode]['href'] = $path_prefix . $str_tids . $matches[3];
  430. }
  431. }
  432. return $links;
  433. }
  434. }
  435. /**
  436. * Get localized term name unfiltered.
  437. */
  438. function i18n_taxonomy_term_name($term, $langcode = NULL) {
  439. $key = i18n_object_info('taxonomy_term', 'key');
  440. return i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE) ? i18n_string(array('taxonomy', 'term', $term->{$key}, 'name'), $term->name, array('langcode' => $langcode, 'sanitize' => FALSE)) : $term->name;
  441. }
  442. /**
  443. * Get localized term description unfiltered.
  444. */
  445. function i18n_taxonomy_term_description($term, $langcode = NULL) {
  446. $key = i18n_object_info('taxonomy_term', 'key');
  447. return i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE) ? i18n_string(array('taxonomy', 'term', $term->{$key}, 'description'), $term->description, array('langcode' => $langcode, 'sanitize' => FALSE)) : $term->description;
  448. }
  449. /**
  450. * Find term translation from translation set.
  451. *
  452. * @param $term
  453. * Term object to find translation.
  454. * @param $langcode
  455. * Language code to find translation for.
  456. * @result object Taxonomy Term
  457. * Translation if exists.
  458. */
  459. function i18n_taxonomy_term_get_translation($term, $langcode) {
  460. if (i18n_object_langcode($term)) {
  461. if ($term->language == $langcode) {
  462. // Translation is the term itself.
  463. return $term;
  464. }
  465. elseif (!empty($term->i18n_tsid)) {
  466. return i18n_translation_set_load($term->i18n_tsid)->get_item($langcode);
  467. }
  468. else {
  469. return NULL;
  470. }
  471. }
  472. else {
  473. // Term has no language, translation should be the same
  474. return $term;
  475. }
  476. }
  477. /**
  478. * Get localized vocabulary name, unfiltered.
  479. */
  480. function i18n_taxonomy_vocabulary_name($vocabulary, $langcode = NULL) {
  481. return i18n_object_langcode($vocabulary) ? $vocabulary->name : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'name'), $vocabulary->name, array('langcode' => $langcode, 'sanitize' => FALSE));
  482. }
  483. /**
  484. * Get localized vocabulary description, unfiltered.
  485. */
  486. function i18n_taxonomy_vocabulary_description($vocabulary, $langcode = NULL) {
  487. return i18n_object_langcode($vocabulary) ? $vocabulary->description : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'), $vocabulary->description, array('langcode' => $langcode, 'sanitize' => FALSE));
  488. }
  489. /**
  490. * Get translated term's tid.
  491. *
  492. * @param $tid
  493. * Node nid to search for translation.
  494. * @param $language
  495. * Language to search for the translation, defaults to current language.
  496. * @param $default
  497. * Value that will be returned if no translation is found.
  498. * @return
  499. * Translated term tid if exists, or $default.
  500. */
  501. function i18n_taxonomy_translation_term_tid($tid, $language = NULL, $default = NULL) {
  502. $translation = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_data} a ON t.i18n_tsid = a.i18n_tsid AND t.tid <> a.tid WHERE a.tid = :tid AND t.language = :language AND t.i18n_tsid > 0', array(
  503. ':tid' => $tid,
  504. ':language' => $language ? $language : i18n_language_content()->language
  505. ))->fetchField();
  506. return $translation ? $translation : $default;
  507. }
  508. /**
  509. * Returns an url for the translated taxonomy-page, if exists.
  510. */
  511. function i18n_taxonomy_translation_tids($str_tids, $lang) {
  512. if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
  513. $separator = '+';
  514. // The '+' character in a query string may be parsed as ' '.
  515. $tids = preg_split('/[+ ]/', $str_tids);
  516. }
  517. elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
  518. $separator = ',';
  519. $tids = explode(',', $str_tids);
  520. }
  521. else {
  522. return;
  523. }
  524. $translated_tids = array();
  525. foreach ($tids as $tid) {
  526. if ($translated_tid = i18n_taxonomy_translation_term_tid($tid, $lang)) {
  527. $translated_tids[] = $translated_tid;
  528. }
  529. }
  530. return implode($separator, $translated_tids);
  531. }
  532. /**
  533. * Implements hook_taxonomy_display_breadcrumb_parents_alter().
  534. */
  535. function i18n_taxonomy_taxonomy_display_breadcrumb_parents_alter(&$parents) {
  536. $parents = i18n_taxonomy_localize_terms($parents);
  537. }
  538. /**
  539. * Implements hook_taxonomy_display_term_page_term_object_alter().
  540. */
  541. function i18n_taxonomy_taxonomy_display_term_page_term_object_alter(&$term) {
  542. $term = i18n_taxonomy_localize_terms($term);
  543. }
  544. /**
  545. * Implements hook_taxonomy_term_insert()
  546. */
  547. function i18n_taxonomy_taxonomy_term_insert($term) {
  548. i18n_taxonomy_taxonomy_term_update($term);
  549. }
  550. /**
  551. * Implements hook_taxonomy_term_update()
  552. */
  553. function i18n_taxonomy_taxonomy_term_update($term) {
  554. if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE)) {
  555. i18n_string_object_update('taxonomy_term', $term);
  556. }
  557. // Multilingual terms, translatable. Link / unlink from translation set.
  558. if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_TRANSLATE) && !empty($term->translation_set)) {
  559. if (i18n_object_langcode($term)) {
  560. $term->translation_set
  561. ->add_item($term)
  562. ->save();
  563. }
  564. elseif (!empty($term->original)) {
  565. // Term set to language neutral, remove it from translation set and update set (delete if empty)
  566. $term->translation_set
  567. ->remove_item($term->original)
  568. ->update_delete();
  569. }
  570. }
  571. }
  572. /**
  573. * Implements hook_taxonomy_term_delete()
  574. */
  575. function i18n_taxonomy_taxonomy_term_delete($term) {
  576. // If a translation set exists for this term, remove this term from the set.
  577. if (isset($term->i18n_tsid) && $term->i18n_tsid) {
  578. $translation_set = i18n_translation_set_load($term->i18n_tsid);
  579. $translation_set->get_translations();
  580. $translation_set->remove_language($term->language);
  581. // If there are no terms left in this translation set, delete the set.
  582. // Otherwise update the set.
  583. $translation_set->update_delete();
  584. }
  585. // Just in case there's any left over string we run it for all terms.
  586. i18n_string_object_remove('taxonomy_term', $term);
  587. }
  588. /**
  589. * Implements hook_taxonomy_vocabulary_insert()
  590. */
  591. function i18n_taxonomy_taxonomy_vocabulary_insert($vocabulary) {
  592. i18n_taxonomy_taxonomy_vocabulary_update($vocabulary);
  593. }
  594. /**
  595. * Implements hook_taxonomy_vocabulary_update()
  596. */
  597. function i18n_taxonomy_taxonomy_vocabulary_update($vocabulary) {
  598. // Update language for related terms
  599. switch (i18n_taxonomy_vocabulary_mode($vocabulary)) {
  600. case I18N_MODE_LANGUAGE:
  601. $update['language'] = $vocabulary->language;
  602. break;
  603. case I18N_MODE_NONE:
  604. $update['language'] = LANGUAGE_NONE;
  605. break;
  606. }
  607. if (isset($update)) {
  608. db_update('taxonomy_term_data')
  609. ->fields($update)
  610. ->condition('vid', $vocabulary->vid)
  611. ->execute();
  612. drupal_set_message(t('Reset language for all terms.'));
  613. }
  614. // Update strings, always add translation if no language
  615. if (!i18n_object_langcode($vocabulary)) {
  616. i18n_string_object_update('taxonomy_vocabulary', $vocabulary);
  617. }
  618. }
  619. /**
  620. * Implements hook_taxonomy_vocabulary_delete()
  621. */
  622. function i18n_taxonomy_taxonomy_vocabulary_delete($vocabulary) {
  623. i18n_string_object_remove('taxonomy_vocabulary', $vocabulary);
  624. }
  625. /**
  626. * Implements hook_taxonomy_term_presave()
  627. */
  628. function i18n_taxonomy_taxonomy_term_presave($term) {
  629. switch (i18n_taxonomy_vocabulary_mode($term->vid)) {
  630. case I18N_MODE_LANGUAGE: // Predefined language for all terms
  631. $term->language = taxonomy_vocabulary_load($term->vid)->language;
  632. break;
  633. case I18N_MODE_TRANSLATE: // Multilingual terms, translatable
  634. if (!isset($term->language)) {
  635. // The term may come from a node tags field, just if this is not a taxonomy form.
  636. // Or from any other object we are editing. So we use 'context' language here.
  637. $term->language = i18n_language_context()->language;
  638. }
  639. // Only for the case the term has no language, it may need to be removed from translation set
  640. if (empty($term->language)) {
  641. $term->i18n_tsid = 0;
  642. }
  643. break;
  644. }
  645. }
  646. /**
  647. * Implements hook_form_FORM_ID_alter()
  648. */
  649. function i18n_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
  650. if (!isset($form_state['confirm_delete'])) {
  651. $vocabulary = $form_state['vocabulary'];
  652. $i18n_mode = i18n_taxonomy_vocabulary_mode($vocabulary);
  653. $langcode = i18n_object_langcode($vocabulary, LANGUAGE_NONE);
  654. // Define the replacement names to add some logic to the translation mode options.
  655. $form += i18n_translation_mode_element('taxonomy_vocabulary', $i18n_mode, $langcode);
  656. if (user_access('translate interface')) {
  657. $form['actions']['translate'] = array(
  658. '#type' => 'submit',
  659. '#name' => 'save_translate',
  660. '#value' => t('Save and translate'),
  661. '#weight' => 5,
  662. '#states' => array(
  663. 'invisible' => array(
  664. // Hide the button if language mode is selected value needs to be a
  665. // string so that the javascript-side matching works.
  666. 'input[name=i18n_mode]' => array('value' => (string)I18N_MODE_LANGUAGE),
  667. )
  668. )
  669. );
  670. // Make sure the delete buttons shows up last.
  671. if (isset($form['actions']['delete'])) {
  672. $form['actions']['delete']['#weight'] = 10;
  673. }
  674. }
  675. $form['#validate'][] = 'i18n_taxonomy_form_vocabulary_validate';
  676. $form['#submit'][] = 'i18n_taxonomy_form_vocabulary_submit';
  677. }
  678. }
  679. /**
  680. * Form submit callback to redirect when using the save and translate button.
  681. */
  682. function i18n_taxonomy_form_vocabulary_submit($form, &$form_state) {
  683. if ($form_state['triggering_element']['#name'] == 'save_translate') {
  684. $form_state['redirect'] = 'admin/structure/taxonomy/' . $form_state['values']['machine_name'] . '/translate';
  685. }
  686. }
  687. /**
  688. * Implements hook_form_FORM_ID_alter()
  689. */
  690. function i18n_taxonomy_form_taxonomy_form_term_alter(&$form, &$form_state) {
  691. // Check for confirmation forms
  692. if (isset($form_state['confirm_delete']) || isset($form_state['confirm_parents'])) return;
  693. $term = $form_state['term'];
  694. $vocabulary = $form['#vocabulary'];
  695. // Mark form so we can know later when saving the term it came from a taxonomy form
  696. $form['i18n_taxonomy_form'] = array('#type' => 'value', '#value' => 1);
  697. // Add language field or not depending on taxonomy mode.
  698. switch (i18n_taxonomy_vocabulary_mode($vocabulary->vid)) {
  699. case I18N_MODE_TRANSLATE:
  700. // Set $form_state['storage'] default as empty array because we will add
  701. // the translation and target from $_GET. So we still have it when the
  702. // page partially reloads with ajax.
  703. if(!isset($form_state['storage'])) {
  704. $form_state['storage'] = array();
  705. }
  706. // get translation from $_GET or $form_state['storage']
  707. $translation = null;
  708. if(isset($_GET['translation'])) {
  709. $translation = $_GET['translation'];
  710. $form_state['storage']['translation'] = $translation;
  711. } else if(isset($form_state['storage']) && isset($form_state['storage']['translation'])){
  712. $translation = $form_state['storage']['translation'];
  713. }
  714. // get target from $_GET or $form_state['storage']
  715. $target = null;
  716. if(isset($_GET['target'])) {
  717. $target = $_GET['target'];
  718. $form_state['storage']['target'] = $target;
  719. } else if(isset($form_state['storage']) && isset($form_state['storage']['target'])){
  720. $target = $form_state['storage']['target'];
  721. }
  722. $form['language'] = array(
  723. '#description' => t('This term belongs to a multilingual vocabulary. You can set a language for it.'),
  724. ) + i18n_element_language_select($term);
  725. // If the term to be added will be a translation of a source term,
  726. // set the default value of the option list to the target language and
  727. // create a form element for storing the translation set of the source term.
  728. if (empty($term->tid) && isset($translation) && isset($target) && ($source_term = taxonomy_term_load($translation)) && ($target_language = i18n_language_object($target))) {
  729. // Set context language to target language.
  730. i18n_language_context($target_language);
  731. // Add the translation set to the form so we know the new term
  732. // needs to be added to that set.
  733. if (!empty($source_term->i18n_tsid)) {
  734. $translation_set = i18n_taxonomy_translation_set_load($source_term->i18n_tsid);
  735. }
  736. else {
  737. // No translation set yet, build a new one with the source term.
  738. $translation_set = i18n_translation_set_create('taxonomy_term', $vocabulary->machine_name)
  739. ->add_item($source_term);
  740. }
  741. $form['language']['#default_value'] = $target_language->language;
  742. $form['language']['#disabled'] = TRUE;
  743. drupal_set_title(t('%language translation of term %title', array('%language' => locale_language_name($_GET['target']), '%title' => $source_term->name)), PASS_THROUGH);
  744. }
  745. elseif (!empty($term->tid) && i18n_object_langcode($term)) {
  746. // Set context language to term language.
  747. i18n_language_context(i18n_language_object($term->language));
  748. // If the current term is part of a translation set,
  749. // remove all other languages of the option list.
  750. if (!empty($term->i18n_tsid)) {
  751. $translation_set = i18n_taxonomy_translation_set_load($term->i18n_tsid);
  752. $translations = $translation_set->get_translations();
  753. // If the number of translations equals 1, there's only a source translation.
  754. if (count($translations) > 1) {
  755. //unset($form['language']['#options'][LANGUAGE_NONE]);
  756. foreach ($translations as $langcode => $translation) {
  757. if ($translation->tid !== $term->tid) {
  758. unset($form['language']['#options'][$langcode]);
  759. }
  760. }
  761. $form['language']['#description'] = t('This term already belongs to a <a href="@term-translation">translation set</a>. Changing language to <i>Language neutral</i> will remove it from the set.', array('@term-translation' => url('taxonomy/term/' . $term->tid . '/translate')));
  762. }
  763. }
  764. }
  765. // If we've got a translation set, add it to the form.
  766. if (!empty($translation_set)) {
  767. $form['translation_set'] = array(
  768. '#type' => 'value',
  769. '#value' => $translation_set,
  770. );
  771. }
  772. break;
  773. case I18N_MODE_LANGUAGE:
  774. // Set context language to vocabulary language and add value to the form.
  775. i18n_language_context(i18n_language_object($vocabulary->language));
  776. $form['language'] = array(
  777. '#type' => 'value',
  778. '#value' => $vocabulary->language
  779. );
  780. $form['identification']['language_info'] = array('#value' => t('All terms in this vocabulary have a fixed language: %language', array('%language' => i18n_language_name($vocabulary->language))));
  781. break;
  782. case I18N_MODE_LOCALIZE:
  783. $form['language'] = array(
  784. '#type' => 'value',
  785. '#value' => LANGUAGE_NONE,
  786. );
  787. break;
  788. case I18N_MODE_NONE:
  789. default:
  790. $form['language'] = array(
  791. '#type' => 'value',
  792. '#value' => LANGUAGE_NONE,
  793. );
  794. break;
  795. }
  796. if (user_access('translate interface') && i18n_taxonomy_vocabulary_mode($vocabulary->vid) & I18N_MODE_MULTIPLE) {
  797. $form['actions']['translate'] = array(
  798. '#type' => 'submit',
  799. '#name' => 'save_translate',
  800. '#value' => t('Save and translate'),
  801. '#weight' => 5,
  802. '#states' => array(
  803. 'invisible' => array(
  804. // Hide the button if term is language neutral.
  805. 'select[name=language]' => array('value' => LANGUAGE_NONE),
  806. ),
  807. ),
  808. );
  809. // Make sure the delete buttons shows up last.
  810. if (isset($form['actions']['delete'])) {
  811. $form['actions']['delete']['#weight'] = 10;
  812. }
  813. $form['#submit'][] = 'i18n_taxonomy_form_term_submit';
  814. }
  815. }
  816. /**
  817. * Form submit callback to redirect when using the save and translate button.
  818. */
  819. function i18n_taxonomy_form_term_submit($form, &$form_state) {
  820. if ($form_state['triggering_element']['#name'] == 'save_translate') {
  821. // When using the edit link on the list terms, a destination param is
  822. // added that needs to be unset to make the redirection work.
  823. unset($_GET['destination']);
  824. $form_state['redirect'] = 'taxonomy/term/' . $form_state['values']['tid'] . '/translate';
  825. }
  826. }
  827. /**
  828. * Implements hook_form_alter().
  829. *
  830. * This is the place to add language fields to all forms.
  831. *
  832. * @ TO DO The vocabulary form needs some javascript.
  833. */
  834. function i18n_taxonomy_form_alter(&$form, $form_state, $form_id) {
  835. switch ($form_id) {
  836. case 'taxonomy_overview_vocabularies':
  837. $vocabularies = taxonomy_get_vocabularies();
  838. foreach ($vocabularies as $vocabulary) {
  839. if (i18n_object_langcode($vocabulary)) {
  840. $form[$vocabulary->vid]['name']['#markup'] .= ' (' . i18n_language_name($vocabulary->language) . ')';
  841. }
  842. }
  843. break;
  844. case 'taxonomy_overview_terms':
  845. // We check for vocabulary object first, because when confirming alphabetical ordering it uses the same form
  846. if (!empty($form['#vocabulary']) && i18n_taxonomy_vocabulary_mode($form['#vocabulary']->vid) & I18N_MODE_TRANSLATE) {
  847. foreach (element_children($form) as $key) {
  848. if (isset($form[$key]['#term']) && ($lang = i18n_object_langcode($form[$key]['#term']))) {
  849. $form[$key]['view']['#suffix'] = ' (' . i18n_language_name($lang) . ')';
  850. }
  851. }
  852. }
  853. break;
  854. case 'search_form':
  855. // Localize category selector in advanced search form.
  856. if (!empty($form['advanced']) && !empty($form['advanced']['category'])) {
  857. i18n_taxonomy_form_all_localize($form['advanced']['category']);
  858. }
  859. break;
  860. }
  861. }
  862. /**
  863. * Validate multilingual options for vocabulary form
  864. */
  865. function i18n_taxonomy_form_vocabulary_validate($form, &$form_state) {
  866. if ($form_state['values']['i18n_mode'] & I18N_MODE_LANGUAGE) {
  867. if ($form_state['values']['language'] == LANGUAGE_NONE) {
  868. form_set_error('language', t('If selecting "Set language to vocabulary" you need to set a language to this vocabulary. Either change the translation mode or select a language.'));
  869. }
  870. }
  871. else {
  872. $form_state['values']['language'] = LANGUAGE_NONE;
  873. }
  874. }
  875. /**
  876. * Localize a taxonomy_form_all() kind of control
  877. *
  878. * The options array is indexed by vocabulary name and then by term id, with tree structure
  879. * We just need to localize vocabulary name and localizable terms. Multilingual vocabularies
  880. * should have been taken care of by query rewriting.
  881. **/
  882. function i18n_taxonomy_form_all_localize(&$item) {
  883. $options = &$item['#options'];
  884. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  885. if (!empty($options[$vocabulary->name])) {
  886. // Localize vocabulary name if translated
  887. $vname = i18n_taxonomy_vocabulary_name($vocabulary);
  888. if ($vname != $vocabulary->name) {
  889. $options[$vname] = $options[$vocabulary->name];
  890. unset($options[$vocabulary->name]);
  891. }
  892. if (i18n_taxonomy_vocabulary_mode($vid) & I18N_MODE_LOCALIZE) {
  893. $tree = taxonomy_get_tree($vid);
  894. if ($tree && (count($tree) > 0)) {
  895. foreach ($tree as $term) {
  896. if (isset($options[$vname][$term->tid])) {
  897. $options[$vname][$term->tid] = str_repeat('-', $term->depth) . i18n_taxonomy_term_name($term);
  898. }
  899. }
  900. }
  901. }
  902. }
  903. }
  904. }
  905. /**
  906. * Translate an array of taxonomy terms.
  907. *
  908. * Translates all terms with language, just passing over terms without it.
  909. * Filter out terms with a different language
  910. *
  911. * @param $taxonomy
  912. * Array of term objects or tids or multiple arrays or terms indexed by vid
  913. * @param $langcode
  914. * Language code of target language
  915. * @param $fullterms
  916. * Whether to return full $term objects, returns tids otherwise
  917. * @return
  918. * Array with translated terms: tid -> $term
  919. * Array with vid and term array
  920. */
  921. function i18n_taxonomy_translate_terms($taxonomy, $langcode, $fullterms = TRUE) {
  922. $translation = array();
  923. if (is_array($taxonomy) && $taxonomy) {
  924. foreach ($taxonomy as $index => $tdata) {
  925. if (is_array($tdata)) {
  926. // Case 1: Index is vid, $tdata is an array of terms
  927. $mode = i18n_taxonomy_vocabulary_mode($index);
  928. // We translate just some vocabularies: translatable, fixed language
  929. // Fixed language ones may have terms translated, though the UI doesn't support it
  930. if ($mode & I18N_MODE_LANGUAGE || $mode & I18N_MODE_TRANSLATE) {
  931. $translation[$index] = i18n_taxonomy_translate_terms($tdata, $langcode, $fullterms);
  932. }
  933. elseif ($fullterms) {
  934. $translation[$index] = array_map('_i18n_taxonomy_filter_terms', $tdata);
  935. }
  936. else {
  937. $translation[$index] = array_map('_i18n_taxonomy_filter_tids', $tdata);
  938. }
  939. continue;
  940. }
  941. elseif (is_object($tdata)) {
  942. // Case 2: This is a term object
  943. $term = $tdata;
  944. }
  945. elseif (is_numeric($tdata) && ($tid = (int)$tdata)) {
  946. // Case 3: This is a term tid, load the full term
  947. $term = taxonomy_term_load($tid);
  948. }
  949. // Translate the term if we got it
  950. if (empty($term)) {
  951. // Couldn't identify term, pass through whatever it is
  952. $translation[$index] = $tdata;
  953. }
  954. elseif ($term->language && $term->language != $langcode) {
  955. $translation_set = i18n_translation_set_load($term->i18n_tsid);
  956. $translations = ($translation_set) ? $translation_set->get_translations() : NULL;
  957. if ($translations && !empty($translations[$langcode])) {
  958. $newterm = $translations[$langcode];
  959. $translation[$newterm->tid] = $fullterms ? $newterm : $newterm->tid;
  960. }
  961. }
  962. else {
  963. // Term has no language. Should be ok.
  964. $translation[$index] = $fullterms ? $term : $term->tid;
  965. }
  966. }
  967. }
  968. return $translation;
  969. }
  970. /**
  971. * Localize taxonomy terms for localizable vocabularies.
  972. *
  973. * @param $terms
  974. * Array of term objects or term object.
  975. * @return
  976. * Array of terms with the right ones localized.
  977. */
  978. function i18n_taxonomy_localize_terms($terms) {
  979. // If not localizable language just return. Performance optimizations.
  980. if (!i18n_string_translate_langcode()) {
  981. return $terms;
  982. }
  983. // $terms is not a valid array or term.
  984. if (empty($terms)) {
  985. return $terms;
  986. }
  987. $object_info = i18n_object_info('taxonomy_term');
  988. $list = is_array($terms) ? $terms : array($terms);
  989. foreach ($list as $index => $term) {
  990. if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE)) {
  991. $localize[$index] = $term->tid;
  992. }
  993. }
  994. // If multiple terms, preload all translations, then run object translation.
  995. if (!empty($localize)) {
  996. i18n_string_translation_search(array('taxonomy', 'term', $localize, '*'));
  997. foreach ($localize as $index => $tid) {
  998. $list[$index] = i18n_string_object_translate('taxonomy_term', $list[$index]);
  999. }
  1000. }
  1001. // Return array or simple object, depending on incoming format.
  1002. return is_array($terms) ? $list : reset($list);
  1003. }
  1004. /**
  1005. * Taxonomy vocabulary settings.
  1006. *
  1007. * @param $vid
  1008. * Vocabulary object or vocabulary id.
  1009. * @param $mode
  1010. * Vocabulary mode to compare with.
  1011. *
  1012. */
  1013. function i18n_taxonomy_vocabulary_mode($vid, $mode = NULL) {
  1014. $modes = &drupal_static(__FUNCTION__);
  1015. if (is_object($vid)) {
  1016. $vid_mode = i18n_object_field($vid, 'i18n_mode', I18N_MODE_NONE);
  1017. return isset($mode) ? $mode & $vid_mode : $vid_mode;
  1018. }
  1019. else {
  1020. if (!isset($modes[$vid])) {
  1021. $modes[$vid] = i18n_object_field(taxonomy_vocabulary_load($vid), 'i18n_mode', I18N_MODE_NONE);
  1022. }
  1023. return isset($mode) ? $mode & $modes[$vid] : $modes[$vid];
  1024. }
  1025. }
  1026. /**
  1027. * Get taxonomy tree for a given language
  1028. *
  1029. * @param $vid
  1030. * Vocabulary id
  1031. * @param $lang
  1032. * Language code
  1033. * @param $parent
  1034. * Parent term id for the tree
  1035. */
  1036. function i18n_taxonomy_get_tree($vid, $langcode, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
  1037. $children = &drupal_static(__FUNCTION__, array());
  1038. $parents = &drupal_static(__FUNCTION__ . ':parents', array());
  1039. $terms = &drupal_static(__FUNCTION__ . ':terms', array());
  1040. // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  1041. // and its children, too.
  1042. if (!isset($children[$vid][$langcode])) {
  1043. $children[$vid][$langcode] = array();
  1044. $parents[$vid][$langcode] = array();
  1045. $terms[$vid][$langcode] = array();
  1046. $query = db_select('taxonomy_term_data', 't');
  1047. $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
  1048. $result = $query
  1049. ->addTag('translatable')
  1050. ->addTag('taxonomy_term_access')
  1051. ->fields('t')
  1052. ->fields('h', array('parent'))
  1053. ->condition('t.vid', $vid)
  1054. ->condition('t.language', $langcode)
  1055. ->orderBy('t.weight')
  1056. ->orderBy('t.name')
  1057. ->execute();
  1058. foreach ($result as $term) {
  1059. $children[$vid][$langcode][$term->parent][] = $term->tid;
  1060. $parents[$vid][$langcode][$term->tid][] = $term->parent;
  1061. $terms[$vid][$langcode][$term->tid] = $term;
  1062. }
  1063. }
  1064. // Load full entities, if necessary. The entity controller statically
  1065. // caches the results.
  1066. if ($load_entities) {
  1067. $term_entities = taxonomy_term_load_multiple(array_keys($terms[$vid][$langcode]));
  1068. }
  1069. $max_depth = (!isset($max_depth)) ? count($children[$vid][$langcode]) : $max_depth;
  1070. $tree = array();
  1071. // Keeps track of the parents we have to process, the last entry is used
  1072. // for the next processing step.
  1073. $process_parents = array();
  1074. $process_parents[] = $parent;
  1075. // Loops over the parent terms and adds its children to the tree array.
  1076. // Uses a loop instead of a recursion, because it's more efficient.
  1077. while (count($process_parents)) {
  1078. $parent = array_pop($process_parents);
  1079. // The number of parents determines the current depth.
  1080. $depth = count($process_parents);
  1081. if ($max_depth > $depth && !empty($children[$vid][$langcode][$parent])) {
  1082. $has_children = FALSE;
  1083. $child = current($children[$vid][$langcode][$parent]);
  1084. do {
  1085. if (empty($child)) {
  1086. break;
  1087. }
  1088. $term = $load_entities ? $term_entities[$child] : $terms[$vid][$langcode][$child];
  1089. if (count($parents[$vid][$langcode][$term->tid]) > 1) {
  1090. // We have a term with multi parents here. Clone the term,
  1091. // so that the depth attribute remains correct.
  1092. $term = clone $term;
  1093. }
  1094. $term->depth = $depth;
  1095. unset($term->parent);
  1096. $term->parents = $parents[$vid][$langcode][$term->tid];
  1097. $tree[] = $term;
  1098. if (!empty($children[$vid][$langcode][$term->tid])) {
  1099. $has_children = TRUE;
  1100. // We have to continue with this parent later.
  1101. $process_parents[] = $parent;
  1102. // Use the current term as parent for the next iteration.
  1103. $process_parents[] = $term->tid;
  1104. // Reset pointers for child lists because we step in there more often
  1105. // with multi parents.
  1106. reset($children[$vid][$langcode][$term->tid]);
  1107. // Move pointer so that we get the correct term the next time.
  1108. next($children[$vid][$langcode][$parent]);
  1109. break;
  1110. }
  1111. } while ($child = next($children[$vid][$langcode][$parent]));
  1112. if (!$has_children) {
  1113. // We processed all terms in this hierarchy-level, reset pointer
  1114. // so that this function works the next time it gets called.
  1115. reset($children[$vid][$langcode][$parent]);
  1116. }
  1117. }
  1118. }
  1119. return $tree;
  1120. }
  1121. /**
  1122. * Recursive array filtering, convert all terms to 'tid'.
  1123. */
  1124. function _i18n_taxonomy_filter_tids($tid) {
  1125. if (is_array($tid)) {
  1126. return array_map('_i18n_taxonomy_filter_tids', $tid);
  1127. }
  1128. else {
  1129. return is_object($tid) ? $tid->tid : (int)$tid;
  1130. }
  1131. }
  1132. /**
  1133. * Recursive array filtering, convert all terms to 'term object'
  1134. */
  1135. function _i18n_taxonomy_filter_terms($term) {
  1136. if (is_array($term)) {
  1137. return array_map('_i18n_taxonomy_filter_terms', $term);
  1138. }
  1139. else {
  1140. return is_object($term) ? $term : taxonomy_term_load($term);
  1141. }
  1142. }
  1143. /**
  1144. * Load translation set. Menu loading callback.
  1145. */
  1146. function i18n_taxonomy_translation_set_load($tsid) {
  1147. return i18n_translation_set_load($tsid, 'taxonomy_term');
  1148. }
  1149. /**
  1150. * Implements hook_field_uuid_load().
  1151. */
  1152. function i18n_taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  1153. taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
  1154. }
  1155. /**
  1156. * Implements hook_field_uuid_presave().
  1157. */
  1158. function i18n_taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  1159. taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
  1160. }
  1161. /**
  1162. * Implements hook_entity_info_alter().
  1163. */
  1164. function i18n_taxonomy_entity_info_alter(&$entity_info) {
  1165. if (isset($entity_info['taxonomy_vocabulary'])) {
  1166. // Add altered vocabulary schema fields.
  1167. $entity_info['taxonomy_vocabulary']['schema_fields_sql']['base table'][] = 'i18n_mode';
  1168. $entity_info['taxonomy_vocabulary']['schema_fields_sql']['base table'][] = 'language';
  1169. }
  1170. if (isset($entity_info['taxonomy_term'])) {
  1171. // Core doesn't provide a label callback for taxonomy terms. By setting one
  1172. // we can use it to return the correct localized term name.
  1173. $entity_info['taxonomy_term']['label callback'] = 'i18n_taxonomy_taxonomy_term_label';
  1174. // Also let core know terms have languages, now.
  1175. $entity_info['taxonomy_term']['entity keys']['language'] = 'language';
  1176. }
  1177. }
  1178. /**
  1179. * Providing a hook_entity_info() 'label callback' to ensure modules that use
  1180. * entity_label() get the correct localized taxonomy term.
  1181. */
  1182. function i18n_taxonomy_taxonomy_term_label($term, $entity_type) {
  1183. return i18n_taxonomy_term_name($term, i18n_language_interface()->language);
  1184. }
  1185. /**
  1186. * Implements hook_views_api().
  1187. */
  1188. function i18n_taxonomy_views_api() {
  1189. return array(
  1190. 'api' => 3,
  1191. );
  1192. }
  1193. /**
  1194. * Implements hook_module_enabled().
  1195. *
  1196. * Updates options_list_callback for taxonomy term fields.
  1197. *
  1198. * @param $modules
  1199. */
  1200. function i18n_taxonomy_modules_enabled($modules) {
  1201. $modules = drupal_map_assoc($modules);
  1202. if (isset($modules['i18n_taxonomy'])) {
  1203. foreach (field_info_fields() as $fieldname => $field) {
  1204. if ($field['type'] == 'taxonomy_term_reference') {
  1205. $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
  1206. field_update_field($field);
  1207. }
  1208. }
  1209. }
  1210. }
  1211. /**
  1212. * Implements hook_views_pre_render().
  1213. */
  1214. function i18n_taxonomy_views_pre_render(&$view) {
  1215. if($view->base_table !== 'rules_scheduler') {
  1216. global $language;
  1217. foreach ($view->result as $delta => $term){
  1218. if (isset($term->tid)) {
  1219. i18n_string_translate_langcode($language->language);
  1220. $localized_term = i18n_taxonomy_localize_terms(taxonomy_term_load($term->tid));
  1221. $term->tid = $localized_term->tid;
  1222. $term->taxonomy_term_data_name = $localized_term->name;
  1223. $term->taxonomy_term_data_description = $localized_term->description;
  1224. }
  1225. }
  1226. }
  1227. }