entity_translation.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 = array();
  11. $schema['entity_translation'] = array(
  12. 'description' => 'Table to track entity translations',
  13. 'fields' => array(
  14. 'entity_type' => array(
  15. 'type' => 'varchar',
  16. 'length' => 128,
  17. 'not null' => TRUE,
  18. 'default' => '',
  19. 'description' => 'The entity type this translation relates to',
  20. ),
  21. 'entity_id' => array(
  22. 'type' => 'int',
  23. 'unsigned' => TRUE,
  24. 'not null' => TRUE,
  25. 'description' => 'The entity id this translation relates to',
  26. ),
  27. 'revision_id' => array(
  28. 'type' => 'int',
  29. 'unsigned' => TRUE,
  30. 'not null' => TRUE,
  31. 'description' => 'The entity revision id this translation relates to',
  32. ),
  33. 'language' => array(
  34. 'type' => 'varchar',
  35. 'length' => 32,
  36. 'not null' => TRUE,
  37. 'default' => '',
  38. 'description' => 'The target language for this translation.',
  39. ),
  40. 'source' => array(
  41. 'type' => 'varchar',
  42. 'length' => 32,
  43. 'not null' => TRUE,
  44. 'default' => '',
  45. 'description' => 'The source language from which this translation was created.',
  46. ),
  47. 'uid' => array(
  48. 'description' => 'The author of this translation.',
  49. 'type' => 'int',
  50. 'not null' => TRUE,
  51. 'default' => 0,
  52. ),
  53. 'status' => array(
  54. 'description' => 'Boolean indicating whether the translation is published (visible to non-administrators).',
  55. 'type' => 'int',
  56. 'not null' => TRUE,
  57. 'default' => 1,
  58. ),
  59. 'translate' => array(
  60. 'description' => 'A boolean indicating whether this translation needs to be updated.',
  61. 'type' => 'int',
  62. 'not null' => TRUE,
  63. 'default' => 0,
  64. ),
  65. 'created' => array(
  66. 'description' => 'The Unix timestamp when the translation was created.',
  67. 'type' => 'int',
  68. 'not null' => TRUE,
  69. 'default' => 0,
  70. ),
  71. 'changed' => array(
  72. 'description' => 'The Unix timestamp when the translation was most recently saved.',
  73. 'type' => 'int',
  74. 'not null' => TRUE,
  75. 'default' => 0,
  76. ),
  77. ),
  78. 'primary key' => array('entity_type', 'entity_id', 'language'),
  79. );
  80. $schema['entity_translation_revision'] = array(
  81. 'description' => 'Table to track entity translation revisions',
  82. 'fields' => array(
  83. 'entity_type' => array(
  84. 'type' => 'varchar',
  85. 'length' => 128,
  86. 'not null' => TRUE,
  87. 'default' => '',
  88. 'description' => 'The entity type this translation revision relates to',
  89. ),
  90. 'entity_id' => array(
  91. 'type' => 'int',
  92. 'unsigned' => TRUE,
  93. 'not null' => TRUE,
  94. 'description' => 'The entity id this translation revision relates to',
  95. ),
  96. 'revision_id' => array(
  97. 'type' => 'int',
  98. 'unsigned' => TRUE,
  99. 'not null' => TRUE,
  100. 'description' => 'The entity revision id this translation relates to',
  101. ),
  102. 'language' => array(
  103. 'type' => 'varchar',
  104. 'length' => 32,
  105. 'not null' => TRUE,
  106. 'default' => '',
  107. 'description' => 'The target language for this translation revision.',
  108. ),
  109. 'source' => array(
  110. 'type' => 'varchar',
  111. 'length' => 32,
  112. 'not null' => TRUE,
  113. 'default' => '',
  114. 'description' => 'The source language from which this translation revision was created.',
  115. ),
  116. 'uid' => array(
  117. 'description' => 'The author of this translation revision.',
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. ),
  122. 'status' => array(
  123. 'description' => 'Boolean indicating whether the translation revision is published (visible to non-administrators).',
  124. 'type' => 'int',
  125. 'not null' => TRUE,
  126. 'default' => 1,
  127. ),
  128. 'translate' => array(
  129. 'description' => 'A boolean indicating whether this translation revision needs to be updated.',
  130. 'type' => 'int',
  131. 'not null' => TRUE,
  132. 'default' => 0,
  133. ),
  134. 'created' => array(
  135. 'description' => 'The Unix timestamp when the translation revision was created.',
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. 'default' => 0,
  139. ),
  140. 'changed' => array(
  141. 'description' => 'The Unix timestamp when the translation revision was most recently saved.',
  142. 'type' => 'int',
  143. 'not null' => TRUE,
  144. 'default' => 0,
  145. ),
  146. ),
  147. 'primary key' => array('entity_type', 'revision_id', 'language'),
  148. 'indexes'=> array('revision_id' => array('revision_id')),
  149. );
  150. return $schema;
  151. }
  152. /**
  153. * Implements hook_install().
  154. */
  155. function entity_translation_install() {
  156. // entity_translation_form_alter() needs to run after locale_form_alter() and
  157. // translation_menu(); entity_translation_menu_alter() needs to run after
  158. // i18n_node_menu_alter().
  159. db_update('system')
  160. ->fields(array('weight' => 11))
  161. ->condition('name', 'entity_translation')
  162. ->execute();
  163. // Enable translation for nodes.
  164. variable_set('entity_translation_entity_types', array('node' => 'node'));
  165. // Make translation use the content language type.
  166. variable_set('translation_language_type', LANGUAGE_TYPE_CONTENT);
  167. // Enable revision support for entity translation.
  168. variable_set('entity_translation_revision_enabled', TRUE);
  169. }
  170. /**
  171. * Grant 'edit $type original values' permission to existing roles.
  172. */
  173. function _entity_translation_grant_edit_permissions() {
  174. variable_set('entity_translation_workflow_enabled', TRUE);
  175. $permissions = array();
  176. // Nodes.
  177. $permissions['node'][] = 'bypass node access';
  178. foreach (node_permissions_get_configured_types() as $type) {
  179. $permissions['node'][] = "edit own $type content";
  180. $permissions['node'][] = "edit any $type content";
  181. }
  182. // Comments.
  183. if (module_exists('comment')) {
  184. $permissions['comment'][] = 'administer comments';
  185. $permissions['comment'][] = 'edit own comments';
  186. }
  187. // Taxonomy terms.
  188. if (module_exists('taxonomy')) {
  189. $permissions['taxonomy_term'][] = 'administer taxonomy';
  190. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  191. $permissions['taxonomy_term'][] = "edit terms in {$vocabulary->vid}";
  192. }
  193. }
  194. $assignments = array();
  195. foreach ($permissions as $entity_type => $permissions_filter) {
  196. if (entity_translation_enabled($entity_type)) {
  197. $permission = "edit $entity_type original values";
  198. $assignments[] = _entity_translation_grant_permission($permission, $permissions_filter);
  199. $permission = "edit $entity_type translation shared fields";
  200. $assignments[] = _entity_translation_grant_permission($permission, $permissions_filter);
  201. }
  202. }
  203. $assignments = '<ul><li>' . implode('</li><li>', $assignments) . '</li></ul>';
  204. $t = get_t();
  205. return $t('The following permissions have been assigned to existing roles: !assignments', array('!assignments' => $assignments));
  206. }
  207. /**
  208. * Grant the given permission to all roles which already have any of the
  209. * permissions specified in the $permissions_filter parameter.
  210. *
  211. * @param $permission
  212. * The new permission which to grant.
  213. * @param $permissions_filter
  214. * List of permissions used for loading roles.
  215. *
  216. * @return
  217. * A message describing permission changes.
  218. */
  219. function _entity_translation_grant_permission($permission, $permissions_filter = NULL) {
  220. $roles = user_roles(FALSE, $permissions_filter);
  221. foreach ($roles as $rid => $role) {
  222. user_role_grant_permissions($rid, array($permission));
  223. }
  224. $t = get_t();
  225. return $t('%permission was assigned to %roles', array(
  226. '%permission' => $permission,
  227. '%roles' => implode(', ', $roles)
  228. ));
  229. }
  230. /**
  231. * Implements hook_enable().
  232. */
  233. function entity_translation_enable() {
  234. // Re-activate entity translation for content types which had used it when
  235. // the module was last disabled (if any), unless these have since been altered
  236. // by the user to use a different translation option.
  237. $entity_translation_types = variable_get('entity_translation_disabled_content_types', array());
  238. foreach ($entity_translation_types as $index => $type) {
  239. if (variable_get("language_content_type_$type", 0) == 0) {
  240. variable_set("language_content_type_$type", ENTITY_TRANSLATION_ENABLED);
  241. }
  242. // We should show the warning only if we actually restored at least one
  243. // content type.
  244. else {
  245. unset($entity_translation_types[$index]);
  246. }
  247. }
  248. if ($entity_translation_types) {
  249. drupal_set_message(t('All content types previously configured to use field translation are now using it again.'), 'warning');
  250. }
  251. variable_del('entity_translation_disabled_content_types');
  252. }
  253. /**
  254. * Implements hook_disable().
  255. */
  256. function entity_translation_disable() {
  257. // Store record of which types are using entity translation, and set those
  258. // types to not be translated. These content types will be reset to use entity
  259. // translation again if the module is later re-enabled, unless they have been
  260. // changed by the user in the meantime.
  261. $entity_translation_types = array();
  262. foreach (node_type_get_types() as $type => $object) {
  263. if (variable_get("language_content_type_$type", 0) == ENTITY_TRANSLATION_ENABLED) {
  264. $entity_translation_types[] = $type;
  265. variable_set("language_content_type_$type", 0);
  266. }
  267. }
  268. if ($entity_translation_types) {
  269. variable_set('entity_translation_disabled_content_types', $entity_translation_types);
  270. 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');
  271. }
  272. }
  273. /**
  274. * Implements hook_uninstall().
  275. */
  276. function entity_translation_uninstall() {
  277. db_delete('variable')
  278. ->condition('name', db_like('entity_translation_') . '%', 'LIKE')
  279. ->execute();
  280. variable_del('translation_language_type');
  281. variable_del('locale_field_language_fallback');
  282. }
  283. /**
  284. * Implements hook_update_N().
  285. */
  286. function entity_translation_update_7001() {
  287. db_update('system')
  288. ->fields(array('weight' => 11))
  289. ->condition('name', 'entity_translation')
  290. ->execute();
  291. }
  292. /**
  293. * Grant 'edit original values' and 'edit shared field' permissions to roles which have entity editing permissions.
  294. */
  295. function entity_translation_update_7002() {
  296. // Grant the 'edit original values' permission, so we don't break editing on
  297. // existing sites.
  298. return _entity_translation_grant_edit_permissions();
  299. }
  300. /**
  301. * Configure node and comment language settings to the prior default behavior.
  302. */
  303. function entity_translation_update_7003() {
  304. module_load_include('inc', 'entity_translation', 'entity_translation.admin');
  305. foreach (array_keys(entity_get_info()) as $entity_type) {
  306. entity_translation_settings_init($entity_type);
  307. }
  308. }
  309. /**
  310. * Rebuild entity information to update the path scheme settings.
  311. */
  312. function entity_translation_update_7004() {
  313. entity_info_cache_clear();
  314. }
  315. /**
  316. * Rebuild the class registry to pick up the translation handler factory class.
  317. */
  318. function entity_translation_update_7005() {
  319. registry_rebuild();
  320. }
  321. /**
  322. * Add revision schema for entity translation metadata.
  323. */
  324. function entity_translation_update_7006() {
  325. // Create revision id column.
  326. $spec = array(
  327. 'type' => 'int',
  328. 'unsigned' => TRUE,
  329. // If we have existing data we cannot enforce this to be NOT NULL.
  330. 'not null' => FALSE,
  331. 'description' => 'The entity revision id this translation relates to',
  332. );
  333. db_add_field('entity_translation', 'revision_id', $spec);
  334. // Create the entity translation revision schema.
  335. $table = array(
  336. 'description' => 'Table to track entity translation revisions',
  337. 'fields' => array(
  338. 'entity_type' => array(
  339. 'type' => 'varchar',
  340. 'length' => 128,
  341. 'not null' => TRUE,
  342. 'default' => '',
  343. 'description' => 'The entity type this translation revision relates to',
  344. ),
  345. 'entity_id' => array(
  346. 'type' => 'int',
  347. 'unsigned' => TRUE,
  348. 'not null' => TRUE,
  349. 'description' => 'The entity id this translation revision relates to',
  350. ),
  351. 'revision_id' => array(
  352. 'type' => 'int',
  353. 'unsigned' => TRUE,
  354. 'not null' => TRUE,
  355. 'description' => 'The entity revision id this translation relates to',
  356. ),
  357. 'language' => array(
  358. 'type' => 'varchar',
  359. 'length' => 32,
  360. 'not null' => TRUE,
  361. 'default' => '',
  362. 'description' => 'The target language for this translation revision.',
  363. ),
  364. 'source' => array(
  365. 'type' => 'varchar',
  366. 'length' => 32,
  367. 'not null' => TRUE,
  368. 'default' => '',
  369. 'description' => 'The source language from which this translation revision was created.',
  370. ),
  371. 'uid' => array(
  372. 'description' => 'The author of this translation revision.',
  373. 'type' => 'int',
  374. 'not null' => TRUE,
  375. 'default' => 0,
  376. ),
  377. 'status' => array(
  378. 'description' => 'Boolean indicating whether the translation revision is published (visible to non-administrators).',
  379. 'type' => 'int',
  380. 'not null' => TRUE,
  381. 'default' => 1,
  382. ),
  383. 'translate' => array(
  384. 'description' => 'A boolean indicating whether this translation revision needs to be updated.',
  385. 'type' => 'int',
  386. 'not null' => TRUE,
  387. 'default' => 0,
  388. ),
  389. 'created' => array(
  390. 'description' => 'The Unix timestamp when the translation revision was created.',
  391. 'type' => 'int',
  392. 'not null' => TRUE,
  393. 'default' => 0,
  394. ),
  395. 'changed' => array(
  396. 'description' => 'The Unix timestamp when the translation revision was most recently saved.',
  397. 'type' => 'int',
  398. 'not null' => TRUE,
  399. 'default' => 0,
  400. ),
  401. ),
  402. 'primary key' => array('entity_type', 'revision_id', 'language'),
  403. 'indexes'=> array('revision_id' => array('revision_id')),
  404. );
  405. db_create_table('entity_translation_revision', $table);
  406. }
  407. /**
  408. * Disable revision support on existing installations.
  409. */
  410. function entity_translation_update_7007() {
  411. // Revision support is not enabled by default on existing installations as
  412. // making it work implies copying translation metadata to the
  413. // {entity_translation_revision} table for all the existing translations.
  414. // Since this process cannot be reliably implemented in an update function,
  415. // we leave the choice of manually performing the upgrade to people with the
  416. // required skills to do so. Be aware that enabling revision support on sites
  417. // where data has not been manually migrated may cause translation metadata to
  418. // be permanently lost or corrupted. See https://www.drupal.org/node/2396103.
  419. variable_set('entity_translation_revision_enabled', FALSE);
  420. }