entity_translation.install 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @file
  4. * Installation functions for Entity Translation module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function entity_translation_schema() {
  10. $schema['entity_translation'] = array(
  11. 'description' => 'Table to track entity translations',
  12. 'fields' => array(
  13. 'entity_type' => array(
  14. 'type' => 'varchar',
  15. 'length' => 128,
  16. 'not null' => TRUE,
  17. 'default' => '',
  18. 'description' => 'The entity type this translation relates to',
  19. ),
  20. 'entity_id' => array(
  21. 'type' => 'int',
  22. 'unsigned' => TRUE,
  23. 'not null' => TRUE,
  24. 'description' => 'The entity id this translation relates to',
  25. ),
  26. // @todo: Consider an integer field for 'language'.
  27. 'language' => array(
  28. 'type' => 'varchar',
  29. 'length' => 32,
  30. 'not null' => TRUE,
  31. 'default' => '',
  32. 'description' => 'The target language for this translation.',
  33. ),
  34. 'source' => array(
  35. 'type' => 'varchar',
  36. 'length' => 32,
  37. 'not null' => TRUE,
  38. 'default' => '',
  39. 'description' => 'The source language from which this translation was created.',
  40. ),
  41. 'uid' => array(
  42. 'description' => 'The author of this translation.',
  43. 'type' => 'int',
  44. 'not null' => TRUE,
  45. 'default' => 0,
  46. ),
  47. 'status' => array(
  48. 'description' => 'Boolean indicating whether the translation is published (visible to non-administrators).',
  49. 'type' => 'int',
  50. 'not null' => TRUE,
  51. 'default' => 1,
  52. ),
  53. 'translate' => array(
  54. 'description' => 'A boolean indicating whether this translation needs to be updated.',
  55. 'type' => 'int',
  56. 'not null' => TRUE,
  57. 'default' => 0,
  58. ),
  59. 'created' => array(
  60. 'description' => 'The Unix timestamp when the translation was created.',
  61. 'type' => 'int',
  62. 'not null' => TRUE,
  63. 'default' => 0,
  64. ),
  65. 'changed' => array(
  66. 'description' => 'The Unix timestamp when the translation was most recently saved.',
  67. 'type' => 'int',
  68. 'not null' => TRUE,
  69. 'default' => 0,
  70. ),
  71. ),
  72. 'primary key' => array('entity_type', 'entity_id', 'language'),
  73. );
  74. return $schema;
  75. }
  76. /**
  77. * Implements hook_install().
  78. */
  79. function entity_translation_install() {
  80. // entity_translation_form_alter() needs to run after locale_form_alter() and
  81. // translation_menu(); entity_translation_menu_alter() needs to run after
  82. // i18n_node_menu_alter().
  83. db_update('system')
  84. ->fields(array('weight' => 11))
  85. ->condition('name', 'entity_translation')
  86. ->execute();
  87. // Enable translation for nodes.
  88. variable_set('entity_translation_entity_types', array('node' => 'node'));
  89. // Make translation use the content language type.
  90. variable_set('translation_language_type', LANGUAGE_TYPE_CONTENT);
  91. }
  92. /**
  93. * Grant 'edit $type original values' permission to existing roles.
  94. */
  95. function _entity_translation_grant_edit_permissions() {
  96. variable_set('entity_translation_workflow_enabled', TRUE);
  97. $permissions = array();
  98. // Nodes.
  99. $permissions['node'][] = 'bypass node access';
  100. foreach (node_permissions_get_configured_types() as $type) {
  101. $permissions['node'][] = "edit own $type content";
  102. $permissions['node'][] = "edit any $type content";
  103. }
  104. // Comments.
  105. if (module_exists('comment')) {
  106. $permissions['comment'][] = 'administer comments';
  107. $permissions['comment'][] = 'edit own comments';
  108. }
  109. // Taxonomy terms.
  110. if (module_exists('taxonomy')) {
  111. $permissions['taxonomy_term'][] = 'administer taxonomy';
  112. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  113. $permissions['taxonomy_term'][] = "edit terms in {$vocabulary->vid}";
  114. }
  115. }
  116. $assignments = array();
  117. foreach ($permissions as $entity_type => $permissions_filter) {
  118. if (entity_translation_enabled($entity_type)) {
  119. $permission = "edit $entity_type original values";
  120. $assignments[] = _entity_translation_grant_permission($permission, $permissions_filter);
  121. $permission = "edit $entity_type translation shared fields";
  122. $assignments[] = _entity_translation_grant_permission($permission, $permissions_filter);
  123. }
  124. }
  125. $assignments = '<ul><li>' . implode('</li><li>', $assignments) . '</li></ul>';
  126. $t = get_t();
  127. return $t('The following permissions have been assigned to existing roles: !assignments', array('!assignments' => $assignments));
  128. }
  129. /**
  130. * Grant the given permission to all roles which already have any of the
  131. * permissions specified in the $permissions_filter parameter.
  132. *
  133. * @param $permission
  134. * The new permission which to grant.
  135. * @param $permissions_filter
  136. * List of permissions used for loading roles.
  137. *
  138. * @return
  139. * A message describing permission changes.
  140. */
  141. function _entity_translation_grant_permission($permission, $permissions_filter = NULL) {
  142. $roles = user_roles(FALSE, $permissions_filter);
  143. foreach ($roles as $rid => $role) {
  144. user_role_grant_permissions($rid, array($permission));
  145. }
  146. $t = get_t();
  147. return $t('%permission was assigned to %roles', array(
  148. '%permission' => $permission,
  149. '%roles' => implode(', ', $roles)
  150. ));
  151. }
  152. /**
  153. * Implements hook_enable().
  154. */
  155. function entity_translation_enable() {
  156. // Re-activate entity translation for content types which had used it when
  157. // the module was last disabled (if any), unless these have since been altered
  158. // by the user to use a different translation option.
  159. $entity_translation_types = variable_get('entity_translation_disabled_content_types', array());
  160. foreach ($entity_translation_types as $index => $type) {
  161. if (variable_get("language_content_type_$type", 0) == 0) {
  162. variable_set("language_content_type_$type", ENTITY_TRANSLATION_ENABLED);
  163. }
  164. // We should show the warning only if we actually restored at least one
  165. // content type.
  166. else {
  167. unset($entity_translation_types[$index]);
  168. }
  169. }
  170. if ($entity_translation_types) {
  171. drupal_set_message(t('All content types previously configured to use field translation are now using it again.'), 'warning');
  172. }
  173. variable_del('entity_translation_disabled_content_types');
  174. }
  175. /**
  176. * Implements hook_disable().
  177. */
  178. function entity_translation_disable() {
  179. // Store record of which types are using entity translation, and set those
  180. // types to not be translated. These content types will be reset to use entity
  181. // translation again if the module is later re-enabled, unless they have been
  182. // changed by the user in the meantime.
  183. $entity_translation_types = array();
  184. foreach (node_type_get_types() as $type => $object) {
  185. if (variable_get("language_content_type_$type", 0) == ENTITY_TRANSLATION_ENABLED) {
  186. $entity_translation_types[] = $type;
  187. variable_set("language_content_type_$type", 0);
  188. }
  189. }
  190. if ($entity_translation_types) {
  191. variable_set('entity_translation_disabled_content_types', $entity_translation_types);
  192. drupal_set_message(t('All content types configured to use field translation now have multilingual support disabled. This change will be reverted if the entity translation module is enabled again.'), 'warning');
  193. }
  194. }
  195. /**
  196. * Implements hook_uninstall().
  197. */
  198. function entity_translation_uninstall() {
  199. db_delete('variable')
  200. ->condition('name', db_like('entity_translation_') . '%', 'LIKE')
  201. ->execute();
  202. variable_del('translation_language_type');
  203. variable_del('locale_field_language_fallback');
  204. }
  205. /**
  206. * Implements hook_update_N().
  207. */
  208. function entity_translation_update_7001() {
  209. db_update('system')
  210. ->fields(array('weight' => 11))
  211. ->condition('name', 'entity_translation')
  212. ->execute();
  213. }
  214. /**
  215. * Grant 'edit original values' and 'edit shared field' permissions to roles which have entity editing permissions.
  216. */
  217. function entity_translation_update_7002() {
  218. // Grant the 'edit original values' permission, so we don't break editing on
  219. // existing sites.
  220. return _entity_translation_grant_edit_permissions();
  221. }
  222. /**
  223. * Configure node and comment language settings to the prior default behavior.
  224. */
  225. function entity_translation_update_7003() {
  226. module_load_include('inc', 'entity_translation', 'entity_translation.admin');
  227. foreach (array_keys(entity_get_info()) as $entity_type) {
  228. entity_translation_settings_init($entity_type);
  229. }
  230. }
  231. /**
  232. * Rebuild entity information to update the path scheme settings.
  233. */
  234. function entity_translation_update_7004() {
  235. entity_info_cache_clear();
  236. }