i18n_taxonomy.install 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for i18n_taxonomy module.
  5. */
  6. /**
  7. * Set language field in its own table.
  8. * Do not drop node.language now, just in case.
  9. * TO-DO: Drop old tables, fields
  10. */
  11. function i18n_taxonomy_install() {
  12. module_load_install('i18n');
  13. i18n_install_create_fields('taxonomy_vocabulary', array('language', 'i18n_mode'), TRUE);
  14. i18n_install_create_fields('taxonomy_term_data', array('language', 'i18n_tsid'));
  15. // Set module weight for it to run after core modules, but before views.
  16. db_query("UPDATE {system} SET weight = 5 WHERE name = 'i18n_taxonomy' AND type = 'module'");
  17. // Set vocabulary mode if updating from D6, module changed name
  18. if (variable_get('i18n_drupal6_update')) {
  19. i18n_taxonomy_update_7000();
  20. }
  21. }
  22. /**
  23. * Implements hook_uninstall().
  24. */
  25. function i18n_taxonomy_uninstall() {
  26. db_drop_field('taxonomy_vocabulary', 'language');
  27. db_drop_field('taxonomy_vocabulary', 'i18n_mode');
  28. db_drop_field('taxonomy_term_data', 'language');
  29. db_drop_field('taxonomy_term_data', 'i18n_tsid');
  30. variable_del('i18n_taxonomy_vocabulary');
  31. }
  32. /**
  33. * Implements hook_schema_alter().
  34. */
  35. function i18n_taxonomy_schema_alter(&$schema) {
  36. $schema['taxonomy_vocabulary']['fields']['language'] = array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => LANGUAGE_NONE);
  37. $schema['taxonomy_vocabulary']['fields']['i18n_mode'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0);
  38. $schema['taxonomy_term_data']['fields']['language'] = array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => LANGUAGE_NONE);
  39. $schema['taxonomy_term_data']['fields']['i18n_tsid'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0);
  40. }
  41. /**
  42. * Implements hook_disable()
  43. */
  44. function i18n_taxonomy_disable() {
  45. foreach (field_info_fields() as $fieldname => $field) {
  46. if ($field['type'] == 'taxonomy_term_reference' && $field['settings']['options_list_callback'] == 'i18n_taxonomy_allowed_values') {
  47. $field['settings']['options_list_callback'] = NULL;
  48. field_update_field($field);
  49. }
  50. }
  51. }
  52. /**
  53. * Set vocabulary modes from D6 variable
  54. */
  55. function i18n_taxonomy_update_7000() {
  56. if ($options = variable_get('i18ntaxonomy_vocabulary')) {
  57. foreach ($options as $vid => $mode) {
  58. $mode = $mode == 3 ? I18N_MODE_TRANSLATE : $mode;
  59. db_update('taxonomy_vocabulary')
  60. ->fields(array('i18n_mode' => $mode))
  61. ->condition('vid', $vid)
  62. ->execute();
  63. }
  64. variable_del('i18ntaxonomy_vocabulary');
  65. }
  66. // Move to new translation set system
  67. if (db_field_exists('taxonomy_term_data', 'trid')) {
  68. $query = db_select('taxonomy_term_data', 't');
  69. $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
  70. $query
  71. ->fields('t', array('trid'))
  72. ->fields('v', array('machine_name'))
  73. ->condition('t.trid', 0, '>')
  74. ->distinct();
  75. foreach ($query->execute() as $record) {
  76. $tset = i18n_translation_set_create('taxonomy_term', $record->machine_name);
  77. db_update('taxonomy_term_data')
  78. ->fields(array('trid' => 0, 'i18n_tsid' => $tset->tsid))
  79. ->condition('trid', $record->trid)
  80. ->execute();
  81. }
  82. db_drop_field('taxonomy_term_data', 'trid');
  83. }
  84. }
  85. /**
  86. * Drop trid column used in D6 if exists
  87. */
  88. function i18n_taxonomy_update_7001() {
  89. if (db_field_exists('taxonomy_term_data', 'trid')) {
  90. db_drop_field('taxonomy_term_data', 'trid');
  91. }
  92. }
  93. /**
  94. * Switch back to real taxonomy fields and override option_list_callback.
  95. */
  96. function i18n_taxonomy_update_7002(&$sandbox) {
  97. foreach (field_info_fields() as $fieldname => $field) {
  98. if ($field['type'] == 'taxonomy_term_reference') {
  99. $field['module'] = 'taxonomy';
  100. $field['settings']['options_list_callback'] = module_exists('i18n_taxonomy') ? 'i18n_taxonomy_allowed_values' : NULL;
  101. drupal_write_record('field_config', $field, array('id'));
  102. }
  103. }
  104. }
  105. /**
  106. * Unset option_list_callback if i18n_taxonomy is disabled.
  107. */
  108. function i18n_taxonomy_update_7003(&$sandbox) {
  109. if (!function_exists('i18n_taxonomy_allowed_values')) {
  110. i18n_taxonomy_disable();
  111. }
  112. }
  113. /**
  114. * Update D6 language fields in {taxonomy_vocabulary} and {taxonomy_term_data}.
  115. */
  116. function i18n_taxonomy_update_7004() {
  117. i18n_install_create_fields('taxonomy_vocabulary', array('language'));
  118. i18n_install_create_fields('taxonomy_term_data', array('language'));
  119. }